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.
25 * $FreeBSD: src/sys/dev/sdhci/sdhci.c,v 1.8 2009/02/17 19:12:15 mav Exp $
28 #include <sys/param.h>
29 #include <sys/systm.h>
31 #include <sys/callout.h>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/spinlock.h>
37 #include <sys/resource.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
42 #include <bus/mmc/bridge.h>
43 #include <bus/mmc/mmcreg.h>
44 #include <bus/mmc/mmcbrvar.h>
50 SYSCTL_NODE(_hw
, OID_AUTO
, sdhci
, CTLFLAG_RD
, 0, "sdhci driver");
53 TUNABLE_INT("hw.sdhci.debug", &sdhci_debug
);
54 SYSCTL_INT(_hw_sdhci
, OID_AUTO
, debug
, CTLFLAG_RW
, &sdhci_debug
, 0, "Debug level");
56 #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off))
57 #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off))
58 #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off))
59 #define RD_MULTI_4(slot, off, ptr, count) \
60 SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
61 #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
62 #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
63 #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
64 #define WR_MULTI_4(slot, off, ptr, count) \
65 SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
67 static int slot_printf(struct sdhci_slot
*, const char *, ...)
70 static void sdhci_set_clock(struct sdhci_slot
*slot
, uint32_t clock
);
71 static void sdhci_start(struct sdhci_slot
*slot
);
72 static void sdhci_start_data(struct sdhci_slot
*slot
, struct mmc_data
*data
);
74 static void sdhci_card_task(void *, int);
77 #define SDHCI_LOCK(_slot) lockmgr(&(_slot)->lock, LK_EXCLUSIVE)
78 #define SDHCI_UNLOCK(_slot) lockmgr(&(_slot)->lock, LK_RELEASE)
79 #define SDHCI_LOCK_INIT(_slot) lockinit(&(_slot)->lock, "sdhci", 0, LK_CANRECURSE)
80 #define SDHCI_LOCK_DESTROY(_slot) lockuninit(&(_slot)->lock);
81 #define SDHCI_ASSERT_LOCKED(_slot) KKASSERT(lockstatus(&(_slot)->lock, curthread) != 0);
82 #define SDHCI_ASSERT_UNLOCKED(_slot) KKASSERT(lockstatus(&(_slot)->lock, curthread) == 0);
84 #define SDHCI_DEFAULT_MAX_FREQ 50
86 #define SDHCI_200_MAX_DIVIDER 256
87 #define SDHCI_300_MAX_DIVIDER 2046
90 * Broadcom BCM577xx Controller Constants
92 #define BCM577XX_DEFAULT_MAX_DIVIDER 256 /* Maximum divider supported by the default clock source. */
93 #define BCM577XX_ALT_CLOCK_BASE 63000000 /* Alternative clock's base frequency. */
95 #define BCM577XX_HOST_CONTROL 0x198
96 #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF
97 #define BCM577XX_CTRL_CLKSEL_SHIFT 12
98 #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0
99 #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3
103 slot_printf(struct sdhci_slot
*slot
, const char * fmt
, ...)
108 retval
= kprintf("%s-slot%d: ",
109 device_get_nameunit(slot
->bus
), slot
->num
);
112 retval
+= kvprintf(fmt
, ap
);
118 sdhci_dumpregs(struct sdhci_slot
*slot
)
121 "============== REGISTER DUMP ==============\n");
123 slot_printf(slot
, "SDMA addr: 0x%08x | Version: 0x%08x\n",
124 RD4(slot
, SDHCI_SDMA_ADDRESS
), RD2(slot
, SDHCI_HOST_VERSION
));
125 slot_printf(slot
, "Blk size: 0x%08x | Blk cnt: 0x%08x\n",
126 RD2(slot
, SDHCI_BLOCK_SIZE
), RD2(slot
, SDHCI_BLOCK_COUNT
));
127 slot_printf(slot
, "Argument: 0x%08x | Trn mode: 0x%08x\n",
128 RD4(slot
, SDHCI_ARGUMENT
), RD2(slot
, SDHCI_TRANSFER_MODE
));
129 slot_printf(slot
, "Present: 0x%08x | Host ctl: 0x%08x\n",
130 RD4(slot
, SDHCI_PRESENT_STATE
), RD1(slot
, SDHCI_HOST_CONTROL
));
131 slot_printf(slot
, "Power: 0x%08x | Blk gap: 0x%08x\n",
132 RD1(slot
, SDHCI_POWER_CONTROL
), RD1(slot
, SDHCI_BLOCK_GAP_CONTROL
));
133 slot_printf(slot
, "Wake-up: 0x%08x | Clock: 0x%08x\n",
134 RD1(slot
, SDHCI_WAKE_UP_CONTROL
), RD2(slot
, SDHCI_CLOCK_CONTROL
));
135 slot_printf(slot
, "Timeout: 0x%08x | Int stat: 0x%08x\n",
136 RD1(slot
, SDHCI_TIMEOUT_CONTROL
), RD4(slot
, SDHCI_INT_STATUS
));
137 slot_printf(slot
, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
138 RD4(slot
, SDHCI_INT_ENABLE
), RD4(slot
, SDHCI_SIGNAL_ENABLE
));
139 slot_printf(slot
, "AC12 err: 0x%08x | Slot int: 0x%08x\n",
140 RD2(slot
, SDHCI_ACMD12_ERR
), RD2(slot
, SDHCI_SLOT_INT_STATUS
));
141 slot_printf(slot
, "Caps: 0x%08x | Max curr: 0x%08x\n",
142 RD4(slot
, SDHCI_CAPABILITIES
), RD4(slot
, SDHCI_MAX_CURRENT
));
145 "===========================================\n");
149 sdhci_reset(struct sdhci_slot
*slot
, uint8_t mask
)
153 if (slot
->quirks
& SDHCI_QUIRK_NO_CARD_NO_RESET
) {
154 if (!(RD4(slot
, SDHCI_PRESENT_STATE
) &
159 /* Some controllers need this kick or reset won't work. */
160 if ((mask
& SDHCI_RESET_ALL
) == 0 &&
161 (slot
->quirks
& SDHCI_QUIRK_CLOCK_BEFORE_RESET
)) {
164 /* This is to force an update */
167 sdhci_set_clock(slot
, clock
);
170 if (mask
& SDHCI_RESET_ALL
) {
175 WR1(slot
, SDHCI_SOFTWARE_RESET
, mask
);
177 if (slot
->quirks
& SDHCI_QUIRK_WAITFOR_RESET_ASSERTED
) {
179 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
180 * specification. The reset bit has internal propagation delay,
181 * so a fast read after write returns 0 even if reset process is
182 * in progress. The workaround is to poll for 1 before polling
183 * for 0. In the worst case, if we miss seeing it asserted the
184 * time we spent waiting is enough to ensure the reset finishes.
187 while ((RD1(slot
, SDHCI_SOFTWARE_RESET
) & mask
) != mask
) {
195 /* Wait max 100 ms */
197 /* Controller clears the bits when it's done */
198 while (RD1(slot
, SDHCI_SOFTWARE_RESET
) & mask
) {
200 slot_printf(slot
, "Reset 0x%x never completed.\n",
202 sdhci_dumpregs(slot
);
211 sdhci_init(struct sdhci_slot
*slot
)
214 sdhci_reset(slot
, SDHCI_RESET_ALL
);
216 /* Enable interrupts. */
217 slot
->intmask
= SDHCI_INT_BUS_POWER
| SDHCI_INT_DATA_END_BIT
|
218 SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_TIMEOUT
| SDHCI_INT_INDEX
|
219 SDHCI_INT_END_BIT
| SDHCI_INT_CRC
| SDHCI_INT_TIMEOUT
|
220 SDHCI_INT_CARD_REMOVE
| SDHCI_INT_CARD_INSERT
|
221 SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
|
222 SDHCI_INT_DMA_END
| SDHCI_INT_DATA_END
| SDHCI_INT_RESPONSE
|
224 WR4(slot
, SDHCI_INT_ENABLE
, slot
->intmask
);
225 WR4(slot
, SDHCI_SIGNAL_ENABLE
, slot
->intmask
);
229 sdhci_set_clock(struct sdhci_slot
*slot
, uint32_t clock
)
238 if (clock
== slot
->clock
)
242 /* Turn off the clock. */
243 clk
= RD2(slot
, SDHCI_CLOCK_CONTROL
);
244 WR2(slot
, SDHCI_CLOCK_CONTROL
, clk
& ~SDHCI_CLOCK_CARD_EN
);
245 /* If no clock requested - left it so. */
249 /* Determine the clock base frequency */
250 clk_base
= slot
->max_clk
;
251 if (slot
->quirks
& SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC
) {
252 clk_sel
= RD2(slot
, BCM577XX_HOST_CONTROL
) & BCM577XX_CTRL_CLKSEL_MASK
;
254 /* Select clock source appropriate for the requested frequency. */
255 if ((clk_base
/ BCM577XX_DEFAULT_MAX_DIVIDER
) > clock
) {
256 clk_base
= BCM577XX_ALT_CLOCK_BASE
;
257 clk_sel
|= (BCM577XX_CTRL_CLKSEL_64MHZ
<< BCM577XX_CTRL_CLKSEL_SHIFT
);
259 clk_sel
|= (BCM577XX_CTRL_CLKSEL_DEFAULT
<< BCM577XX_CTRL_CLKSEL_SHIFT
);
262 WR2(slot
, BCM577XX_HOST_CONTROL
, clk_sel
);
265 /* Recalculate timeout clock frequency based on the new sd clock. */
266 if (slot
->quirks
& SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
)
267 slot
->timeout_clk
= slot
->clock
/ 1000;
269 if (slot
->version
< SDHCI_SPEC_300
) {
270 /* Looking for highest freq <= clock. */
272 for (div
= 1; div
< SDHCI_200_MAX_DIVIDER
; div
<<= 1) {
277 /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
281 /* Version 3.0 divisors are multiples of two up to 1023*2 */
282 if (clock
>= clk_base
)
285 for (div
= 2; div
< SDHCI_300_MAX_DIVIDER
; div
+= 2) {
286 if ((clk_base
/ div
) <= clock
)
293 if (bootverbose
|| sdhci_debug
)
294 slot_printf(slot
, "Divider %d for freq %d (base %d)\n",
295 div
, clock
, clk_base
);
297 /* Now we have got divider, set it. */
298 clk
= (div
& SDHCI_DIVIDER_MASK
) << SDHCI_DIVIDER_SHIFT
;
299 clk
|= ((div
>> SDHCI_DIVIDER_MASK_LEN
) & SDHCI_DIVIDER_HI_MASK
)
300 << SDHCI_DIVIDER_HI_SHIFT
;
302 WR2(slot
, SDHCI_CLOCK_CONTROL
, clk
);
304 clk
|= SDHCI_CLOCK_INT_EN
;
305 WR2(slot
, SDHCI_CLOCK_CONTROL
, clk
);
306 /* Wait up to 10 ms until it stabilize. */
308 while (!((clk
= RD2(slot
, SDHCI_CLOCK_CONTROL
))
309 & SDHCI_CLOCK_INT_STABLE
)) {
312 "Internal clock never stabilised.\n");
313 sdhci_dumpregs(slot
);
319 /* Pass clock signal to the bus. */
320 clk
|= SDHCI_CLOCK_CARD_EN
;
321 WR2(slot
, SDHCI_CLOCK_CONTROL
, clk
);
325 sdhci_set_power(struct sdhci_slot
*slot
, u_char power
)
329 if (slot
->power
== power
)
334 /* Turn off the power. */
336 WR1(slot
, SDHCI_POWER_CONTROL
, pwr
);
337 /* If power down requested - left it so. */
341 switch (1 << power
) {
342 case MMC_OCR_LOW_VOLTAGE
:
343 pwr
|= SDHCI_POWER_180
;
345 case MMC_OCR_290_300
:
346 case MMC_OCR_300_310
:
347 pwr
|= SDHCI_POWER_300
;
349 case MMC_OCR_320_330
:
350 case MMC_OCR_330_340
:
351 pwr
|= SDHCI_POWER_330
;
354 WR1(slot
, SDHCI_POWER_CONTROL
, pwr
);
355 /* Turn on the power. */
356 pwr
|= SDHCI_POWER_ON
;
357 WR1(slot
, SDHCI_POWER_CONTROL
, pwr
);
361 sdhci_read_block_pio(struct sdhci_slot
*slot
)
367 buffer
= slot
->curcmd
->data
->data
;
368 buffer
+= slot
->offset
;
369 /* Transfer one block at a time. */
370 left
= min(512, slot
->curcmd
->data
->len
- slot
->offset
);
371 slot
->offset
+= left
;
373 /* If we are too fast, broken controllers return zeroes. */
374 if (slot
->quirks
& SDHCI_QUIRK_BROKEN_TIMINGS
)
376 /* Handle unaligned and aligned buffer cases. */
377 if ((intptr_t)buffer
& 3) {
379 data
= RD4(slot
, SDHCI_BUFFER
);
381 buffer
[1] = (data
>> 8);
382 buffer
[2] = (data
>> 16);
383 buffer
[3] = (data
>> 24);
388 RD_MULTI_4(slot
, SDHCI_BUFFER
,
389 (uint32_t *)buffer
, left
>> 2);
392 /* Handle uneven size case. */
394 data
= RD4(slot
, SDHCI_BUFFER
);
404 sdhci_write_block_pio(struct sdhci_slot
*slot
)
410 buffer
= slot
->curcmd
->data
->data
;
411 buffer
+= slot
->offset
;
412 /* Transfer one block at a time. */
413 left
= min(512, slot
->curcmd
->data
->len
- slot
->offset
);
414 slot
->offset
+= left
;
416 /* Handle unaligned and aligned buffer cases. */
417 if ((intptr_t)buffer
& 3) {
425 WR4(slot
, SDHCI_BUFFER
, data
);
428 WR_MULTI_4(slot
, SDHCI_BUFFER
,
429 (uint32_t *)buffer
, left
>> 2);
432 /* Handle uneven size case. */
439 WR4(slot
, SDHCI_BUFFER
, data
);
444 sdhci_transfer_pio(struct sdhci_slot
*slot
)
447 /* Read as many blocks as possible. */
448 if (slot
->curcmd
->data
->flags
& MMC_DATA_READ
) {
449 while (RD4(slot
, SDHCI_PRESENT_STATE
) &
450 SDHCI_DATA_AVAILABLE
) {
451 sdhci_read_block_pio(slot
);
452 if (slot
->offset
>= slot
->curcmd
->data
->len
)
456 while (RD4(slot
, SDHCI_PRESENT_STATE
) &
457 SDHCI_SPACE_AVAILABLE
) {
458 sdhci_write_block_pio(slot
);
459 if (slot
->offset
>= slot
->curcmd
->data
->len
)
466 sdhci_card_delay(void *arg
)
468 struct sdhci_slot
*slot
= arg
;
470 taskqueue_enqueue(taskqueue_swi
, &slot
->card_task
);
474 sdhci_card_task(void *arg
, int pending
)
476 struct sdhci_slot
*slot
= arg
;
479 if (RD4(slot
, SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
) {
480 if (slot
->dev
== NULL
) {
481 /* If card is present - attach mmc bus. */
482 slot
->dev
= device_add_child(slot
->bus
, "mmc", -1);
483 device_set_ivars(slot
->dev
, slot
);
485 device_probe_and_attach(slot
->dev
);
489 if (slot
->dev
!= NULL
) {
490 /* If no card present - detach mmc bus. */
491 device_t d
= slot
->dev
;
494 device_delete_child(slot
->bus
, d
);
501 sdhci_init_slot(device_t dev
, struct sdhci_slot
*slot
, int num
)
506 SDHCI_LOCK_INIT(slot
);
510 /* Allocate DMA memory for SDMA. */
511 err
= bus_dmamem_coherent(bus_get_dma_tag(dev
),
512 DMA_BLOCK_SIZE
, 0, BUS_SPACE_MAXADDR_32BIT
,
513 BUS_SPACE_MAXADDR
, DMA_BLOCK_SIZE
, BUS_DMA_NOWAIT
,
516 device_printf(dev
, "Can't alloc DMA memory for SDMA\n");
517 SDHCI_LOCK_DESTROY(slot
);
521 /* Initialize slot. */
523 slot
->version
= (RD2(slot
, SDHCI_HOST_VERSION
)
524 >> SDHCI_SPEC_VER_SHIFT
) & SDHCI_SPEC_VER_MASK
;
525 if (slot
->quirks
& SDHCI_QUIRK_MISSING_CAPS
)
528 caps
= RD4(slot
, SDHCI_CAPABILITIES
);
529 /* Calculate base clock frequency. */
530 if (slot
->version
>= SDHCI_SPEC_300
)
531 freq
= (caps
& SDHCI_CLOCK_V3_BASE_MASK
) >>
532 SDHCI_CLOCK_BASE_SHIFT
;
534 freq
= (caps
& SDHCI_CLOCK_BASE_MASK
) >>
535 SDHCI_CLOCK_BASE_SHIFT
;
537 slot
->max_clk
= freq
* 1000000;
539 * If the frequency wasn't in the capabilities and the hardware driver
540 * hasn't already set max_clk we're probably not going to work right
541 * with an assumption, so complain about it.
543 if (slot
->max_clk
== 0) {
544 slot
->max_clk
= SDHCI_DEFAULT_MAX_FREQ
* 1000000;
545 device_printf(dev
, "Hardware doesn't specify base clock "
546 "frequency, using %dMHz as default.\n", SDHCI_DEFAULT_MAX_FREQ
);
548 /* Calculate timeout clock frequency. */
549 if (slot
->quirks
& SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
) {
550 slot
->timeout_clk
= slot
->max_clk
/ 1000;
553 (caps
& SDHCI_TIMEOUT_CLK_MASK
) >> SDHCI_TIMEOUT_CLK_SHIFT
;
554 if (caps
& SDHCI_TIMEOUT_CLK_UNIT
)
555 slot
->timeout_clk
*= 1000;
558 * If the frequency wasn't in the capabilities and the hardware driver
559 * hasn't already set timeout_clk we'll probably work okay using the
560 * max timeout, but still mention it.
562 if (slot
->timeout_clk
== 0) {
563 device_printf(dev
, "Hardware doesn't specify timeout clock "
564 "frequency, setting BROKEN_TIMEOUT quirk.\n");
565 slot
->quirks
|= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
;
568 slot
->host
.f_min
= SDHCI_MIN_FREQ(slot
->bus
, slot
);
569 slot
->host
.f_max
= slot
->max_clk
;
570 slot
->host
.host_ocr
= 0;
571 if (caps
& SDHCI_CAN_VDD_330
)
572 slot
->host
.host_ocr
|= MMC_OCR_320_330
| MMC_OCR_330_340
;
573 if (caps
& SDHCI_CAN_VDD_300
)
574 slot
->host
.host_ocr
|= MMC_OCR_290_300
| MMC_OCR_300_310
;
575 if (caps
& SDHCI_CAN_VDD_180
)
576 slot
->host
.host_ocr
|= MMC_OCR_LOW_VOLTAGE
;
577 if (slot
->host
.host_ocr
== 0) {
578 device_printf(dev
, "Hardware doesn't report any "
579 "support voltages.\n");
581 slot
->host
.caps
= MMC_CAP_4_BIT_DATA
;
582 if (caps
& SDHCI_CAN_DO_8BITBUS
)
583 slot
->host
.caps
|= MMC_CAP_8_BIT_DATA
;
584 if (caps
& SDHCI_CAN_DO_HISPD
)
585 slot
->host
.caps
|= MMC_CAP_HSPEED
;
586 /* Decide if we have usable DMA. */
587 if (caps
& SDHCI_CAN_DO_DMA
)
588 slot
->opt
|= SDHCI_HAVE_SDMA
;
590 if (slot
->quirks
& SDHCI_QUIRK_BROKEN_DMA
)
591 slot
->opt
&= ~SDHCI_HAVE_SDMA
;
592 if (slot
->quirks
& SDHCI_QUIRK_FORCE_SDMA
)
593 slot
->opt
|= SDHCI_HAVE_SDMA
;
596 * Use platform-provided transfer backend
597 * with PIO as a fallback mechanism
599 if (slot
->opt
& SDHCI_PLATFORM_TRANSFER
)
600 slot
->opt
&= ~SDHCI_HAVE_SDMA
;
602 if (bootverbose
|| sdhci_debug
) {
603 slot_printf(slot
, "%uMHz%s %s%s%s%s %s\n",
604 slot
->max_clk
/ 1000000,
605 (caps
& SDHCI_CAN_DO_HISPD
) ? " HS" : "",
606 (caps
& MMC_CAP_8_BIT_DATA
) ? "8bits" :
607 ((caps
& MMC_CAP_4_BIT_DATA
) ? "4bits" : "1bit"),
608 (caps
& SDHCI_CAN_VDD_330
) ? " 3.3V" : "",
609 (caps
& SDHCI_CAN_VDD_300
) ? " 3.0V" : "",
610 (caps
& SDHCI_CAN_VDD_180
) ? " 1.8V" : "",
611 (slot
->opt
& SDHCI_HAVE_SDMA
) ? "SDMA" : "PIO");
612 sdhci_dumpregs(slot
);
617 SYSCTL_ADD_INT(device_get_sysctl_ctx(slot
->bus
),
618 SYSCTL_CHILDREN(device_get_sysctl_tree(slot
->bus
)), OID_AUTO
,
619 "timeout", CTLFLAG_RW
, &slot
->timeout
, 0,
620 "Maximum timeout for SDHCI transfers (in secs)");
621 TASK_INIT(&slot
->card_task
, 0, sdhci_card_task
, slot
);
622 callout_init(&slot
->card_callout
);
623 callout_init_lk(&slot
->timeout_callout
, &slot
->lock
);
628 sdhci_start_slot(struct sdhci_slot
*slot
)
630 sdhci_card_task(slot
, 0);
634 sdhci_cleanup_slot(struct sdhci_slot
*slot
)
639 callout_drain(&slot
->timeout_callout
);
640 callout_drain(&slot
->card_callout
);
641 taskqueue_drain(taskqueue_swi
, &slot
->card_task
);
648 device_delete_child(slot
->bus
, d
);
651 sdhci_reset(slot
, SDHCI_RESET_ALL
);
654 sdma
= &slot
->sdma_mem
;
655 bus_dmamap_unload(sdma
->dmem_tag
, sdma
->dmem_map
);
656 bus_dmamem_free(sdma
->dmem_tag
, sdma
->dmem_addr
, sdma
->dmem_map
);
657 bus_dma_tag_destroy(sdma
->dmem_tag
);
659 SDHCI_LOCK_DESTROY(slot
);
665 sdhci_generic_suspend(struct sdhci_slot
*slot
)
667 sdhci_reset(slot
, SDHCI_RESET_ALL
);
673 sdhci_generic_resume(struct sdhci_slot
*slot
)
681 sdhci_generic_min_freq(device_t brdev
, struct sdhci_slot
*slot
)
683 if (slot
->version
>= SDHCI_SPEC_300
)
684 return (slot
->max_clk
/ SDHCI_300_MAX_DIVIDER
);
686 return (slot
->max_clk
/ SDHCI_200_MAX_DIVIDER
);
690 sdhci_generic_update_ios(device_t brdev
, device_t reqdev
)
692 struct sdhci_slot
*slot
= device_get_ivars(reqdev
);
693 struct mmc_ios
*ios
= &slot
->host
.ios
;
696 /* Do full reset on bus power down to clear from any state. */
697 if (ios
->power_mode
== power_off
) {
698 WR4(slot
, SDHCI_SIGNAL_ENABLE
, 0);
701 /* Configure the bus. */
702 sdhci_set_clock(slot
, ios
->clock
);
703 sdhci_set_power(slot
, (ios
->power_mode
== power_off
) ? 0 : ios
->vdd
);
704 if (ios
->bus_width
== bus_width_8
) {
705 slot
->hostctrl
|= SDHCI_CTRL_8BITBUS
;
706 slot
->hostctrl
&= ~SDHCI_CTRL_4BITBUS
;
707 } else if (ios
->bus_width
== bus_width_4
) {
708 slot
->hostctrl
&= ~SDHCI_CTRL_8BITBUS
;
709 slot
->hostctrl
|= SDHCI_CTRL_4BITBUS
;
710 } else if (ios
->bus_width
== bus_width_1
) {
711 slot
->hostctrl
&= ~SDHCI_CTRL_8BITBUS
;
712 slot
->hostctrl
&= ~SDHCI_CTRL_4BITBUS
;
714 panic("Invalid bus width: %d", ios
->bus_width
);
716 if (ios
->timing
== bus_timing_hs
&&
717 !(slot
->quirks
& SDHCI_QUIRK_DONT_SET_HISPD_BIT
))
718 slot
->hostctrl
|= SDHCI_CTRL_HISPD
;
720 slot
->hostctrl
&= ~SDHCI_CTRL_HISPD
;
721 WR1(slot
, SDHCI_HOST_CONTROL
, slot
->hostctrl
);
722 /* Some controllers like reset after bus changes. */
723 if(slot
->quirks
& SDHCI_QUIRK_RESET_ON_IOS
)
724 sdhci_reset(slot
, SDHCI_RESET_CMD
| SDHCI_RESET_DATA
);
731 sdhci_req_done(struct sdhci_slot
*slot
)
733 struct mmc_request
*req
;
735 if (slot
->req
!= NULL
&& slot
->curcmd
!= NULL
) {
736 callout_stop(&slot
->timeout_callout
);
737 if (slot
->curcmd
->error
!= MMC_ERR_TIMEOUT
)
747 sdhci_timeout(void *arg
)
749 struct sdhci_slot
*slot
= arg
;
751 if (slot
->curcmd
!= NULL
) {
752 slot_printf(slot
, " Controller timeout\n");
753 sdhci_dumpregs(slot
);
754 sdhci_reset(slot
, SDHCI_RESET_CMD
|SDHCI_RESET_DATA
);
755 slot
->curcmd
->error
= MMC_ERR_TIMEOUT
;
756 sdhci_req_done(slot
);
758 slot_printf(slot
, " Spurious timeout - no active command\n");
763 sdhci_set_transfer_mode(struct sdhci_slot
*slot
,
764 struct mmc_data
*data
)
771 mode
= SDHCI_TRNS_BLK_CNT_EN
;
773 mode
|= SDHCI_TRNS_MULTI
;
774 if (data
->flags
& MMC_DATA_READ
)
775 mode
|= SDHCI_TRNS_READ
;
777 mode
|= SDHCI_TRNS_ACMD12
;
778 if (slot
->flags
& SDHCI_USE_SDMA
)
779 mode
|= SDHCI_TRNS_DMA
;
781 WR2(slot
, SDHCI_TRANSFER_MODE
, mode
);
785 sdhci_start_command(struct sdhci_slot
*slot
, struct mmc_command
*cmd
)
788 uint32_t mask
, state
;
793 cmd
->error
= MMC_ERR_NONE
;
795 /* This flags combination is not supported by controller. */
796 if ((cmd
->flags
& MMC_RSP_136
) && (cmd
->flags
& MMC_RSP_BUSY
)) {
797 slot_printf(slot
, "Unsupported response type!\n");
798 cmd
->error
= MMC_ERR_FAILED
;
799 sdhci_req_done(slot
);
803 /* Read controller present state. */
804 state
= RD4(slot
, SDHCI_PRESENT_STATE
);
805 /* Do not issue command if there is no card, clock or power.
806 * Controller will not detect timeout without clock active. */
807 if ((state
& SDHCI_CARD_PRESENT
) == 0 ||
810 cmd
->error
= MMC_ERR_FAILED
;
811 sdhci_req_done(slot
);
814 /* Always wait for free CMD bus. */
815 mask
= SDHCI_CMD_INHIBIT
;
816 /* Wait for free DAT if we have data or busy signal. */
817 if (cmd
->data
|| (cmd
->flags
& MMC_RSP_BUSY
))
818 mask
|= SDHCI_DAT_INHIBIT
;
819 /* We shouldn't wait for DAT for stop commands. */
820 if (cmd
== slot
->req
->stop
)
821 mask
&= ~SDHCI_DAT_INHIBIT
;
823 * Wait for bus no more then 250 ms. Typically there will be no wait
824 * here at all, but when writing a crash dump we may be bypassing the
825 * host platform's interrupt handler, and in some cases that handler
826 * may be working around hardware quirks such as not respecting r1b
827 * busy indications. In those cases, this wait-loop serves the purpose
828 * of waiting for the prior command and data transfers to be done, and
829 * SD cards are allowed to take up to 250ms for write and erase ops.
830 * (It's usually more like 20-30ms in the real world.)
833 while (state
& mask
) {
835 slot_printf(slot
, "Controller never released "
836 "inhibit bit(s).\n");
837 sdhci_dumpregs(slot
);
838 cmd
->error
= MMC_ERR_FAILED
;
839 sdhci_req_done(slot
);
844 state
= RD4(slot
, SDHCI_PRESENT_STATE
);
847 /* Prepare command flags. */
848 if (!(cmd
->flags
& MMC_RSP_PRESENT
))
849 flags
= SDHCI_CMD_RESP_NONE
;
850 else if (cmd
->flags
& MMC_RSP_136
)
851 flags
= SDHCI_CMD_RESP_LONG
;
852 else if (cmd
->flags
& MMC_RSP_BUSY
)
853 flags
= SDHCI_CMD_RESP_SHORT_BUSY
;
855 flags
= SDHCI_CMD_RESP_SHORT
;
856 if (cmd
->flags
& MMC_RSP_CRC
)
857 flags
|= SDHCI_CMD_CRC
;
858 if (cmd
->flags
& MMC_RSP_OPCODE
)
859 flags
|= SDHCI_CMD_INDEX
;
861 flags
|= SDHCI_CMD_DATA
;
862 if (cmd
->opcode
== MMC_STOP_TRANSMISSION
)
863 flags
|= SDHCI_CMD_TYPE_ABORT
;
865 sdhci_start_data(slot
, cmd
->data
);
867 * Interrupt aggregation: To reduce total number of interrupts
868 * group response interrupt with data interrupt when possible.
869 * If there going to be data interrupt, mask response one.
871 if (slot
->data_done
== 0) {
872 WR4(slot
, SDHCI_SIGNAL_ENABLE
,
873 slot
->intmask
&= ~SDHCI_INT_RESPONSE
);
875 /* Set command argument. */
876 WR4(slot
, SDHCI_ARGUMENT
, cmd
->arg
);
877 /* Set data transfer mode. */
878 sdhci_set_transfer_mode(slot
, cmd
->data
);
880 WR2(slot
, SDHCI_COMMAND_FLAGS
, (cmd
->opcode
<< 8) | (flags
& 0xff));
883 * Start timeout callout. Timeout is dropped to 2 seconds with
884 * repeated controller timeouts.
887 timeout
= slot
->timeout
/ 5;
889 timeout
= slot
->timeout
;
892 callout_reset(&slot
->timeout_callout
, timeout
* hz
,
893 sdhci_timeout
, slot
);
897 sdhci_finish_command(struct sdhci_slot
*slot
)
902 /* Interrupt aggregation: Restore command interrupt.
903 * Main restore point for the case when command interrupt
905 WR4(slot
, SDHCI_SIGNAL_ENABLE
, slot
->intmask
|= SDHCI_INT_RESPONSE
);
906 /* In case of error - reset host and return. */
907 if (slot
->curcmd
->error
) {
908 sdhci_reset(slot
, SDHCI_RESET_CMD
);
909 sdhci_reset(slot
, SDHCI_RESET_DATA
);
913 /* If command has response - fetch it. */
914 if (slot
->curcmd
->flags
& MMC_RSP_PRESENT
) {
915 if (slot
->curcmd
->flags
& MMC_RSP_136
) {
916 /* CRC is stripped so we need one byte shift. */
918 for (i
= 0; i
< 4; i
++) {
919 uint32_t val
= RD4(slot
, SDHCI_RESPONSE
+ i
* 4);
920 if (slot
->quirks
& SDHCI_QUIRK_DONT_SHIFT_RESPONSE
) {
921 slot
->curcmd
->resp
[3 - i
] = val
;
923 slot
->curcmd
->resp
[3 - i
] =
929 slot
->curcmd
->resp
[0] = RD4(slot
, SDHCI_RESPONSE
);
932 /* If data ready - finish. */
938 sdhci_start_data(struct sdhci_slot
*slot
, struct mmc_data
*data
)
940 uint32_t target_timeout
, current_timeout
;
943 if (data
== NULL
&& (slot
->curcmd
->flags
& MMC_RSP_BUSY
) == 0) {
950 /* Calculate and set data timeout.*/
951 /* XXX: We should have this from mmc layer, now assume 1 sec. */
952 if (slot
->quirks
& SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
) {
955 target_timeout
= 1000000;
957 current_timeout
= (1 << 13) * 1000 / slot
->timeout_clk
;
958 while (current_timeout
< target_timeout
&& div
< 0xE) {
960 current_timeout
<<= 1;
962 /* Compensate for an off-by-one error in the CaFe chip.*/
964 (slot
->quirks
& SDHCI_QUIRK_INCR_TIMEOUT_CONTROL
)) {
968 WR1(slot
, SDHCI_TIMEOUT_CONTROL
, div
);
973 /* Use DMA if possible. */
974 if ((slot
->opt
& SDHCI_HAVE_SDMA
))
975 slot
->flags
|= SDHCI_USE_SDMA
;
976 /* If data is small, broken DMA may return zeroes instead of data. */
977 if ((slot
->quirks
& SDHCI_QUIRK_BROKEN_TIMINGS
) &&
979 slot
->flags
&= ~SDHCI_USE_SDMA
;
980 /* Some controllers require even block sizes. */
981 if ((slot
->quirks
& SDHCI_QUIRK_32BIT_DMA_SIZE
) &&
983 slot
->flags
&= ~SDHCI_USE_SDMA
;
984 /* Load DMA buffer. */
985 if (slot
->flags
& SDHCI_USE_SDMA
) {
986 bus_dmamem_t
*sdma
= &slot
->sdma_mem
;
988 if (data
->flags
& MMC_DATA_READ
) {
989 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
990 BUS_DMASYNC_PREREAD
);
992 memcpy(sdma
->dmem_addr
, data
->data
,
993 (data
->len
< DMA_BLOCK_SIZE
) ?
994 data
->len
: DMA_BLOCK_SIZE
);
995 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
996 BUS_DMASYNC_PREWRITE
);
998 WR4(slot
, SDHCI_SDMA_ADDRESS
, sdma
->dmem_busaddr
);
999 /* Interrupt aggregation: Mask border interrupt
1000 * for the last page and unmask else. */
1001 if (data
->len
== DMA_BLOCK_SIZE
)
1002 slot
->intmask
&= ~SDHCI_INT_DMA_END
;
1004 slot
->intmask
|= SDHCI_INT_DMA_END
;
1005 WR4(slot
, SDHCI_SIGNAL_ENABLE
, slot
->intmask
);
1007 /* Current data offset for both PIO and DMA. */
1009 /* Set block size and request IRQ on 4K border. */
1010 WR2(slot
, SDHCI_BLOCK_SIZE
,
1011 SDHCI_MAKE_BLKSZ(DMA_BOUNDARY
, (data
->len
< 512)?data
->len
:512));
1012 /* Set block count. */
1013 WR2(slot
, SDHCI_BLOCK_COUNT
, (data
->len
+ 511) / 512);
1017 sdhci_finish_data(struct sdhci_slot
*slot
)
1019 struct mmc_data
*data
= slot
->curcmd
->data
;
1021 /* Interrupt aggregation: Restore command interrupt.
1022 * Auxiliary restore point for the case when data interrupt
1023 * happened first. */
1024 if (!slot
->cmd_done
) {
1025 WR4(slot
, SDHCI_SIGNAL_ENABLE
,
1026 slot
->intmask
|= SDHCI_INT_RESPONSE
);
1028 /* Unload rest of data from DMA buffer. */
1029 if (!slot
->data_done
&& (slot
->flags
& SDHCI_USE_SDMA
)) {
1030 bus_dmamem_t
*sdma
= &slot
->sdma_mem
;
1032 if (data
->flags
& MMC_DATA_READ
) {
1033 size_t left
= data
->len
- slot
->offset
;
1034 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1035 BUS_DMASYNC_POSTREAD
);
1036 memcpy((u_char
*)data
->data
+ slot
->offset
,
1038 (left
< DMA_BLOCK_SIZE
)?left
:DMA_BLOCK_SIZE
);
1040 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1041 BUS_DMASYNC_POSTWRITE
);
1043 slot
->data_done
= 1;
1044 /* If there was error - reset the host. */
1045 if (slot
->curcmd
->error
) {
1046 sdhci_reset(slot
, SDHCI_RESET_CMD
);
1047 sdhci_reset(slot
, SDHCI_RESET_DATA
);
1051 /* If we already have command response - finish. */
1057 sdhci_start(struct sdhci_slot
*slot
)
1059 struct mmc_request
*req
;
1065 if (!(slot
->flags
& CMD_STARTED
)) {
1066 slot
->flags
|= CMD_STARTED
;
1067 sdhci_start_command(slot
, req
->cmd
);
1070 /* We don't need this until using Auto-CMD12 feature
1071 if (!(slot->flags & STOP_STARTED) && req->stop) {
1072 slot->flags |= STOP_STARTED;
1073 sdhci_start_command(slot, req->stop);
1077 if (sdhci_debug
> 1)
1078 slot_printf(slot
, "result: %d\n", req
->cmd
->error
);
1079 if (!req
->cmd
->error
&&
1080 (slot
->quirks
& SDHCI_QUIRK_RESET_AFTER_REQUEST
)) {
1081 sdhci_reset(slot
, SDHCI_RESET_CMD
);
1082 sdhci_reset(slot
, SDHCI_RESET_DATA
);
1085 sdhci_req_done(slot
);
1089 sdhci_generic_request(device_t brdev
, device_t reqdev
, struct mmc_request
*req
)
1091 struct sdhci_slot
*slot
= device_get_ivars(reqdev
);
1094 if (slot
->req
!= NULL
) {
1098 if (sdhci_debug
> 1) {
1099 slot_printf(slot
, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1100 req
->cmd
->opcode
, req
->cmd
->arg
, req
->cmd
->flags
,
1101 (req
->cmd
->data
)?(u_int
)req
->cmd
->data
->len
:0,
1102 (req
->cmd
->data
)?req
->cmd
->data
->flags
:0);
1109 while (slot
->req
!= NULL
) {
1110 sdhci_generic_intr(slot
);
1118 sdhci_generic_get_ro(device_t brdev
, device_t reqdev
)
1120 struct sdhci_slot
*slot
= device_get_ivars(reqdev
);
1124 val
= RD4(slot
, SDHCI_PRESENT_STATE
);
1126 return (!(val
& SDHCI_WRITE_PROTECT
));
1130 sdhci_generic_acquire_host(device_t brdev
, device_t reqdev
)
1132 struct sdhci_slot
*slot
= device_get_ivars(reqdev
);
1136 while (slot
->bus_busy
)
1137 lksleep(slot
, &slot
->lock
, 0, "sdhciah", 0);
1140 WR1(slot
, SDHCI_HOST_CONTROL
, slot
->hostctrl
|= SDHCI_CTRL_LED
);
1146 sdhci_generic_release_host(device_t brdev
, device_t reqdev
)
1148 struct sdhci_slot
*slot
= device_get_ivars(reqdev
);
1151 /* Deactivate led. */
1152 WR1(slot
, SDHCI_HOST_CONTROL
, slot
->hostctrl
&= ~SDHCI_CTRL_LED
);
1160 sdhci_cmd_irq(struct sdhci_slot
*slot
, uint32_t intmask
)
1163 if (!slot
->curcmd
) {
1164 slot_printf(slot
, "Got command interrupt 0x%08x, but "
1165 "there is no active command.\n", intmask
);
1166 sdhci_dumpregs(slot
);
1169 if (intmask
& SDHCI_INT_TIMEOUT
)
1170 slot
->curcmd
->error
= MMC_ERR_TIMEOUT
;
1171 else if (intmask
& SDHCI_INT_CRC
)
1172 slot
->curcmd
->error
= MMC_ERR_BADCRC
;
1173 else if (intmask
& (SDHCI_INT_END_BIT
| SDHCI_INT_INDEX
))
1174 slot
->curcmd
->error
= MMC_ERR_FIFO
;
1176 sdhci_finish_command(slot
);
1180 sdhci_data_irq(struct sdhci_slot
*slot
, uint32_t intmask
)
1183 if (!slot
->curcmd
) {
1184 slot_printf(slot
, "Got data interrupt 0x%08x, but "
1185 "there is no active command.\n", intmask
);
1186 sdhci_dumpregs(slot
);
1189 if (slot
->curcmd
->data
== NULL
&&
1190 (slot
->curcmd
->flags
& MMC_RSP_BUSY
) == 0) {
1191 slot_printf(slot
, "Got data interrupt 0x%08x, but "
1192 "there is no active data operation.\n",
1194 sdhci_dumpregs(slot
);
1197 if (intmask
& SDHCI_INT_DATA_TIMEOUT
)
1198 slot
->curcmd
->error
= MMC_ERR_TIMEOUT
;
1199 else if (intmask
& (SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_END_BIT
))
1200 slot
->curcmd
->error
= MMC_ERR_BADCRC
;
1201 if (slot
->curcmd
->data
== NULL
&&
1202 (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
|
1203 SDHCI_INT_DMA_END
))) {
1204 slot_printf(slot
, "Got data interrupt 0x%08x, but "
1205 "there is busy-only command.\n", intmask
);
1206 sdhci_dumpregs(slot
);
1207 slot
->curcmd
->error
= MMC_ERR_INVALID
;
1209 if (slot
->curcmd
->error
) {
1210 /* No need to continue after any error. */
1214 /* Handle PIO interrupt. */
1215 if (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
)) {
1216 if ((slot
->opt
& SDHCI_PLATFORM_TRANSFER
) &&
1217 SDHCI_PLATFORM_WILL_HANDLE(slot
->bus
, slot
)) {
1218 SDHCI_PLATFORM_START_TRANSFER(slot
->bus
, slot
, &intmask
);
1219 slot
->flags
|= PLATFORM_DATA_STARTED
;
1221 sdhci_transfer_pio(slot
);
1223 /* Handle DMA border. */
1224 if (intmask
& SDHCI_INT_DMA_END
) {
1225 struct mmc_data
*data
= slot
->curcmd
->data
;
1226 bus_dmamem_t
*sdma
= &slot
->sdma_mem
;
1229 /* Unload DMA buffer... */
1230 left
= data
->len
- slot
->offset
;
1231 if (data
->flags
& MMC_DATA_READ
) {
1232 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1233 BUS_DMASYNC_POSTREAD
);
1234 memcpy((u_char
*)data
->data
+ slot
->offset
,
1236 (left
< DMA_BLOCK_SIZE
)?left
:DMA_BLOCK_SIZE
);
1238 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1239 BUS_DMASYNC_POSTWRITE
);
1241 /* ... and reload it again. */
1242 slot
->offset
+= DMA_BLOCK_SIZE
;
1243 left
= data
->len
- slot
->offset
;
1244 if (data
->flags
& MMC_DATA_READ
) {
1245 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1246 BUS_DMASYNC_PREREAD
);
1248 memcpy(sdma
->dmem_addr
,
1249 (u_char
*)data
->data
+ slot
->offset
,
1250 (left
< DMA_BLOCK_SIZE
)?left
:DMA_BLOCK_SIZE
);
1251 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1252 BUS_DMASYNC_PREWRITE
);
1254 /* Interrupt aggregation: Mask border interrupt
1255 * for the last page. */
1256 if (left
== DMA_BLOCK_SIZE
) {
1257 slot
->intmask
&= ~SDHCI_INT_DMA_END
;
1258 WR4(slot
, SDHCI_SIGNAL_ENABLE
, slot
->intmask
);
1261 WR4(slot
, SDHCI_SDMA_ADDRESS
, sdma
->dmem_busaddr
);
1263 /* We have got all data. */
1264 if (intmask
& SDHCI_INT_DATA_END
) {
1265 if (slot
->flags
& PLATFORM_DATA_STARTED
) {
1266 slot
->flags
&= ~PLATFORM_DATA_STARTED
;
1267 SDHCI_PLATFORM_FINISH_TRANSFER(slot
->bus
, slot
);
1269 sdhci_finish_data(slot
);
1273 if (slot
->curcmd
!= NULL
&& slot
->curcmd
->error
!= 0) {
1274 if (slot
->flags
& PLATFORM_DATA_STARTED
) {
1275 slot
->flags
&= ~PLATFORM_DATA_STARTED
;
1276 SDHCI_PLATFORM_FINISH_TRANSFER(slot
->bus
, slot
);
1278 sdhci_finish_data(slot
);
1284 sdhci_acmd_irq(struct sdhci_slot
*slot
)
1288 err
= RD4(slot
, SDHCI_ACMD12_ERR
);
1289 if (!slot
->curcmd
) {
1290 slot_printf(slot
, "Got AutoCMD12 error 0x%04x, but "
1291 "there is no active command.\n", err
);
1292 sdhci_dumpregs(slot
);
1295 slot_printf(slot
, "Got AutoCMD12 error 0x%04x\n", err
);
1296 sdhci_reset(slot
, SDHCI_RESET_CMD
);
1300 sdhci_generic_intr(struct sdhci_slot
*slot
)
1305 /* Read slot interrupt status. */
1306 intmask
= RD4(slot
, SDHCI_INT_STATUS
);
1307 if (intmask
== 0 || intmask
== 0xffffffff) {
1311 if (sdhci_debug
> 2)
1312 slot_printf(slot
, "Interrupt %#x\n", intmask
);
1314 /* Handle card presence interrupts. */
1315 if (intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
)) {
1316 WR4(slot
, SDHCI_INT_STATUS
, intmask
&
1317 (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
));
1319 if (intmask
& SDHCI_INT_CARD_REMOVE
) {
1320 if (bootverbose
|| sdhci_debug
)
1321 slot_printf(slot
, "Card removed\n");
1322 callout_stop(&slot
->card_callout
);
1323 taskqueue_enqueue(taskqueue_swi
,
1326 if (intmask
& SDHCI_INT_CARD_INSERT
) {
1327 if (bootverbose
|| sdhci_debug
)
1328 slot_printf(slot
, "Card inserted\n");
1329 callout_reset(&slot
->card_callout
, hz
/ 2,
1330 sdhci_card_delay
, slot
);
1332 intmask
&= ~(SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
);
1334 /* Handle command interrupts. */
1335 if (intmask
& SDHCI_INT_CMD_MASK
) {
1336 WR4(slot
, SDHCI_INT_STATUS
, intmask
& SDHCI_INT_CMD_MASK
);
1337 sdhci_cmd_irq(slot
, intmask
& SDHCI_INT_CMD_MASK
);
1339 /* Handle data interrupts. */
1340 if (intmask
& SDHCI_INT_DATA_MASK
) {
1341 WR4(slot
, SDHCI_INT_STATUS
, intmask
& SDHCI_INT_DATA_MASK
);
1342 /* Dont call data_irq in case of errored command */
1343 if ((intmask
& SDHCI_INT_CMD_ERROR_MASK
) == 0)
1344 sdhci_data_irq(slot
, intmask
& SDHCI_INT_DATA_MASK
);
1346 /* Handle AutoCMD12 error interrupt. */
1347 if (intmask
& SDHCI_INT_ACMD12ERR
) {
1348 WR4(slot
, SDHCI_INT_STATUS
, SDHCI_INT_ACMD12ERR
);
1349 sdhci_acmd_irq(slot
);
1351 intmask
&= ~(SDHCI_INT_CMD_MASK
| SDHCI_INT_DATA_MASK
);
1352 intmask
&= ~SDHCI_INT_ACMD12ERR
;
1353 intmask
&= ~SDHCI_INT_ERROR
;
1354 /* Handle bus power interrupt. */
1355 if (intmask
& SDHCI_INT_BUS_POWER
) {
1356 WR4(slot
, SDHCI_INT_STATUS
, SDHCI_INT_BUS_POWER
);
1358 "Card is consuming too much power!\n");
1359 intmask
&= ~SDHCI_INT_BUS_POWER
;
1361 /* The rest is unknown. */
1363 WR4(slot
, SDHCI_INT_STATUS
, intmask
);
1364 slot_printf(slot
, "Unexpected interrupt 0x%08x.\n",
1366 sdhci_dumpregs(slot
);
1373 sdhci_generic_read_ivar(device_t bus
, device_t child
, int which
, uintptr_t *result
)
1375 struct sdhci_slot
*slot
= device_get_ivars(child
);
1380 case MMCBR_IVAR_BUS_MODE
:
1381 *(int *)result
= slot
->host
.ios
.bus_mode
;
1383 case MMCBR_IVAR_BUS_WIDTH
:
1384 *(int *)result
= slot
->host
.ios
.bus_width
;
1386 case MMCBR_IVAR_CHIP_SELECT
:
1387 *(int *)result
= slot
->host
.ios
.chip_select
;
1389 case MMCBR_IVAR_CLOCK
:
1390 *(int *)result
= slot
->host
.ios
.clock
;
1392 case MMCBR_IVAR_F_MIN
:
1393 *(int *)result
= slot
->host
.f_min
;
1395 case MMCBR_IVAR_F_MAX
:
1396 *(int *)result
= slot
->host
.f_max
;
1398 case MMCBR_IVAR_HOST_OCR
:
1399 *(int *)result
= slot
->host
.host_ocr
;
1401 case MMCBR_IVAR_MODE
:
1402 *(int *)result
= slot
->host
.mode
;
1404 case MMCBR_IVAR_OCR
:
1405 *(int *)result
= slot
->host
.ocr
;
1407 case MMCBR_IVAR_POWER_MODE
:
1408 *(int *)result
= slot
->host
.ios
.power_mode
;
1410 case MMCBR_IVAR_VDD
:
1411 *(int *)result
= slot
->host
.ios
.vdd
;
1413 case MMCBR_IVAR_CAPS
:
1414 *(int *)result
= slot
->host
.caps
;
1416 case MMCBR_IVAR_TIMING
:
1417 *(int *)result
= slot
->host
.ios
.timing
;
1419 case MMCBR_IVAR_MAX_DATA
:
1420 *(int *)result
= 65535;
1427 sdhci_generic_write_ivar(device_t bus
, device_t child
, int which
, uintptr_t value
)
1429 struct sdhci_slot
*slot
= device_get_ivars(child
);
1434 case MMCBR_IVAR_BUS_MODE
:
1435 slot
->host
.ios
.bus_mode
= value
;
1437 case MMCBR_IVAR_BUS_WIDTH
:
1438 slot
->host
.ios
.bus_width
= value
;
1440 case MMCBR_IVAR_CHIP_SELECT
:
1441 slot
->host
.ios
.chip_select
= value
;
1443 case MMCBR_IVAR_CLOCK
:
1449 max_clock
= slot
->max_clk
;
1452 if (slot
->version
< SDHCI_SPEC_300
) {
1453 for (i
= 0; i
< SDHCI_200_MAX_DIVIDER
;
1461 for (i
= 0; i
< SDHCI_300_MAX_DIVIDER
;
1465 clock
= max_clock
/ (i
+ 2);
1469 slot
->host
.ios
.clock
= clock
;
1471 slot
->host
.ios
.clock
= 0;
1473 case MMCBR_IVAR_MODE
:
1474 slot
->host
.mode
= value
;
1476 case MMCBR_IVAR_OCR
:
1477 slot
->host
.ocr
= value
;
1479 case MMCBR_IVAR_POWER_MODE
:
1480 slot
->host
.ios
.power_mode
= value
;
1482 case MMCBR_IVAR_VDD
:
1483 slot
->host
.ios
.vdd
= value
;
1485 case MMCBR_IVAR_TIMING
:
1486 slot
->host
.ios
.timing
= value
;
1488 case MMCBR_IVAR_CAPS
:
1489 case MMCBR_IVAR_HOST_OCR
:
1490 case MMCBR_IVAR_F_MIN
:
1491 case MMCBR_IVAR_F_MAX
:
1492 case MMCBR_IVAR_MAX_DATA
:
1498 MODULE_VERSION(sdhci
, 1);