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);
76 static int sdhci_dma_alloc(struct sdhci_slot
*slot
);
77 static void sdhci_dmamem_free(bus_dmamem_t
*mem
);
78 static void sdhci_dma_free(struct sdhci_slot
*slot
);
79 static void sdhci_adma2_getaddr(void *arg
, bus_dma_segment_t
*segs
, int nsegs
,
83 #define SDHCI_LOCK(_slot) lockmgr(&(_slot)->lock, LK_EXCLUSIVE)
84 #define SDHCI_UNLOCK(_slot) lockmgr(&(_slot)->lock, LK_RELEASE)
85 #define SDHCI_LOCK_INIT(_slot) lockinit(&(_slot)->lock, "sdhci", 0, LK_CANRECURSE)
86 #define SDHCI_LOCK_DESTROY(_slot) lockuninit(&(_slot)->lock);
87 #define SDHCI_ASSERT_LOCKED(_slot) KKASSERT(lockstatus(&(_slot)->lock, curthread) != 0);
88 #define SDHCI_ASSERT_UNLOCKED(_slot) KKASSERT(lockstatus(&(_slot)->lock, curthread) == 0);
90 #define SDHCI_DEFAULT_MAX_FREQ 50
92 #define SDHCI_200_MAX_DIVIDER 256
93 #define SDHCI_300_MAX_DIVIDER 2046
96 * Broadcom BCM577xx Controller Constants
98 #define BCM577XX_DEFAULT_MAX_DIVIDER 256 /* Maximum divider supported by the default clock source. */
99 #define BCM577XX_ALT_CLOCK_BASE 63000000 /* Alternative clock's base frequency. */
101 #define BCM577XX_HOST_CONTROL 0x198
102 #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF
103 #define BCM577XX_CTRL_CLKSEL_SHIFT 12
104 #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0
105 #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3
108 slot_printf(struct sdhci_slot
*slot
, const char * fmt
, ...)
113 retval
= kprintf("%s-slot%d: ",
114 device_get_nameunit(slot
->bus
), slot
->num
);
117 retval
+= kvprintf(fmt
, ap
);
123 sdhci_dumpregs(struct sdhci_slot
*slot
)
126 "============== REGISTER DUMP ==============\n");
128 slot_printf(slot
, "SDMA addr: 0x%08x | Version: 0x%08x\n",
129 RD4(slot
, SDHCI_SDMA_ADDRESS
), RD2(slot
, SDHCI_HOST_VERSION
));
130 slot_printf(slot
, "Blk size: 0x%08x | Blk cnt: 0x%08x\n",
131 RD2(slot
, SDHCI_BLOCK_SIZE
), RD2(slot
, SDHCI_BLOCK_COUNT
));
132 slot_printf(slot
, "Argument: 0x%08x | Trn mode: 0x%08x\n",
133 RD4(slot
, SDHCI_ARGUMENT
), RD2(slot
, SDHCI_TRANSFER_MODE
));
134 slot_printf(slot
, "Present: 0x%08x | Host ctl: 0x%08x\n",
135 RD4(slot
, SDHCI_PRESENT_STATE
), RD1(slot
, SDHCI_HOST_CONTROL
));
136 slot_printf(slot
, "Power: 0x%08x | Blk gap: 0x%08x\n",
137 RD1(slot
, SDHCI_POWER_CONTROL
), RD1(slot
, SDHCI_BLOCK_GAP_CONTROL
));
138 slot_printf(slot
, "Wake-up: 0x%08x | Clock: 0x%08x\n",
139 RD1(slot
, SDHCI_WAKE_UP_CONTROL
), RD2(slot
, SDHCI_CLOCK_CONTROL
));
140 slot_printf(slot
, "Timeout: 0x%08x | Int stat: 0x%08x\n",
141 RD1(slot
, SDHCI_TIMEOUT_CONTROL
), RD4(slot
, SDHCI_INT_STATUS
));
142 slot_printf(slot
, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
143 RD4(slot
, SDHCI_INT_ENABLE
), RD4(slot
, SDHCI_SIGNAL_ENABLE
));
144 slot_printf(slot
, "AC12 err: 0x%08x | Host ctl2: 0x%08x\n",
145 RD2(slot
, SDHCI_ACMD12_ERR
), RD2(slot
, SDHCI_HOST_CONTROL2
));
146 slot_printf(slot
, "Caps: 0x%08x | Caps2: 0x%08x\n",
147 RD4(slot
, SDHCI_CAPABILITIES
), RD4(slot
, SDHCI_CAPABILITIES2
));
148 slot_printf(slot
, "Max curr: 0x%08x | ADMA err: 0x%08x\n",
149 RD4(slot
, SDHCI_MAX_CURRENT
), RD1(slot
, SDHCI_ADMA_ERR
));
150 slot_printf(slot
, "ADMA addr: 0x%08x | Slot int: 0x%08x\n",
151 RD4(slot
, SDHCI_ADMA_ADDRESS_LOW
), RD2(slot
, SDHCI_SLOT_INT_STATUS
));
154 "===========================================\n");
158 sdhci_reset(struct sdhci_slot
*slot
, uint8_t mask
)
162 if (slot
->quirks
& SDHCI_QUIRK_NO_CARD_NO_RESET
) {
163 if (!SDHCI_GET_CARD_PRESENT(slot
->bus
, slot
))
167 /* Some controllers need this kick or reset won't work. */
168 if ((mask
& SDHCI_RESET_ALL
) == 0 &&
169 (slot
->quirks
& SDHCI_QUIRK_CLOCK_BEFORE_RESET
)) {
172 /* This is to force an update */
175 sdhci_set_clock(slot
, clock
);
178 if (mask
& SDHCI_RESET_ALL
) {
183 WR1(slot
, SDHCI_SOFTWARE_RESET
, mask
);
185 if (slot
->quirks
& SDHCI_QUIRK_WAITFOR_RESET_ASSERTED
) {
187 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
188 * specification. The reset bit has internal propagation delay,
189 * so a fast read after write returns 0 even if reset process is
190 * in progress. The workaround is to poll for 1 before polling
191 * for 0. In the worst case, if we miss seeing it asserted the
192 * time we spent waiting is enough to ensure the reset finishes.
195 while ((RD1(slot
, SDHCI_SOFTWARE_RESET
) & mask
) != mask
) {
203 /* Wait max 100 ms */
205 /* Controller clears the bits when it's done */
206 while (RD1(slot
, SDHCI_SOFTWARE_RESET
) & mask
) {
208 slot_printf(slot
, "Reset 0x%x never completed.\n",
210 sdhci_dumpregs(slot
);
219 sdhci_init(struct sdhci_slot
*slot
)
222 sdhci_reset(slot
, SDHCI_RESET_ALL
);
224 /* Enable interrupts. */
225 slot
->intmask
= SDHCI_INT_BUS_POWER
| SDHCI_INT_DATA_END_BIT
|
226 SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_TIMEOUT
| SDHCI_INT_INDEX
|
227 SDHCI_INT_END_BIT
| SDHCI_INT_CRC
| SDHCI_INT_TIMEOUT
|
228 SDHCI_INT_CARD_REMOVE
| SDHCI_INT_CARD_INSERT
|
229 SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
|
230 SDHCI_INT_DMA_END
| SDHCI_INT_DATA_END
| SDHCI_INT_RESPONSE
|
231 SDHCI_INT_ACMD12ERR
| SDHCI_INT_ADMAERR
;
232 WR4(slot
, SDHCI_INT_ENABLE
, slot
->intmask
);
233 WR4(slot
, SDHCI_SIGNAL_ENABLE
, slot
->intmask
);
237 sdhci_set_clock(struct sdhci_slot
*slot
, uint32_t clock
)
246 if (clock
== slot
->clock
)
250 /* Turn off the clock. */
251 clk
= RD2(slot
, SDHCI_CLOCK_CONTROL
);
252 WR2(slot
, SDHCI_CLOCK_CONTROL
, clk
& ~SDHCI_CLOCK_CARD_EN
);
253 /* If no clock requested - left it so. */
257 /* Determine the clock base frequency */
258 clk_base
= slot
->max_clk
;
259 if (slot
->quirks
& SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC
) {
260 clk_sel
= RD2(slot
, BCM577XX_HOST_CONTROL
) & BCM577XX_CTRL_CLKSEL_MASK
;
262 /* Select clock source appropriate for the requested frequency. */
263 if ((clk_base
/ BCM577XX_DEFAULT_MAX_DIVIDER
) > clock
) {
264 clk_base
= BCM577XX_ALT_CLOCK_BASE
;
265 clk_sel
|= (BCM577XX_CTRL_CLKSEL_64MHZ
<< BCM577XX_CTRL_CLKSEL_SHIFT
);
267 clk_sel
|= (BCM577XX_CTRL_CLKSEL_DEFAULT
<< BCM577XX_CTRL_CLKSEL_SHIFT
);
270 WR2(slot
, BCM577XX_HOST_CONTROL
, clk_sel
);
273 /* Recalculate timeout clock frequency based on the new sd clock. */
274 if (slot
->quirks
& SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
)
275 slot
->timeout_clk
= slot
->clock
/ 1000;
277 if (slot
->version
< SDHCI_SPEC_300
) {
278 /* Looking for highest freq <= clock. */
280 for (div
= 1; div
< SDHCI_200_MAX_DIVIDER
; div
<<= 1) {
285 /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
289 /* Version 3.0 divisors are multiples of two up to 1023*2 */
290 if (clock
>= clk_base
)
293 for (div
= 2; div
< SDHCI_300_MAX_DIVIDER
; div
+= 2) {
294 if ((clk_base
/ div
) <= clock
)
301 if (bootverbose
|| sdhci_debug
)
302 slot_printf(slot
, "Divider %d for freq %d (base %d)\n",
303 div
, clock
, clk_base
);
305 /* Now we have got divider, set it. */
306 clk
= (div
& SDHCI_DIVIDER_MASK
) << SDHCI_DIVIDER_SHIFT
;
307 clk
|= ((div
>> SDHCI_DIVIDER_MASK_LEN
) & SDHCI_DIVIDER_HI_MASK
)
308 << SDHCI_DIVIDER_HI_SHIFT
;
310 WR2(slot
, SDHCI_CLOCK_CONTROL
, clk
);
312 clk
|= SDHCI_CLOCK_INT_EN
;
313 WR2(slot
, SDHCI_CLOCK_CONTROL
, clk
);
314 /* Wait up to 10 ms until it stabilize. */
316 while (!((clk
= RD2(slot
, SDHCI_CLOCK_CONTROL
))
317 & SDHCI_CLOCK_INT_STABLE
)) {
320 "Internal clock never stabilised.\n");
321 sdhci_dumpregs(slot
);
327 /* Pass clock signal to the bus. */
328 clk
|= SDHCI_CLOCK_CARD_EN
;
329 WR2(slot
, SDHCI_CLOCK_CONTROL
, clk
);
333 sdhci_set_power(struct sdhci_slot
*slot
, u_char power
)
337 if (slot
->power
== power
)
342 /* Turn off the power. */
344 WR1(slot
, SDHCI_POWER_CONTROL
, pwr
);
345 /* If power down requested - left it so. */
349 switch (1 << power
) {
350 case MMC_OCR_LOW_VOLTAGE
:
351 pwr
|= SDHCI_POWER_180
;
353 case MMC_OCR_290_300
:
354 case MMC_OCR_300_310
:
355 pwr
|= SDHCI_POWER_300
;
357 case MMC_OCR_320_330
:
358 case MMC_OCR_330_340
:
359 pwr
|= SDHCI_POWER_330
;
362 WR1(slot
, SDHCI_POWER_CONTROL
, pwr
);
363 /* Turn on the power. */
364 pwr
|= SDHCI_POWER_ON
;
365 WR1(slot
, SDHCI_POWER_CONTROL
, pwr
);
369 sdhci_read_block_pio(struct sdhci_slot
*slot
)
375 buffer
= slot
->curcmd
->data
->data
;
376 buffer
+= slot
->offset
;
377 /* Transfer one block at a time. */
378 left
= min(512, slot
->curcmd
->data
->len
- slot
->offset
);
379 slot
->offset
+= left
;
381 /* If we are too fast, broken controllers return zeroes. */
382 if (slot
->quirks
& SDHCI_QUIRK_BROKEN_TIMINGS
)
384 /* Handle unaligned and aligned buffer cases. */
385 if ((intptr_t)buffer
& 3) {
387 data
= RD4(slot
, SDHCI_BUFFER
);
389 buffer
[1] = (data
>> 8);
390 buffer
[2] = (data
>> 16);
391 buffer
[3] = (data
>> 24);
396 RD_MULTI_4(slot
, SDHCI_BUFFER
,
397 (uint32_t *)buffer
, left
>> 2);
400 /* Handle uneven size case. */
402 data
= RD4(slot
, SDHCI_BUFFER
);
412 sdhci_write_block_pio(struct sdhci_slot
*slot
)
418 buffer
= slot
->curcmd
->data
->data
;
419 buffer
+= slot
->offset
;
420 /* Transfer one block at a time. */
421 left
= min(512, slot
->curcmd
->data
->len
- slot
->offset
);
422 slot
->offset
+= left
;
424 /* Handle unaligned and aligned buffer cases. */
425 if ((intptr_t)buffer
& 3) {
433 WR4(slot
, SDHCI_BUFFER
, data
);
436 WR_MULTI_4(slot
, SDHCI_BUFFER
,
437 (uint32_t *)buffer
, left
>> 2);
440 /* Handle uneven size case. */
447 WR4(slot
, SDHCI_BUFFER
, data
);
452 sdhci_transfer_pio(struct sdhci_slot
*slot
)
455 /* Read as many blocks as possible. */
456 if (slot
->curcmd
->data
->flags
& MMC_DATA_READ
) {
457 while (RD4(slot
, SDHCI_PRESENT_STATE
) &
458 SDHCI_DATA_AVAILABLE
) {
459 sdhci_read_block_pio(slot
);
460 if (slot
->offset
>= slot
->curcmd
->data
->len
)
464 while (RD4(slot
, SDHCI_PRESENT_STATE
) &
465 SDHCI_SPACE_AVAILABLE
) {
466 sdhci_write_block_pio(slot
);
467 if (slot
->offset
>= slot
->curcmd
->data
->len
)
474 sdhci_card_delay(void *arg
)
476 struct sdhci_slot
*slot
= arg
;
478 taskqueue_enqueue(taskqueue_swi_mp
, &slot
->card_task
);
482 sdhci_card_task(void *arg
, int pending
)
484 struct sdhci_slot
*slot
= arg
;
487 if (SDHCI_GET_CARD_PRESENT(slot
->bus
, slot
)) {
488 if (slot
->dev
== NULL
) {
489 /* If card is present - attach mmc bus. */
490 slot
->dev
= device_add_child(slot
->bus
, "mmc", -1);
491 device_set_ivars(slot
->dev
, slot
);
493 device_probe_and_attach(slot
->dev
);
497 if (slot
->dev
!= NULL
) {
498 /* If no card present - detach mmc bus. */
499 device_t d
= slot
->dev
;
502 device_delete_child(slot
->bus
, d
);
509 sdhci_dma_alloc(struct sdhci_slot
*slot
)
513 /* Allocate DMA memory for SDMA. */
514 err
= bus_dmamem_coherent(bus_get_dma_tag(slot
->bus
),
515 DMA_BLOCK_SIZE
, 0, BUS_SPACE_MAXADDR_32BIT
,
516 BUS_SPACE_MAXADDR
, DMA_BLOCK_SIZE
, BUS_DMA_NOWAIT
,
519 device_printf(slot
->bus
, "Can't alloc DMA memory for SDMA\n");
523 /* Allocate DMA memory for 32bit ADMA2 descriptors. */
524 err
= bus_dmamem_coherent(bus_get_dma_tag(slot
->bus
),
525 4, 0, BUS_SPACE_MAXADDR_32BIT
,
526 BUS_SPACE_MAXADDR
, SDHCI_ADMA2_DESCBUF_SIZE
, BUS_DMA_NOWAIT
,
529 device_printf(slot
->bus
,
530 "Can't alloc DMA memory for ADMA2 descriptors\n");
534 /* Allocate DMA tag for 32bit ADMA2 data buffer */
535 err
= bus_dma_tag_create(bus_get_dma_tag(slot
->bus
),
536 4, 0, BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
537 MAXPHYS
, SDHCI_ADMA2_DESC_COUNT
,
538 MIN(MAXPHYS
, SDHCI_ADMA2_MAX_SEGSIZE
),
539 BUS_DMA_ALLOCNOW
| BUS_DMA_ALLOCALL
,
542 device_printf(slot
->bus
, "Can't create DMA tag for ADMA2\n");
546 /* Allocate DMA map for ADMA2 data buffer */
547 err
= bus_dmamap_create(slot
->adma2_tag
, BUS_DMA_NOWAIT
,
550 device_printf(slot
->bus
, "Can't create DMA map for ADMA2\n");
557 bus_dma_tag_destroy(slot
->adma2_tag
);
559 sdhci_dmamem_free(&slot
->adma2_descs
);
561 sdhci_dmamem_free(&slot
->sdma_mem
);
567 sdhci_dmamem_free(bus_dmamem_t
*dma
)
569 bus_dmamap_unload(dma
->dmem_tag
, dma
->dmem_map
);
570 bus_dmamem_free(dma
->dmem_tag
, dma
->dmem_addr
, dma
->dmem_map
);
571 bus_dma_tag_destroy(dma
->dmem_tag
);
575 sdhci_dma_free(struct sdhci_slot
*slot
)
577 bus_dmamap_destroy(slot
->adma2_tag
, slot
->adma2_map
);
578 bus_dma_tag_destroy(slot
->adma2_tag
);
579 sdhci_dmamem_free(&slot
->sdma_mem
);
580 sdhci_dmamem_free(&slot
->adma2_descs
);
584 sdhci_adma2_getaddr(void *arg
, bus_dma_segment_t
*segs
, int nsegs
, int error
)
586 struct sdhci_slot
*slot
= arg
;
587 bus_dmamem_t
*descmem
= &slot
->adma2_descs
;
588 struct sdhci_adma2_desc32
*descs
= (void *)descmem
->dmem_addr
;
592 /* This signals, that loading was unsuccessful */
593 memset(&descs
[0], 0, sizeof(*descs
));
597 for (i
= 0; i
< nsegs
; i
++) {
598 descs
[i
].address
= segs
[i
].ds_addr
;
600 * The 65536 segment length case is broken in some sdhc host
601 * controllers, so we actually use a maximum segment length
602 * of 32768 for the DMA mapping and ds_len should be at most
605 if (segs
[i
].ds_len
== 65536)
608 descs
[i
].length
= segs
[i
].ds_len
;
610 SDHCI_ADMA2_ATTR_VALID
| SDHCI_ADMA2_ATTR_OP_TRAN
;
612 descs
[nsegs
-1].attribute
|= SDHCI_ADMA2_ATTR_END
;
613 /* If there is room left, explicitly add an invalid descriptor. */
614 if (nsegs
< SDHCI_ADMA2_DESC_COUNT
)
615 memset(&descs
[nsegs
], 0, sizeof(*descs
));
619 sdhci_init_slot(device_t dev
, struct sdhci_slot
*slot
, int num
)
624 SDHCI_LOCK_INIT(slot
);
628 err
= sdhci_dma_alloc(slot
);
630 SDHCI_LOCK_DESTROY(slot
);
634 /* Initialize slot. */
636 slot
->version
= (RD2(slot
, SDHCI_HOST_VERSION
)
637 >> SDHCI_SPEC_VER_SHIFT
) & SDHCI_SPEC_VER_MASK
;
638 if (slot
->quirks
& SDHCI_QUIRK_MISSING_CAPS
)
641 caps
= RD4(slot
, SDHCI_CAPABILITIES
);
642 /* Calculate base clock frequency. */
643 if (slot
->version
>= SDHCI_SPEC_300
)
644 freq
= (caps
& SDHCI_CLOCK_V3_BASE_MASK
) >>
645 SDHCI_CLOCK_BASE_SHIFT
;
647 freq
= (caps
& SDHCI_CLOCK_BASE_MASK
) >>
648 SDHCI_CLOCK_BASE_SHIFT
;
650 slot
->max_clk
= freq
* 1000000;
652 * If the frequency wasn't in the capabilities and the hardware driver
653 * hasn't already set max_clk we're probably not going to work right
654 * with an assumption, so complain about it.
656 if (slot
->max_clk
== 0) {
657 slot
->max_clk
= SDHCI_DEFAULT_MAX_FREQ
* 1000000;
658 device_printf(dev
, "Hardware doesn't specify base clock "
659 "frequency, using %dMHz as default.\n", SDHCI_DEFAULT_MAX_FREQ
);
661 /* Calculate timeout clock frequency. */
662 if (slot
->quirks
& SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
) {
663 slot
->timeout_clk
= slot
->max_clk
/ 1000;
666 (caps
& SDHCI_TIMEOUT_CLK_MASK
) >> SDHCI_TIMEOUT_CLK_SHIFT
;
667 if (caps
& SDHCI_TIMEOUT_CLK_UNIT
)
668 slot
->timeout_clk
*= 1000;
671 * If the frequency wasn't in the capabilities and the hardware driver
672 * hasn't already set timeout_clk we'll probably work okay using the
673 * max timeout, but still mention it.
675 if (slot
->timeout_clk
== 0) {
676 device_printf(dev
, "Hardware doesn't specify timeout clock "
677 "frequency, setting BROKEN_TIMEOUT quirk.\n");
678 slot
->quirks
|= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
;
681 slot
->host
.f_min
= SDHCI_MIN_FREQ(slot
->bus
, slot
);
682 slot
->host
.f_max
= slot
->max_clk
;
683 slot
->host
.host_ocr
= 0;
684 if (caps
& SDHCI_CAN_VDD_330
)
685 slot
->host
.host_ocr
|= MMC_OCR_320_330
| MMC_OCR_330_340
;
686 if (caps
& SDHCI_CAN_VDD_300
)
687 slot
->host
.host_ocr
|= MMC_OCR_290_300
| MMC_OCR_300_310
;
688 if (caps
& SDHCI_CAN_VDD_180
)
689 slot
->host
.host_ocr
|= MMC_OCR_LOW_VOLTAGE
;
690 if (slot
->host
.host_ocr
== 0) {
691 device_printf(dev
, "Hardware doesn't report any "
692 "support voltages.\n");
694 slot
->host
.caps
= MMC_CAP_4_BIT_DATA
;
695 if (caps
& SDHCI_CAN_DO_8BITBUS
)
696 slot
->host
.caps
|= MMC_CAP_8_BIT_DATA
;
697 if (caps
& SDHCI_CAN_DO_HISPD
)
698 slot
->host
.caps
|= MMC_CAP_HSPEED
;
699 /* Decide if we have usable DMA. */
700 if (caps
& SDHCI_CAN_DO_DMA
)
701 slot
->opt
|= SDHCI_HAVE_SDMA
;
702 if (caps
& SDHCI_CAN_DO_ADMA2
)
703 slot
->opt
|= SDHCI_HAVE_ADMA2
;
705 if (slot
->quirks
& SDHCI_QUIRK_BROKEN_DMA
) {
706 slot
->opt
&= ~SDHCI_HAVE_SDMA
;
707 slot
->opt
&= ~SDHCI_HAVE_ADMA2
;
709 if (slot
->quirks
& SDHCI_QUIRK_FORCE_SDMA
)
710 slot
->opt
|= SDHCI_HAVE_SDMA
;
713 * Use platform-provided transfer backend
714 * with PIO as a fallback mechanism
716 if (slot
->opt
& SDHCI_PLATFORM_TRANSFER
) {
717 slot
->opt
&= ~SDHCI_HAVE_SDMA
;
718 slot
->opt
&= ~SDHCI_HAVE_ADMA2
;
721 if (bootverbose
|| sdhci_debug
) {
722 slot_printf(slot
, "%uMHz%s %s%s%s%s %s\n",
723 slot
->max_clk
/ 1000000,
724 (caps
& SDHCI_CAN_DO_HISPD
) ? " HS" : "",
725 (slot
->host
.caps
& MMC_CAP_8_BIT_DATA
) ? "8bits" :
726 ((slot
->host
.caps
& MMC_CAP_4_BIT_DATA
) ? "4bits" :
728 (caps
& SDHCI_CAN_VDD_330
) ? " 3.3V" : "",
729 (caps
& SDHCI_CAN_VDD_300
) ? " 3.0V" : "",
730 (caps
& SDHCI_CAN_VDD_180
) ? " 1.8V" : "",
731 (slot
->opt
& SDHCI_HAVE_ADMA2
) ? "ADMA2" :
732 (slot
->opt
& SDHCI_HAVE_SDMA
) ? "SDMA" : "PIO");
733 sdhci_dumpregs(slot
);
738 SYSCTL_ADD_INT(device_get_sysctl_ctx(slot
->bus
),
739 SYSCTL_CHILDREN(device_get_sysctl_tree(slot
->bus
)), OID_AUTO
,
740 "timeout", CTLFLAG_RW
, &slot
->timeout
, 0,
741 "Maximum timeout for SDHCI transfers (in secs)");
742 TASK_INIT(&slot
->card_task
, 0, sdhci_card_task
, slot
);
743 callout_init_mp(&slot
->card_callout
);
744 callout_init_lk(&slot
->timeout_callout
, &slot
->lock
);
749 sdhci_start_slot(struct sdhci_slot
*slot
)
751 sdhci_card_task(slot
, 0);
755 sdhci_cleanup_slot(struct sdhci_slot
*slot
)
759 callout_drain(&slot
->timeout_callout
);
760 callout_drain(&slot
->card_callout
);
761 taskqueue_drain(taskqueue_swi_mp
, &slot
->card_task
);
768 device_delete_child(slot
->bus
, d
);
771 sdhci_reset(slot
, SDHCI_RESET_ALL
);
774 sdhci_dma_free(slot
);
776 SDHCI_LOCK_DESTROY(slot
);
782 sdhci_generic_suspend(struct sdhci_slot
*slot
)
784 sdhci_reset(slot
, SDHCI_RESET_ALL
);
790 sdhci_generic_resume(struct sdhci_slot
*slot
)
798 sdhci_generic_min_freq(device_t brdev
, struct sdhci_slot
*slot
)
800 if (slot
->version
>= SDHCI_SPEC_300
)
801 return (slot
->max_clk
/ SDHCI_300_MAX_DIVIDER
);
803 return (slot
->max_clk
/ SDHCI_200_MAX_DIVIDER
);
807 sdhci_generic_get_card_present(device_t brdev
, struct sdhci_slot
*slot
)
810 return (RD4(slot
, SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
);
814 sdhci_generic_update_ios(device_t brdev
, device_t reqdev
)
816 struct sdhci_slot
*slot
= device_get_ivars(reqdev
);
817 struct mmc_ios
*ios
= &slot
->host
.ios
;
820 /* Do full reset on bus power down to clear from any state. */
821 if (ios
->power_mode
== power_off
) {
822 WR4(slot
, SDHCI_SIGNAL_ENABLE
, 0);
825 /* Configure the bus. */
826 sdhci_set_clock(slot
, ios
->clock
);
827 sdhci_set_power(slot
, (ios
->power_mode
== power_off
) ? 0 : ios
->vdd
);
828 if (ios
->bus_width
== bus_width_8
) {
829 slot
->hostctrl
|= SDHCI_CTRL_8BITBUS
;
830 slot
->hostctrl
&= ~SDHCI_CTRL_4BITBUS
;
831 } else if (ios
->bus_width
== bus_width_4
) {
832 slot
->hostctrl
&= ~SDHCI_CTRL_8BITBUS
;
833 slot
->hostctrl
|= SDHCI_CTRL_4BITBUS
;
834 } else if (ios
->bus_width
== bus_width_1
) {
835 slot
->hostctrl
&= ~SDHCI_CTRL_8BITBUS
;
836 slot
->hostctrl
&= ~SDHCI_CTRL_4BITBUS
;
838 panic("Invalid bus width: %d", ios
->bus_width
);
840 if (ios
->timing
== bus_timing_hs
&&
841 !(slot
->quirks
& SDHCI_QUIRK_DONT_SET_HISPD_BIT
))
842 slot
->hostctrl
|= SDHCI_CTRL_HISPD
;
844 slot
->hostctrl
&= ~SDHCI_CTRL_HISPD
;
845 WR1(slot
, SDHCI_HOST_CONTROL
, slot
->hostctrl
);
846 /* Some controllers like reset after bus changes. */
847 if(slot
->quirks
& SDHCI_QUIRK_RESET_ON_IOS
)
848 sdhci_reset(slot
, SDHCI_RESET_CMD
| SDHCI_RESET_DATA
);
855 sdhci_req_done(struct sdhci_slot
*slot
)
857 struct mmc_request
*req
;
859 if (slot
->req
!= NULL
&& slot
->curcmd
!= NULL
) {
860 callout_stop(&slot
->timeout_callout
);
861 if (slot
->curcmd
->error
!= MMC_ERR_TIMEOUT
)
871 sdhci_timeout(void *arg
)
873 struct sdhci_slot
*slot
= arg
;
875 if (slot
->curcmd
!= NULL
) {
876 slot_printf(slot
, " Controller timeout\n");
877 sdhci_dumpregs(slot
);
878 sdhci_reset(slot
, SDHCI_RESET_CMD
|SDHCI_RESET_DATA
);
879 slot
->curcmd
->error
= MMC_ERR_TIMEOUT
;
880 sdhci_req_done(slot
);
882 slot_printf(slot
, " Spurious timeout - no active command\n");
887 sdhci_set_transfer_mode(struct sdhci_slot
*slot
,
888 struct mmc_data
*data
)
895 mode
= SDHCI_TRNS_BLK_CNT_EN
;
897 mode
|= SDHCI_TRNS_MULTI
;
898 if (data
->flags
& MMC_DATA_READ
)
899 mode
|= SDHCI_TRNS_READ
;
901 mode
|= SDHCI_TRNS_ACMD12
;
902 if (slot
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA2
))
903 mode
|= SDHCI_TRNS_DMA
;
905 WR2(slot
, SDHCI_TRANSFER_MODE
, mode
);
909 sdhci_start_command(struct sdhci_slot
*slot
, struct mmc_command
*cmd
)
917 cmd
->error
= MMC_ERR_NONE
;
919 /* This flags combination is not supported by controller. */
920 if ((cmd
->flags
& MMC_RSP_136
) && (cmd
->flags
& MMC_RSP_BUSY
)) {
921 slot_printf(slot
, "Unsupported response type!\n");
922 cmd
->error
= MMC_ERR_FAILED
;
923 sdhci_req_done(slot
);
927 /* Do not issue command if there is no card, clock or power.
928 * Controller will not detect timeout without clock active. */
929 if (!SDHCI_GET_CARD_PRESENT(slot
->bus
, slot
) ||
932 cmd
->error
= MMC_ERR_FAILED
;
933 sdhci_req_done(slot
);
936 /* Always wait for free CMD bus. */
937 mask
= SDHCI_CMD_INHIBIT
;
938 /* Wait for free DAT if we have data or busy signal. */
939 if (cmd
->data
|| (cmd
->flags
& MMC_RSP_BUSY
))
940 mask
|= SDHCI_DAT_INHIBIT
;
941 /* We shouldn't wait for DAT for stop commands. */
942 if (cmd
== slot
->req
->stop
)
943 mask
&= ~SDHCI_DAT_INHIBIT
;
945 * Wait for bus no more then 250 ms. Typically there will be no wait
946 * here at all, but when writing a crash dump we may be bypassing the
947 * host platform's interrupt handler, and in some cases that handler
948 * may be working around hardware quirks such as not respecting r1b
949 * busy indications. In those cases, this wait-loop serves the purpose
950 * of waiting for the prior command and data transfers to be done, and
951 * SD cards are allowed to take up to 250ms for write and erase ops.
952 * (It's usually more like 20-30ms in the real world.)
955 while (mask
& RD4(slot
, SDHCI_PRESENT_STATE
)) {
957 slot_printf(slot
, "Controller never released "
958 "inhibit bit(s).\n");
959 sdhci_dumpregs(slot
);
960 cmd
->error
= MMC_ERR_FAILED
;
961 sdhci_req_done(slot
);
968 /* Prepare command flags. */
969 if (!(cmd
->flags
& MMC_RSP_PRESENT
))
970 flags
= SDHCI_CMD_RESP_NONE
;
971 else if (cmd
->flags
& MMC_RSP_136
)
972 flags
= SDHCI_CMD_RESP_LONG
;
973 else if (cmd
->flags
& MMC_RSP_BUSY
)
974 flags
= SDHCI_CMD_RESP_SHORT_BUSY
;
976 flags
= SDHCI_CMD_RESP_SHORT
;
977 if (cmd
->flags
& MMC_RSP_CRC
)
978 flags
|= SDHCI_CMD_CRC
;
979 if (cmd
->flags
& MMC_RSP_OPCODE
)
980 flags
|= SDHCI_CMD_INDEX
;
982 flags
|= SDHCI_CMD_DATA
;
983 if (cmd
->opcode
== MMC_STOP_TRANSMISSION
)
984 flags
|= SDHCI_CMD_TYPE_ABORT
;
986 sdhci_start_data(slot
, cmd
->data
);
988 * Interrupt aggregation: To reduce total number of interrupts
989 * group response interrupt with data interrupt when possible.
990 * If there going to be data interrupt, mask response one.
992 if (slot
->data_done
== 0) {
993 WR4(slot
, SDHCI_SIGNAL_ENABLE
,
994 slot
->intmask
&= ~SDHCI_INT_RESPONSE
);
996 /* Set command argument. */
997 WR4(slot
, SDHCI_ARGUMENT
, cmd
->arg
);
998 /* Set data transfer mode. */
999 sdhci_set_transfer_mode(slot
, cmd
->data
);
1000 /* Start command. */
1001 WR2(slot
, SDHCI_COMMAND_FLAGS
, (cmd
->opcode
<< 8) | (flags
& 0xff));
1004 * Start timeout callout. Timeout is dropped to 2 seconds with
1005 * repeated controller timeouts.
1008 timeout
= slot
->timeout
/ 5;
1010 timeout
= slot
->timeout
;
1013 callout_reset(&slot
->timeout_callout
, timeout
* hz
,
1014 sdhci_timeout
, slot
);
1018 sdhci_finish_command(struct sdhci_slot
*slot
)
1023 /* Interrupt aggregation: Restore command interrupt.
1024 * Main restore point for the case when command interrupt
1025 * happened first. */
1026 WR4(slot
, SDHCI_SIGNAL_ENABLE
, slot
->intmask
|= SDHCI_INT_RESPONSE
);
1027 /* In case of error - reset host and return. */
1028 if (slot
->curcmd
->error
) {
1029 sdhci_reset(slot
, SDHCI_RESET_CMD
);
1030 sdhci_reset(slot
, SDHCI_RESET_DATA
);
1034 /* If command has response - fetch it. */
1035 if (slot
->curcmd
->flags
& MMC_RSP_PRESENT
) {
1036 if (slot
->curcmd
->flags
& MMC_RSP_136
) {
1037 /* CRC is stripped so we need one byte shift. */
1039 for (i
= 0; i
< 4; i
++) {
1040 uint32_t val
= RD4(slot
, SDHCI_RESPONSE
+ i
* 4);
1041 if (slot
->quirks
& SDHCI_QUIRK_DONT_SHIFT_RESPONSE
) {
1042 slot
->curcmd
->resp
[3 - i
] = val
;
1044 slot
->curcmd
->resp
[3 - i
] =
1050 slot
->curcmd
->resp
[0] = RD4(slot
, SDHCI_RESPONSE
);
1053 /* If data ready - finish. */
1054 if (slot
->data_done
)
1059 sdhci_start_data(struct sdhci_slot
*slot
, struct mmc_data
*data
)
1061 uint32_t target_timeout
, current_timeout
;
1064 if (data
== NULL
&& (slot
->curcmd
->flags
& MMC_RSP_BUSY
) == 0) {
1065 slot
->data_done
= 1;
1069 slot
->data_done
= 0;
1071 /* Calculate and set data timeout.*/
1072 /* XXX: We should have this from mmc layer, now assume 1 sec. */
1073 if (slot
->quirks
& SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
) {
1076 target_timeout
= 1000000;
1078 current_timeout
= (1 << 13) * 1000 / slot
->timeout_clk
;
1079 while (current_timeout
< target_timeout
&& div
< 0xE) {
1081 current_timeout
<<= 1;
1083 /* Compensate for an off-by-one error in the CaFe chip.*/
1085 (slot
->quirks
& SDHCI_QUIRK_INCR_TIMEOUT_CONTROL
)) {
1089 WR1(slot
, SDHCI_TIMEOUT_CONTROL
, div
);
1094 /* Use DMA if possible. Prefer ADMA2 over SDMA. */
1095 if ((slot
->opt
& SDHCI_HAVE_ADMA2
)) {
1096 slot
->flags
|= SDHCI_USE_ADMA2
;
1097 slot
->flags
&= ~SDHCI_USE_SDMA
;
1098 } else if ((slot
->opt
& SDHCI_HAVE_SDMA
)) {
1099 slot
->flags
|= SDHCI_USE_SDMA
;
1100 slot
->flags
&= ~SDHCI_USE_ADMA2
;
1102 /* If data is small, broken DMA may return zeroes instead of data. */
1103 if ((slot
->quirks
& SDHCI_QUIRK_BROKEN_TIMINGS
) &&
1104 (data
->len
<= 512)) {
1105 slot
->flags
&= ~SDHCI_USE_SDMA
;
1106 slot
->flags
&= ~SDHCI_USE_ADMA2
;
1108 /* Some controllers require even block sizes. */
1109 if ((slot
->quirks
& SDHCI_QUIRK_32BIT_DMA_SIZE
) &&
1110 ((data
->len
) & 0x3)) {
1111 slot
->flags
&= ~SDHCI_USE_SDMA
;
1112 slot
->flags
&= ~SDHCI_USE_ADMA2
;
1114 /* Load DMA buffer. */
1115 if (slot
->flags
& SDHCI_USE_ADMA2
) {
1116 bus_dmamem_t
*descmem
= &slot
->adma2_descs
;
1117 struct sdhci_adma2_desc32
*descs
= (void *)descmem
->dmem_addr
;
1120 /* It shouldn't really be possible for this to fail */
1121 err
= bus_dmamap_load(slot
->adma2_tag
, slot
->adma2_map
,
1122 data
->data
, data
->len
, sdhci_adma2_getaddr
, slot
,
1123 dumping
? BUS_DMA_NOWAIT
: BUS_DMA_WAITOK
);
1125 device_printf(slot
->bus
,
1126 "Dma load for ADMA2 fail: %d\n", err
);
1127 } else if (descs
[0].address
== 0) {
1128 device_printf(slot
->bus
,
1129 "Dma load for ADMA2 fail, segment constraints\n");
1131 if (err
!= 0 || descs
[0].address
== 0) {
1132 /* fallback to PIO for this request */
1133 slot
->flags
&= ~SDHCI_USE_ADMA2
;
1136 /* sync dma descriptors */
1137 bus_dmamap_sync(descmem
->dmem_tag
, descmem
->dmem_map
,
1138 BUS_DMASYNC_PREWRITE
);
1139 /* sync data buffers */
1140 if (data
->flags
& MMC_DATA_READ
) {
1141 bus_dmamap_sync(slot
->adma2_tag
, slot
->adma2_map
,
1142 BUS_DMASYNC_PREREAD
);
1144 bus_dmamap_sync(slot
->adma2_tag
, slot
->adma2_map
,
1145 BUS_DMASYNC_PREWRITE
);
1147 WR4(slot
, SDHCI_ADMA_ADDRESS_LOW
, descmem
->dmem_busaddr
);
1148 if ((slot
->hostctrl
& SDHCI_CTRL_DMA_MASK
) !=
1150 slot
->hostctrl
&= ~SDHCI_CTRL_DMA_MASK
;
1151 slot
->hostctrl
|= SDHCI_CTRL_ADMA2
;
1152 WR1(slot
, SDHCI_HOST_CONTROL
, slot
->hostctrl
);
1154 /* We don't expect any DMA_END interrupts with ADMA2 */
1155 WR4(slot
, SDHCI_SIGNAL_ENABLE
,
1156 slot
->intmask
&= ~SDHCI_INT_DMA_END
);
1157 } else if (slot
->flags
& SDHCI_USE_SDMA
) {
1158 bus_dmamem_t
*sdma
= &slot
->sdma_mem
;
1160 if (data
->flags
& MMC_DATA_READ
) {
1161 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1162 BUS_DMASYNC_PREREAD
);
1164 memcpy(sdma
->dmem_addr
, data
->data
,
1165 (data
->len
< DMA_BLOCK_SIZE
) ?
1166 data
->len
: DMA_BLOCK_SIZE
);
1167 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1168 BUS_DMASYNC_PREWRITE
);
1170 WR4(slot
, SDHCI_SDMA_ADDRESS
, sdma
->dmem_busaddr
);
1171 if ((slot
->hostctrl
& SDHCI_CTRL_DMA_MASK
) !=
1173 slot
->hostctrl
&= ~SDHCI_CTRL_DMA_MASK
;
1174 slot
->hostctrl
|= SDHCI_CTRL_SDMA
;
1175 WR1(slot
, SDHCI_HOST_CONTROL
, slot
->hostctrl
);
1177 /* Interrupt aggregation: Mask border interrupt
1178 * for the last page and unmask else. */
1179 if (data
->len
== DMA_BLOCK_SIZE
)
1180 slot
->intmask
&= ~SDHCI_INT_DMA_END
;
1182 slot
->intmask
|= SDHCI_INT_DMA_END
;
1183 WR4(slot
, SDHCI_SIGNAL_ENABLE
, slot
->intmask
);
1186 /* Current data offset for both PIO and SDMA. */
1188 /* Set block size and for SDMA request IRQ on 4K border. */
1189 WR2(slot
, SDHCI_BLOCK_SIZE
,
1190 SDHCI_MAKE_BLKSZ(DMA_BOUNDARY
, (data
->len
< 512)?data
->len
:512));
1191 /* Set block count. */
1192 WR2(slot
, SDHCI_BLOCK_COUNT
, (data
->len
+ 511) / 512);
1196 sdhci_finish_data(struct sdhci_slot
*slot
)
1198 struct mmc_data
*data
= slot
->curcmd
->data
;
1200 /* Interrupt aggregation: Restore command interrupt.
1201 * Auxiliary restore point for the case when data interrupt
1202 * happened first. */
1203 if (!slot
->cmd_done
) {
1204 WR4(slot
, SDHCI_SIGNAL_ENABLE
,
1205 slot
->intmask
|= SDHCI_INT_RESPONSE
);
1207 /* Unload rest of data from DMA buffer. */
1208 if (!slot
->data_done
&& (slot
->flags
& SDHCI_USE_ADMA2
)) {
1209 bus_dmamem_t
*descmem
= &slot
->adma2_descs
;
1211 bus_dmamap_sync(descmem
->dmem_tag
, descmem
->dmem_map
,
1212 BUS_DMASYNC_POSTWRITE
);
1213 if (data
->flags
& MMC_DATA_READ
) {
1214 bus_dmamap_sync(slot
->adma2_tag
, slot
->adma2_map
,
1215 BUS_DMASYNC_POSTREAD
);
1217 bus_dmamap_sync(slot
->adma2_tag
, slot
->adma2_map
,
1218 BUS_DMASYNC_POSTWRITE
);
1220 bus_dmamap_unload(slot
->adma2_tag
, slot
->adma2_map
);
1221 } else if (!slot
->data_done
&& (slot
->flags
& SDHCI_USE_SDMA
)) {
1222 bus_dmamem_t
*sdma
= &slot
->sdma_mem
;
1224 if (data
->flags
& MMC_DATA_READ
) {
1225 size_t left
= data
->len
- slot
->offset
;
1226 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1227 BUS_DMASYNC_POSTREAD
);
1228 memcpy((u_char
*)data
->data
+ slot
->offset
,
1230 (left
< DMA_BLOCK_SIZE
)?left
:DMA_BLOCK_SIZE
);
1232 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1233 BUS_DMASYNC_POSTWRITE
);
1236 slot
->data_done
= 1;
1237 /* If there was an error - reset the host. */
1238 if (slot
->curcmd
->error
) {
1239 sdhci_reset(slot
, SDHCI_RESET_CMD
);
1240 sdhci_reset(slot
, SDHCI_RESET_DATA
);
1244 /* If we already have command response - finish. */
1250 sdhci_start(struct sdhci_slot
*slot
)
1252 struct mmc_request
*req
;
1258 if (!(slot
->flags
& CMD_STARTED
)) {
1259 slot
->flags
|= CMD_STARTED
;
1260 sdhci_start_command(slot
, req
->cmd
);
1263 /* We don't need this until using Auto-CMD12 feature
1264 if (!(slot->flags & STOP_STARTED) && req->stop) {
1265 slot->flags |= STOP_STARTED;
1266 sdhci_start_command(slot, req->stop);
1270 if (sdhci_debug
> 1)
1271 slot_printf(slot
, "result: %d\n", req
->cmd
->error
);
1272 if (!req
->cmd
->error
&&
1273 (slot
->quirks
& SDHCI_QUIRK_RESET_AFTER_REQUEST
)) {
1274 sdhci_reset(slot
, SDHCI_RESET_CMD
);
1275 sdhci_reset(slot
, SDHCI_RESET_DATA
);
1278 sdhci_req_done(slot
);
1282 sdhci_generic_request(device_t brdev
, device_t reqdev
, struct mmc_request
*req
)
1284 struct sdhci_slot
*slot
= device_get_ivars(reqdev
);
1287 if (slot
->req
!= NULL
) {
1291 if (sdhci_debug
> 1) {
1292 slot_printf(slot
, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1293 req
->cmd
->opcode
, req
->cmd
->arg
, req
->cmd
->flags
,
1294 (req
->cmd
->data
)?(u_int
)req
->cmd
->data
->len
:0,
1295 (req
->cmd
->data
)?req
->cmd
->data
->flags
:0);
1302 while (slot
->req
!= NULL
) {
1303 sdhci_generic_intr(slot
);
1311 sdhci_generic_get_ro(device_t brdev
, device_t reqdev
)
1313 struct sdhci_slot
*slot
= device_get_ivars(reqdev
);
1317 val
= RD4(slot
, SDHCI_PRESENT_STATE
);
1319 return (!(val
& SDHCI_WRITE_PROTECT
));
1323 sdhci_generic_acquire_host(device_t brdev
, device_t reqdev
)
1325 struct sdhci_slot
*slot
= device_get_ivars(reqdev
);
1329 while (slot
->bus_busy
)
1330 lksleep(slot
, &slot
->lock
, 0, "sdhciah", 0);
1333 WR1(slot
, SDHCI_HOST_CONTROL
, slot
->hostctrl
|= SDHCI_CTRL_LED
);
1339 sdhci_generic_release_host(device_t brdev
, device_t reqdev
)
1341 struct sdhci_slot
*slot
= device_get_ivars(reqdev
);
1344 /* Deactivate led. */
1345 WR1(slot
, SDHCI_HOST_CONTROL
, slot
->hostctrl
&= ~SDHCI_CTRL_LED
);
1353 sdhci_cmd_irq(struct sdhci_slot
*slot
, uint32_t intmask
)
1356 if (!slot
->curcmd
) {
1357 slot_printf(slot
, "Got command interrupt 0x%08x, but "
1358 "there is no active command.\n", intmask
);
1359 sdhci_dumpregs(slot
);
1362 if (intmask
& SDHCI_INT_TIMEOUT
)
1363 slot
->curcmd
->error
= MMC_ERR_TIMEOUT
;
1364 else if (intmask
& SDHCI_INT_CRC
)
1365 slot
->curcmd
->error
= MMC_ERR_BADCRC
;
1366 else if (intmask
& (SDHCI_INT_END_BIT
| SDHCI_INT_INDEX
))
1367 slot
->curcmd
->error
= MMC_ERR_FIFO
;
1369 sdhci_finish_command(slot
);
1373 sdhci_data_irq(struct sdhci_slot
*slot
, uint32_t intmask
)
1376 if (!slot
->curcmd
) {
1377 slot_printf(slot
, "Got data interrupt 0x%08x, but "
1378 "there is no active command.\n", intmask
);
1379 sdhci_dumpregs(slot
);
1382 if (slot
->curcmd
->data
== NULL
&&
1383 (slot
->curcmd
->flags
& MMC_RSP_BUSY
) == 0) {
1384 slot_printf(slot
, "Got data interrupt 0x%08x, but "
1385 "there is no active data operation.\n",
1387 sdhci_dumpregs(slot
);
1390 if (intmask
& SDHCI_INT_DATA_TIMEOUT
)
1391 slot
->curcmd
->error
= MMC_ERR_TIMEOUT
;
1392 else if (intmask
& (SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_END_BIT
))
1393 slot
->curcmd
->error
= MMC_ERR_BADCRC
;
1394 if (slot
->curcmd
->data
== NULL
&&
1395 (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
|
1396 SDHCI_INT_DMA_END
))) {
1397 slot_printf(slot
, "Got data interrupt 0x%08x, but "
1398 "there is busy-only command.\n", intmask
);
1399 sdhci_dumpregs(slot
);
1400 slot
->curcmd
->error
= MMC_ERR_INVALID
;
1402 if (slot
->curcmd
->error
) {
1403 /* No need to continue after any error. */
1407 /* Handle PIO interrupt. */
1408 if (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
)) {
1409 if ((slot
->opt
& SDHCI_PLATFORM_TRANSFER
) &&
1410 SDHCI_PLATFORM_WILL_HANDLE(slot
->bus
, slot
)) {
1411 SDHCI_PLATFORM_START_TRANSFER(slot
->bus
, slot
, &intmask
);
1412 slot
->flags
|= PLATFORM_DATA_STARTED
;
1414 sdhci_transfer_pio(slot
);
1416 /* Handle DMA border. */
1417 if (intmask
& SDHCI_INT_DMA_END
) {
1418 struct mmc_data
*data
= slot
->curcmd
->data
;
1419 bus_dmamem_t
*sdma
= &slot
->sdma_mem
;
1422 /* Unload DMA buffer... */
1423 left
= data
->len
- slot
->offset
;
1424 if (data
->flags
& MMC_DATA_READ
) {
1425 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1426 BUS_DMASYNC_POSTREAD
);
1427 memcpy((u_char
*)data
->data
+ slot
->offset
,
1429 (left
< DMA_BLOCK_SIZE
)?left
:DMA_BLOCK_SIZE
);
1431 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1432 BUS_DMASYNC_POSTWRITE
);
1434 /* ... and reload it again. */
1435 slot
->offset
+= DMA_BLOCK_SIZE
;
1436 left
= data
->len
- slot
->offset
;
1437 if (data
->flags
& MMC_DATA_READ
) {
1438 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1439 BUS_DMASYNC_PREREAD
);
1441 memcpy(sdma
->dmem_addr
,
1442 (u_char
*)data
->data
+ slot
->offset
,
1443 (left
< DMA_BLOCK_SIZE
)?left
:DMA_BLOCK_SIZE
);
1444 bus_dmamap_sync(sdma
->dmem_tag
, sdma
->dmem_map
,
1445 BUS_DMASYNC_PREWRITE
);
1447 /* Interrupt aggregation: Mask border interrupt
1448 * for the last page. */
1449 if (left
== DMA_BLOCK_SIZE
) {
1450 slot
->intmask
&= ~SDHCI_INT_DMA_END
;
1451 WR4(slot
, SDHCI_SIGNAL_ENABLE
, slot
->intmask
);
1454 WR4(slot
, SDHCI_SDMA_ADDRESS
, sdma
->dmem_busaddr
);
1456 /* We have got all data. */
1457 if (intmask
& SDHCI_INT_DATA_END
) {
1458 if (slot
->flags
& PLATFORM_DATA_STARTED
) {
1459 slot
->flags
&= ~PLATFORM_DATA_STARTED
;
1460 SDHCI_PLATFORM_FINISH_TRANSFER(slot
->bus
, slot
);
1462 sdhci_finish_data(slot
);
1466 if (slot
->curcmd
!= NULL
&& slot
->curcmd
->error
!= 0) {
1467 if (slot
->flags
& PLATFORM_DATA_STARTED
) {
1468 slot
->flags
&= ~PLATFORM_DATA_STARTED
;
1469 SDHCI_PLATFORM_FINISH_TRANSFER(slot
->bus
, slot
);
1471 sdhci_finish_data(slot
);
1478 sdhci_acmd_irq(struct sdhci_slot
*slot
)
1482 err
= RD4(slot
, SDHCI_ACMD12_ERR
);
1483 if (!slot
->curcmd
) {
1484 slot_printf(slot
, "Got AutoCMD12 error 0x%04x, but "
1485 "there is no active command.\n", err
);
1486 sdhci_dumpregs(slot
);
1489 slot_printf(slot
, "Got AutoCMD12 error 0x%04x\n", err
);
1490 sdhci_reset(slot
, SDHCI_RESET_CMD
);
1494 sdhci_adma_irq(struct sdhci_slot
*slot
)
1496 bus_dmamem_t
*descmem
= &slot
->adma2_descs
;
1497 struct sdhci_adma2_desc32
*desc
;
1498 bus_addr_t addr
= 0;
1499 uint8_t err
, adma_state
;
1501 err
= RD1(slot
, SDHCI_ADMA_ERR
);
1502 if (slot
->curcmd
&& (slot
->flags
& SDHCI_USE_ADMA2
)) {
1503 slot_printf(slot
, "Got ADMA2 error 0x%02x\n", err
);
1505 slot_printf(slot
, "Got ADMA2 error 0x%02x, but "
1506 "there is no active command.\n", err
);
1507 sdhci_dumpregs(slot
);
1510 /* Try to print the erronous ADMA2 descriptor */
1511 adma_state
= err
& SDHCI_ADMA_ERR_STATE_MASK
;
1512 if (adma_state
== SDHCI_ADMA_ERR_STATE_STOP
) {
1513 addr
= RD4(slot
, SDHCI_ADMA_ADDRESS_LOW
);
1514 if (addr
> sizeof(*desc
))
1515 addr
-= sizeof(*desc
);
1518 } else if (adma_state
== SDHCI_ADMA_ERR_STATE_FDS
) {
1519 addr
= RD4(slot
, SDHCI_ADMA_ADDRESS_LOW
);
1520 } else if (adma_state
== SDHCI_ADMA_ERR_STATE_TFR
) {
1521 addr
= RD4(slot
, SDHCI_ADMA_ADDRESS_LOW
);
1522 if (addr
> sizeof(*desc
))
1523 addr
-= sizeof(*desc
);
1527 slot_printf(slot
, "Invalid ADMA2 state 0x%02x\n", adma_state
);
1529 if (addr
>= descmem
->dmem_busaddr
&&
1530 addr
< descmem
->dmem_busaddr
+ SDHCI_ADMA2_DESCBUF_SIZE
) {
1531 desc
= (void *) ((char *)descmem
->dmem_addr
+
1532 (addr
- descmem
->dmem_busaddr
));
1534 "Descriptor: Addr=0x%08x Length=0x%04x Attr=0x%04x\n",
1535 desc
->address
, desc
->length
, desc
->attribute
);
1538 if (slot
->curcmd
&& (slot
->flags
& SDHCI_USE_ADMA2
)) {
1539 sdhci_reset(slot
, SDHCI_RESET_CMD
);
1544 sdhci_generic_intr(struct sdhci_slot
*slot
)
1549 /* Read slot interrupt status. */
1550 intmask
= RD4(slot
, SDHCI_INT_STATUS
);
1551 if (intmask
== 0 || intmask
== 0xffffffff) {
1555 if (sdhci_debug
> 2)
1556 slot_printf(slot
, "Interrupt %#x\n", intmask
);
1558 /* Handle card presence interrupts. */
1559 if (intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
)) {
1560 WR4(slot
, SDHCI_INT_STATUS
, intmask
&
1561 (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
));
1563 if (intmask
& SDHCI_INT_CARD_REMOVE
) {
1564 if (bootverbose
|| sdhci_debug
)
1565 slot_printf(slot
, "Card removed\n");
1566 callout_stop(&slot
->card_callout
);
1567 taskqueue_enqueue(taskqueue_swi_mp
, &slot
->card_task
);
1569 if (intmask
& SDHCI_INT_CARD_INSERT
) {
1570 if (bootverbose
|| sdhci_debug
)
1571 slot_printf(slot
, "Card inserted\n");
1572 callout_reset(&slot
->card_callout
, hz
/ 2,
1573 sdhci_card_delay
, slot
);
1575 intmask
&= ~(SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
);
1577 /* Handle command interrupts. */
1578 if (intmask
& SDHCI_INT_CMD_MASK
) {
1579 WR4(slot
, SDHCI_INT_STATUS
, intmask
& SDHCI_INT_CMD_MASK
);
1580 sdhci_cmd_irq(slot
, intmask
& SDHCI_INT_CMD_MASK
);
1582 /* Handle data interrupts. */
1583 if (intmask
& SDHCI_INT_DATA_MASK
) {
1584 WR4(slot
, SDHCI_INT_STATUS
, intmask
& SDHCI_INT_DATA_MASK
);
1585 /* Dont call data_irq in case of errored command */
1586 if ((intmask
& SDHCI_INT_CMD_ERROR_MASK
) == 0)
1587 sdhci_data_irq(slot
, intmask
& SDHCI_INT_DATA_MASK
);
1589 /* Handle AutoCMD12 error interrupt. */
1590 if (intmask
& SDHCI_INT_ACMD12ERR
) {
1591 WR4(slot
, SDHCI_INT_STATUS
, SDHCI_INT_ACMD12ERR
);
1592 sdhci_acmd_irq(slot
);
1594 /* Handle ADMA2 error interrupt. */
1595 if (intmask
& SDHCI_INT_ADMAERR
) {
1596 WR4(slot
, SDHCI_INT_STATUS
, SDHCI_INT_ADMAERR
);
1597 sdhci_adma_irq(slot
);
1599 intmask
&= ~(SDHCI_INT_CMD_MASK
| SDHCI_INT_DATA_MASK
);
1600 intmask
&= ~SDHCI_INT_ACMD12ERR
;
1601 intmask
&= ~SDHCI_INT_ADMAERR
;
1602 intmask
&= ~SDHCI_INT_ERROR
;
1603 /* Handle bus power interrupt. */
1604 if (intmask
& SDHCI_INT_BUS_POWER
) {
1605 WR4(slot
, SDHCI_INT_STATUS
, SDHCI_INT_BUS_POWER
);
1607 "Card is consuming too much power!\n");
1608 intmask
&= ~SDHCI_INT_BUS_POWER
;
1610 /* The rest is unknown. */
1612 WR4(slot
, SDHCI_INT_STATUS
, intmask
);
1613 slot_printf(slot
, "Unexpected interrupt 0x%08x.\n",
1615 sdhci_dumpregs(slot
);
1622 sdhci_generic_read_ivar(device_t bus
, device_t child
, int which
, uintptr_t *result
)
1624 struct sdhci_slot
*slot
= device_get_ivars(child
);
1629 case MMCBR_IVAR_BUS_MODE
:
1630 *(int *)result
= slot
->host
.ios
.bus_mode
;
1632 case MMCBR_IVAR_BUS_WIDTH
:
1633 *(int *)result
= slot
->host
.ios
.bus_width
;
1635 case MMCBR_IVAR_CHIP_SELECT
:
1636 *(int *)result
= slot
->host
.ios
.chip_select
;
1638 case MMCBR_IVAR_CLOCK
:
1639 *(int *)result
= slot
->host
.ios
.clock
;
1641 case MMCBR_IVAR_F_MIN
:
1642 *(int *)result
= slot
->host
.f_min
;
1644 case MMCBR_IVAR_F_MAX
:
1645 *(int *)result
= slot
->host
.f_max
;
1647 case MMCBR_IVAR_HOST_OCR
:
1648 *(int *)result
= slot
->host
.host_ocr
;
1650 case MMCBR_IVAR_MODE
:
1651 *(int *)result
= slot
->host
.mode
;
1653 case MMCBR_IVAR_OCR
:
1654 *(int *)result
= slot
->host
.ocr
;
1656 case MMCBR_IVAR_POWER_MODE
:
1657 *(int *)result
= slot
->host
.ios
.power_mode
;
1659 case MMCBR_IVAR_VDD
:
1660 *(int *)result
= slot
->host
.ios
.vdd
;
1662 case MMCBR_IVAR_CAPS
:
1663 *(int *)result
= slot
->host
.caps
;
1665 case MMCBR_IVAR_TIMING
:
1666 *(int *)result
= slot
->host
.ios
.timing
;
1668 case MMCBR_IVAR_MAX_DATA
:
1669 *(int *)result
= 65535;
1676 sdhci_generic_write_ivar(device_t bus
, device_t child
, int which
, uintptr_t value
)
1678 struct sdhci_slot
*slot
= device_get_ivars(child
);
1683 case MMCBR_IVAR_BUS_MODE
:
1684 slot
->host
.ios
.bus_mode
= value
;
1686 case MMCBR_IVAR_BUS_WIDTH
:
1687 slot
->host
.ios
.bus_width
= value
;
1689 case MMCBR_IVAR_CHIP_SELECT
:
1690 slot
->host
.ios
.chip_select
= value
;
1692 case MMCBR_IVAR_CLOCK
:
1698 max_clock
= slot
->max_clk
;
1701 if (slot
->version
< SDHCI_SPEC_300
) {
1702 for (i
= 0; i
< SDHCI_200_MAX_DIVIDER
;
1710 for (i
= 0; i
< SDHCI_300_MAX_DIVIDER
;
1714 clock
= max_clock
/ (i
+ 2);
1718 slot
->host
.ios
.clock
= clock
;
1720 slot
->host
.ios
.clock
= 0;
1722 case MMCBR_IVAR_MODE
:
1723 slot
->host
.mode
= value
;
1725 case MMCBR_IVAR_OCR
:
1726 slot
->host
.ocr
= value
;
1728 case MMCBR_IVAR_POWER_MODE
:
1729 slot
->host
.ios
.power_mode
= value
;
1731 case MMCBR_IVAR_VDD
:
1732 slot
->host
.ios
.vdd
= value
;
1734 case MMCBR_IVAR_TIMING
:
1735 slot
->host
.ios
.timing
= value
;
1737 case MMCBR_IVAR_CAPS
:
1738 case MMCBR_IVAR_HOST_OCR
:
1739 case MMCBR_IVAR_F_MIN
:
1740 case MMCBR_IVAR_F_MAX
:
1741 case MMCBR_IVAR_MAX_DATA
:
1747 MODULE_VERSION(sdhci
, 1);