xref: /freebsd/sys/dev/sdhci/sdhci.c (revision 85083a8072899e77c00da29b1b2fe2aaf26f0e3c)
1831f5dcfSAlexander Motin /*-
2831f5dcfSAlexander Motin  * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
3831f5dcfSAlexander Motin  * All rights reserved.
4831f5dcfSAlexander Motin  *
5831f5dcfSAlexander Motin  * Redistribution and use in source and binary forms, with or without
6831f5dcfSAlexander Motin  * modification, are permitted provided that the following conditions
7831f5dcfSAlexander Motin  * are met:
8831f5dcfSAlexander Motin  * 1. Redistributions of source code must retain the above copyright
9831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer.
10831f5dcfSAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
11831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
12831f5dcfSAlexander Motin  *    documentation and/or other materials provided with the distribution.
13831f5dcfSAlexander Motin  *
14831f5dcfSAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15831f5dcfSAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16831f5dcfSAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17831f5dcfSAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18831f5dcfSAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19831f5dcfSAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20831f5dcfSAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21831f5dcfSAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22831f5dcfSAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23831f5dcfSAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24831f5dcfSAlexander Motin  */
25831f5dcfSAlexander Motin 
26831f5dcfSAlexander Motin #include <sys/cdefs.h>
27831f5dcfSAlexander Motin __FBSDID("$FreeBSD$");
28831f5dcfSAlexander Motin 
29831f5dcfSAlexander Motin #include <sys/param.h>
30831f5dcfSAlexander Motin #include <sys/systm.h>
31831f5dcfSAlexander Motin #include <sys/bus.h>
32e64f01a9SIan Lepore #include <sys/callout.h>
33831f5dcfSAlexander Motin #include <sys/conf.h>
34831f5dcfSAlexander Motin #include <sys/kernel.h>
35831f5dcfSAlexander Motin #include <sys/lock.h>
36831f5dcfSAlexander Motin #include <sys/module.h>
37831f5dcfSAlexander Motin #include <sys/mutex.h>
38831f5dcfSAlexander Motin #include <sys/resource.h>
39831f5dcfSAlexander Motin #include <sys/rman.h>
405b69a497SAlexander Motin #include <sys/sysctl.h>
41831f5dcfSAlexander Motin #include <sys/taskqueue.h>
42831f5dcfSAlexander Motin 
43831f5dcfSAlexander Motin #include <machine/bus.h>
44831f5dcfSAlexander Motin #include <machine/resource.h>
45831f5dcfSAlexander Motin #include <machine/stdarg.h>
46831f5dcfSAlexander Motin 
47831f5dcfSAlexander Motin #include <dev/mmc/bridge.h>
48831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h>
49831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h>
50831f5dcfSAlexander Motin 
51831f5dcfSAlexander Motin #include "mmcbr_if.h"
52831f5dcfSAlexander Motin #include "sdhci.h"
53d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h"
54831f5dcfSAlexander Motin 
55f0d2731dSMarius Strobl SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver");
56831f5dcfSAlexander Motin 
57f0d2731dSMarius Strobl static int sdhci_debug;
587e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0,
597e6ccea3SMarius Strobl     "Debug level");
600f34084fSMarius Strobl u_int sdhci_quirk_clear = 0;
610f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear,
620f34084fSMarius Strobl     0, "Mask of quirks to clear");
630f34084fSMarius Strobl u_int sdhci_quirk_set = 0;
640f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0,
650f34084fSMarius Strobl     "Mask of quirks to set");
665b69a497SAlexander Motin 
67d6b3aaf8SOleksandr Tymoshenko #define	RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
68d6b3aaf8SOleksandr Tymoshenko #define	RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
69d6b3aaf8SOleksandr Tymoshenko #define	RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
70d6b3aaf8SOleksandr Tymoshenko #define	RD_MULTI_4(slot, off, ptr, count)	\
71d6b3aaf8SOleksandr Tymoshenko     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
72831f5dcfSAlexander Motin 
73d6b3aaf8SOleksandr Tymoshenko #define	WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
74d6b3aaf8SOleksandr Tymoshenko #define	WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
75d6b3aaf8SOleksandr Tymoshenko #define	WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
76d6b3aaf8SOleksandr Tymoshenko #define	WR_MULTI_4(slot, off, ptr, count)	\
77d6b3aaf8SOleksandr Tymoshenko     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
78831f5dcfSAlexander Motin 
79831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
80831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot);
81831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data);
82831f5dcfSAlexander Motin 
83639f59f0SIan Lepore static void sdhci_card_poll(void *);
84831f5dcfSAlexander Motin static void sdhci_card_task(void *, int);
85831f5dcfSAlexander Motin 
86831f5dcfSAlexander Motin /* helper routines */
870f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot);
880f34084fSMarius Strobl static int slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
890f34084fSMarius Strobl     __printflike(2, 3);
900f34084fSMarius Strobl 
91831f5dcfSAlexander Motin #define	SDHCI_LOCK(_slot)		mtx_lock(&(_slot)->mtx)
92831f5dcfSAlexander Motin #define	SDHCI_UNLOCK(_slot)		mtx_unlock(&(_slot)->mtx)
93831f5dcfSAlexander Motin #define	SDHCI_LOCK_INIT(_slot) \
94831f5dcfSAlexander Motin 	mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
95831f5dcfSAlexander Motin #define	SDHCI_LOCK_DESTROY(_slot)	mtx_destroy(&_slot->mtx);
96831f5dcfSAlexander Motin #define	SDHCI_ASSERT_LOCKED(_slot)	mtx_assert(&_slot->mtx, MA_OWNED);
97831f5dcfSAlexander Motin #define	SDHCI_ASSERT_UNLOCKED(_slot)	mtx_assert(&_slot->mtx, MA_NOTOWNED);
98831f5dcfSAlexander Motin 
9933aad34dSOleksandr Tymoshenko #define	SDHCI_DEFAULT_MAX_FREQ	50
10033aad34dSOleksandr Tymoshenko 
10157677a3aSOleksandr Tymoshenko #define	SDHCI_200_MAX_DIVIDER	256
10257677a3aSOleksandr Tymoshenko #define	SDHCI_300_MAX_DIVIDER	2046
10357677a3aSOleksandr Tymoshenko 
104639f59f0SIan Lepore #define	SDHCI_CARD_PRESENT_TICKS	(hz / 5)
105639f59f0SIan Lepore #define	SDHCI_INSERT_DELAY_TICKS	(hz / 2)
106639f59f0SIan Lepore 
10793efdc63SAdrian Chadd /*
10893efdc63SAdrian Chadd  * Broadcom BCM577xx Controller Constants
10993efdc63SAdrian Chadd  */
1101bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */
1111bacf3beSMarius Strobl #define	BCM577XX_DEFAULT_MAX_DIVIDER	256
1121bacf3beSMarius Strobl /* Alternative clock's base frequency. */
1131bacf3beSMarius Strobl #define	BCM577XX_ALT_CLOCK_BASE		63000000
11493efdc63SAdrian Chadd 
11593efdc63SAdrian Chadd #define	BCM577XX_HOST_CONTROL		0x198
11693efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_MASK	0xFFFFCFFF
11793efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_SHIFT	12
11893efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_DEFAULT	0x0
11993efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_64MHZ	0x3
12093efdc63SAdrian Chadd 
121831f5dcfSAlexander Motin static void
122831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
123831f5dcfSAlexander Motin {
1247e6ccea3SMarius Strobl 
125831f5dcfSAlexander Motin 	if (error != 0) {
126831f5dcfSAlexander Motin 		printf("getaddr: error %d\n", error);
127831f5dcfSAlexander Motin 		return;
128831f5dcfSAlexander Motin 	}
129831f5dcfSAlexander Motin 	*(bus_addr_t *)arg = segs[0].ds_addr;
130831f5dcfSAlexander Motin }
131831f5dcfSAlexander Motin 
132d6b3aaf8SOleksandr Tymoshenko static int
133d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
134d6b3aaf8SOleksandr Tymoshenko {
135d6b3aaf8SOleksandr Tymoshenko 	va_list ap;
136d6b3aaf8SOleksandr Tymoshenko 	int retval;
137d6b3aaf8SOleksandr Tymoshenko 
138d6b3aaf8SOleksandr Tymoshenko 	retval = printf("%s-slot%d: ",
139d6b3aaf8SOleksandr Tymoshenko 	    device_get_nameunit(slot->bus), slot->num);
140d6b3aaf8SOleksandr Tymoshenko 
141d6b3aaf8SOleksandr Tymoshenko 	va_start(ap, fmt);
142d6b3aaf8SOleksandr Tymoshenko 	retval += vprintf(fmt, ap);
143d6b3aaf8SOleksandr Tymoshenko 	va_end(ap);
144d6b3aaf8SOleksandr Tymoshenko 	return (retval);
145d6b3aaf8SOleksandr Tymoshenko }
146d6b3aaf8SOleksandr Tymoshenko 
147831f5dcfSAlexander Motin static void
148831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot)
149831f5dcfSAlexander Motin {
1507e6ccea3SMarius Strobl 
151831f5dcfSAlexander Motin 	slot_printf(slot,
152831f5dcfSAlexander Motin 	    "============== REGISTER DUMP ==============\n");
153831f5dcfSAlexander Motin 
154831f5dcfSAlexander Motin 	slot_printf(slot, "Sys addr: 0x%08x | Version:  0x%08x\n",
155831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
156831f5dcfSAlexander Motin 	slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
157831f5dcfSAlexander Motin 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
158831f5dcfSAlexander Motin 	slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
159831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
160831f5dcfSAlexander Motin 	slot_printf(slot, "Present:  0x%08x | Host ctl: 0x%08x\n",
161831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
162831f5dcfSAlexander Motin 	slot_printf(slot, "Power:    0x%08x | Blk gap:  0x%08x\n",
163831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
164831f5dcfSAlexander Motin 	slot_printf(slot, "Wake-up:  0x%08x | Clock:    0x%08x\n",
165831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
166831f5dcfSAlexander Motin 	slot_printf(slot, "Timeout:  0x%08x | Int stat: 0x%08x\n",
167831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
168831f5dcfSAlexander Motin 	slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
169831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
1709dbf8c46SMarius Strobl 	slot_printf(slot, "AC12 err: 0x%08x | Host ctl2: 0x%08x\n",
1719dbf8c46SMarius Strobl 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2));
1729dbf8c46SMarius Strobl 	slot_printf(slot, "Caps:     0x%08x | Caps2:    0x%08x\n",
1739dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2));
1749dbf8c46SMarius Strobl 	slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n",
1759dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR));
1769dbf8c46SMarius Strobl 	slot_printf(slot, "ADMA addr: 0x%08x | Slot int: 0x%08x\n",
1779dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS));
178831f5dcfSAlexander Motin 
179831f5dcfSAlexander Motin 	slot_printf(slot,
180831f5dcfSAlexander Motin 	    "===========================================\n");
181831f5dcfSAlexander Motin }
182831f5dcfSAlexander Motin 
183831f5dcfSAlexander Motin static void
184831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
185831f5dcfSAlexander Motin {
186831f5dcfSAlexander Motin 	int timeout;
187b440e965SMarius Strobl 	uint32_t clock;
188831f5dcfSAlexander Motin 
189d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
1906e37fb2bSIan Lepore 		if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
191831f5dcfSAlexander Motin 			return;
192831f5dcfSAlexander Motin 	}
193831f5dcfSAlexander Motin 
194831f5dcfSAlexander Motin 	/* Some controllers need this kick or reset won't work. */
195831f5dcfSAlexander Motin 	if ((mask & SDHCI_RESET_ALL) == 0 &&
196d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
197831f5dcfSAlexander Motin 		/* This is to force an update */
198831f5dcfSAlexander Motin 		clock = slot->clock;
199831f5dcfSAlexander Motin 		slot->clock = 0;
200831f5dcfSAlexander Motin 		sdhci_set_clock(slot, clock);
201831f5dcfSAlexander Motin 	}
202831f5dcfSAlexander Motin 
203d8208d9eSAlexander Motin 	if (mask & SDHCI_RESET_ALL) {
204831f5dcfSAlexander Motin 		slot->clock = 0;
205d8208d9eSAlexander Motin 		slot->power = 0;
206d8208d9eSAlexander Motin 	}
207831f5dcfSAlexander Motin 
20861bc42f7SIan Lepore 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
20961bc42f7SIan Lepore 
21061bc42f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
21161bc42f7SIan Lepore 		/*
21261bc42f7SIan Lepore 		 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
21361bc42f7SIan Lepore 		 * specification.  The reset bit has internal propagation delay,
21461bc42f7SIan Lepore 		 * so a fast read after write returns 0 even if reset process is
21561bc42f7SIan Lepore 		 * in progress.  The workaround is to poll for 1 before polling
21661bc42f7SIan Lepore 		 * for 0.  In the worst case, if we miss seeing it asserted the
21761bc42f7SIan Lepore 		 * time we spent waiting is enough to ensure the reset finishes.
21861bc42f7SIan Lepore 		 */
21961bc42f7SIan Lepore 		timeout = 10000;
22061bc42f7SIan Lepore 		while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
22161bc42f7SIan Lepore 			if (timeout <= 0)
22261bc42f7SIan Lepore 				break;
22361bc42f7SIan Lepore 			timeout--;
22461bc42f7SIan Lepore 			DELAY(1);
22561bc42f7SIan Lepore 		}
22661bc42f7SIan Lepore 	}
22761bc42f7SIan Lepore 
228831f5dcfSAlexander Motin 	/* Wait max 100 ms */
22961bc42f7SIan Lepore 	timeout = 10000;
230831f5dcfSAlexander Motin 	/* Controller clears the bits when it's done */
23161bc42f7SIan Lepore 	while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
23261bc42f7SIan Lepore 		if (timeout <= 0) {
23361bc42f7SIan Lepore 			slot_printf(slot, "Reset 0x%x never completed.\n",
23461bc42f7SIan Lepore 			    mask);
235831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
236831f5dcfSAlexander Motin 			return;
237831f5dcfSAlexander Motin 		}
238831f5dcfSAlexander Motin 		timeout--;
23961bc42f7SIan Lepore 		DELAY(10);
240831f5dcfSAlexander Motin 	}
241831f5dcfSAlexander Motin }
242831f5dcfSAlexander Motin 
243831f5dcfSAlexander Motin static void
244831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot)
245831f5dcfSAlexander Motin {
246831f5dcfSAlexander Motin 
247831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
248831f5dcfSAlexander Motin 
249831f5dcfSAlexander Motin 	/* Enable interrupts. */
250831f5dcfSAlexander Motin 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
251831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
252831f5dcfSAlexander Motin 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
253831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
254831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
255831f5dcfSAlexander Motin 	    SDHCI_INT_ACMD12ERR;
256639f59f0SIan Lepore 
257639f59f0SIan Lepore 	if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
258639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
259639f59f0SIan Lepore 		slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
260639f59f0SIan Lepore 	}
261639f59f0SIan Lepore 
262831f5dcfSAlexander Motin 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
263831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
264831f5dcfSAlexander Motin }
265831f5dcfSAlexander Motin 
266831f5dcfSAlexander Motin static void
267831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
268831f5dcfSAlexander Motin {
26993efdc63SAdrian Chadd 	uint32_t clk_base;
27093efdc63SAdrian Chadd 	uint32_t clk_sel;
271831f5dcfSAlexander Motin 	uint32_t res;
272831f5dcfSAlexander Motin 	uint16_t clk;
2738f3b7d56SOleksandr Tymoshenko 	uint16_t div;
274831f5dcfSAlexander Motin 	int timeout;
275831f5dcfSAlexander Motin 
276831f5dcfSAlexander Motin 	if (clock == slot->clock)
277831f5dcfSAlexander Motin 		return;
278831f5dcfSAlexander Motin 	slot->clock = clock;
279831f5dcfSAlexander Motin 
280831f5dcfSAlexander Motin 	/* Turn off the clock. */
2814ddc0172SIan Lepore 	clk = RD2(slot, SDHCI_CLOCK_CONTROL);
2824ddc0172SIan Lepore 	WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
283b440e965SMarius Strobl 	/* If no clock requested - leave it so. */
284831f5dcfSAlexander Motin 	if (clock == 0)
285831f5dcfSAlexander Motin 		return;
286ceb9e9f7SIan Lepore 
28793efdc63SAdrian Chadd 	/* Determine the clock base frequency */
28893efdc63SAdrian Chadd 	clk_base = slot->max_clk;
28993efdc63SAdrian Chadd 	if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
2901bacf3beSMarius Strobl 		clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) &
2911bacf3beSMarius Strobl 		    BCM577XX_CTRL_CLKSEL_MASK;
29293efdc63SAdrian Chadd 
2931bacf3beSMarius Strobl 		/*
2941bacf3beSMarius Strobl 		 * Select clock source appropriate for the requested frequency.
2951bacf3beSMarius Strobl 		 */
29693efdc63SAdrian Chadd 		if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
29793efdc63SAdrian Chadd 			clk_base = BCM577XX_ALT_CLOCK_BASE;
2981bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ <<
2991bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
30093efdc63SAdrian Chadd 		} else {
3011bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT <<
3021bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
30393efdc63SAdrian Chadd 		}
30493efdc63SAdrian Chadd 
30593efdc63SAdrian Chadd 		WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
30693efdc63SAdrian Chadd 	}
30793efdc63SAdrian Chadd 
308ceb9e9f7SIan Lepore 	/* Recalculate timeout clock frequency based on the new sd clock. */
309ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
310ceb9e9f7SIan Lepore 		slot->timeout_clk = slot->clock / 1000;
311ceb9e9f7SIan Lepore 
3128f3b7d56SOleksandr Tymoshenko 	if (slot->version < SDHCI_SPEC_300) {
313831f5dcfSAlexander Motin 		/* Looking for highest freq <= clock. */
31493efdc63SAdrian Chadd 		res = clk_base;
31557677a3aSOleksandr Tymoshenko 		for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
316831f5dcfSAlexander Motin 			if (res <= clock)
317831f5dcfSAlexander Motin 				break;
318831f5dcfSAlexander Motin 			res >>= 1;
319831f5dcfSAlexander Motin 		}
320831f5dcfSAlexander Motin 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
3218f3b7d56SOleksandr Tymoshenko 		div >>= 1;
322c11bbc7dSMarius Strobl 	} else {
3238f3b7d56SOleksandr Tymoshenko 		/* Version 3.0 divisors are multiples of two up to 1023 * 2 */
32493efdc63SAdrian Chadd 		if (clock >= clk_base)
32557677a3aSOleksandr Tymoshenko 			div = 0;
3268f3b7d56SOleksandr Tymoshenko 		else {
32757677a3aSOleksandr Tymoshenko 			for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
32893efdc63SAdrian Chadd 				if ((clk_base / div) <= clock)
3298f3b7d56SOleksandr Tymoshenko 					break;
3308f3b7d56SOleksandr Tymoshenko 			}
3318f3b7d56SOleksandr Tymoshenko 		}
3328f3b7d56SOleksandr Tymoshenko 		div >>= 1;
3338f3b7d56SOleksandr Tymoshenko 	}
3348f3b7d56SOleksandr Tymoshenko 
3358f3b7d56SOleksandr Tymoshenko 	if (bootverbose || sdhci_debug)
33693efdc63SAdrian Chadd 		slot_printf(slot, "Divider %d for freq %d (base %d)\n",
33793efdc63SAdrian Chadd 			div, clock, clk_base);
3388f3b7d56SOleksandr Tymoshenko 
339831f5dcfSAlexander Motin 	/* Now we have got divider, set it. */
3408f3b7d56SOleksandr Tymoshenko 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
3418f3b7d56SOleksandr Tymoshenko 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
3428f3b7d56SOleksandr Tymoshenko 		<< SDHCI_DIVIDER_HI_SHIFT;
3438f3b7d56SOleksandr Tymoshenko 
344831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
345831f5dcfSAlexander Motin 	/* Enable clock. */
346831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_INT_EN;
347831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
348831f5dcfSAlexander Motin 	/* Wait up to 10 ms until it stabilize. */
349831f5dcfSAlexander Motin 	timeout = 10;
350831f5dcfSAlexander Motin 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
351831f5dcfSAlexander Motin 		& SDHCI_CLOCK_INT_STABLE)) {
352831f5dcfSAlexander Motin 		if (timeout == 0) {
353831f5dcfSAlexander Motin 			slot_printf(slot,
354831f5dcfSAlexander Motin 			    "Internal clock never stabilised.\n");
355831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
356831f5dcfSAlexander Motin 			return;
357831f5dcfSAlexander Motin 		}
358831f5dcfSAlexander Motin 		timeout--;
359831f5dcfSAlexander Motin 		DELAY(1000);
360831f5dcfSAlexander Motin 	}
361831f5dcfSAlexander Motin 	/* Pass clock signal to the bus. */
362831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_CARD_EN;
363831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
364831f5dcfSAlexander Motin }
365831f5dcfSAlexander Motin 
366831f5dcfSAlexander Motin static void
367831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power)
368831f5dcfSAlexander Motin {
369*85083a80SMarius Strobl 	int i;
370831f5dcfSAlexander Motin 	uint8_t pwr;
371831f5dcfSAlexander Motin 
372831f5dcfSAlexander Motin 	if (slot->power == power)
373831f5dcfSAlexander Motin 		return;
374d6b3aaf8SOleksandr Tymoshenko 
375831f5dcfSAlexander Motin 	slot->power = power;
376831f5dcfSAlexander Motin 
377831f5dcfSAlexander Motin 	/* Turn off the power. */
378831f5dcfSAlexander Motin 	pwr = 0;
379831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
380b440e965SMarius Strobl 	/* If power down requested - leave it so. */
381831f5dcfSAlexander Motin 	if (power == 0)
382831f5dcfSAlexander Motin 		return;
383831f5dcfSAlexander Motin 	/* Set voltage. */
384831f5dcfSAlexander Motin 	switch (1 << power) {
385831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
386831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
387831f5dcfSAlexander Motin 		break;
388831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
389831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
390831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
391831f5dcfSAlexander Motin 		break;
392831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
393831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
394831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
395831f5dcfSAlexander Motin 		break;
396831f5dcfSAlexander Motin 	}
397831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
398*85083a80SMarius Strobl 	/*
399*85083a80SMarius Strobl 	 * Turn on VDD1 power.  Note that at least some Intel controllers can
400*85083a80SMarius Strobl 	 * fail to enable bus power on the first try after transiting from D3
401*85083a80SMarius Strobl 	 * to D0, so we give them up to 20 ms.
402*85083a80SMarius Strobl 	 */
403831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
404*85083a80SMarius Strobl 	for (i = 0; i < 20; i++) {
405831f5dcfSAlexander Motin 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
406*85083a80SMarius Strobl 		if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)
407*85083a80SMarius Strobl 			break;
408*85083a80SMarius Strobl 		DELAY(100);
409*85083a80SMarius Strobl 	}
410*85083a80SMarius Strobl 	if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON))
411*85083a80SMarius Strobl 		slot_printf(slot, "Bus power failed to enable");
412a2832f9fSMarius Strobl 
413a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) {
414a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10);
415a2832f9fSMarius Strobl 		DELAY(10);
416a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
417a2832f9fSMarius Strobl 		DELAY(300);
418a2832f9fSMarius Strobl 	}
419831f5dcfSAlexander Motin }
420831f5dcfSAlexander Motin 
421831f5dcfSAlexander Motin static void
422831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
423831f5dcfSAlexander Motin {
424831f5dcfSAlexander Motin 	uint32_t data;
425831f5dcfSAlexander Motin 	char *buffer;
426831f5dcfSAlexander Motin 	size_t left;
427831f5dcfSAlexander Motin 
428831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
429831f5dcfSAlexander Motin 	buffer += slot->offset;
430831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
431831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
432831f5dcfSAlexander Motin 	slot->offset += left;
433831f5dcfSAlexander Motin 
434831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
435d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
436831f5dcfSAlexander Motin 		DELAY(10);
437ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
438831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
439831f5dcfSAlexander Motin 		while (left > 3) {
440831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
441831f5dcfSAlexander Motin 			buffer[0] = data;
442831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
443831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
444831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
445831f5dcfSAlexander Motin 			buffer += 4;
446831f5dcfSAlexander Motin 			left -= 4;
447831f5dcfSAlexander Motin 		}
448831f5dcfSAlexander Motin 	} else {
449d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
450831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
451831f5dcfSAlexander Motin 		left &= 3;
452831f5dcfSAlexander Motin 	}
453831f5dcfSAlexander Motin 	/* Handle uneven size case. */
454831f5dcfSAlexander Motin 	if (left > 0) {
455831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
456831f5dcfSAlexander Motin 		while (left > 0) {
457831f5dcfSAlexander Motin 			*(buffer++) = data;
458831f5dcfSAlexander Motin 			data >>= 8;
459831f5dcfSAlexander Motin 			left--;
460831f5dcfSAlexander Motin 		}
461831f5dcfSAlexander Motin 	}
462831f5dcfSAlexander Motin }
463831f5dcfSAlexander Motin 
464831f5dcfSAlexander Motin static void
465831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
466831f5dcfSAlexander Motin {
467831f5dcfSAlexander Motin 	uint32_t data = 0;
468831f5dcfSAlexander Motin 	char *buffer;
469831f5dcfSAlexander Motin 	size_t left;
470831f5dcfSAlexander Motin 
471831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
472831f5dcfSAlexander Motin 	buffer += slot->offset;
473831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
474831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
475831f5dcfSAlexander Motin 	slot->offset += left;
476831f5dcfSAlexander Motin 
477ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
478831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
479831f5dcfSAlexander Motin 		while (left > 3) {
480831f5dcfSAlexander Motin 			data = buffer[0] +
481831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
482831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
483831f5dcfSAlexander Motin 			    (buffer[3] << 24);
484831f5dcfSAlexander Motin 			left -= 4;
485831f5dcfSAlexander Motin 			buffer += 4;
486831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
487831f5dcfSAlexander Motin 		}
488831f5dcfSAlexander Motin 	} else {
489d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
490831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
491831f5dcfSAlexander Motin 		left &= 3;
492831f5dcfSAlexander Motin 	}
493831f5dcfSAlexander Motin 	/* Handle uneven size case. */
494831f5dcfSAlexander Motin 	if (left > 0) {
495831f5dcfSAlexander Motin 		while (left > 0) {
496831f5dcfSAlexander Motin 			data <<= 8;
497831f5dcfSAlexander Motin 			data += *(buffer++);
498831f5dcfSAlexander Motin 			left--;
499831f5dcfSAlexander Motin 		}
500831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
501831f5dcfSAlexander Motin 	}
502831f5dcfSAlexander Motin }
503831f5dcfSAlexander Motin 
504831f5dcfSAlexander Motin static void
505831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
506831f5dcfSAlexander Motin {
507831f5dcfSAlexander Motin 
508831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
509831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
510831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
511831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
512831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
513831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
514831f5dcfSAlexander Motin 				break;
515831f5dcfSAlexander Motin 		}
516831f5dcfSAlexander Motin 	} else {
517831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
518831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
519831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
520831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
521831f5dcfSAlexander Motin 				break;
522831f5dcfSAlexander Motin 		}
523831f5dcfSAlexander Motin 	}
524831f5dcfSAlexander Motin }
525831f5dcfSAlexander Motin 
526831f5dcfSAlexander Motin static void
5277e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused)
528831f5dcfSAlexander Motin {
529831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
5307e6ccea3SMarius Strobl 	device_t d;
531831f5dcfSAlexander Motin 
532831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
5336e37fb2bSIan Lepore 	if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
534831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
535831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
536639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
537639f59f0SIan Lepore 				slot_printf(slot, "Card inserted\n");
538d6b3aaf8SOleksandr Tymoshenko 			slot->dev = device_add_child(slot->bus, "mmc", -1);
539831f5dcfSAlexander Motin 			device_set_ivars(slot->dev, slot);
540831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
541831f5dcfSAlexander Motin 			device_probe_and_attach(slot->dev);
542831f5dcfSAlexander Motin 		} else
543831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
544831f5dcfSAlexander Motin 	} else {
545831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
546831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
547639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
548639f59f0SIan Lepore 				slot_printf(slot, "Card removed\n");
5497e6ccea3SMarius Strobl 			d = slot->dev;
550831f5dcfSAlexander Motin 			slot->dev = NULL;
551831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
552d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
553831f5dcfSAlexander Motin 		} else
554831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
555831f5dcfSAlexander Motin 	}
556831f5dcfSAlexander Motin }
557831f5dcfSAlexander Motin 
558b8bf08b1SIan Lepore static void
559b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
560639f59f0SIan Lepore {
561639f59f0SIan Lepore 	bool was_present;
562639f59f0SIan Lepore 
563639f59f0SIan Lepore 	/*
564639f59f0SIan Lepore 	 * If there was no card and now there is one, schedule the task to
565639f59f0SIan Lepore 	 * create the child device after a short delay.  The delay is to
566639f59f0SIan Lepore 	 * debounce the card insert (sometimes the card detect pin stabilizes
567639f59f0SIan Lepore 	 * before the other pins have made good contact).
568639f59f0SIan Lepore 	 *
569639f59f0SIan Lepore 	 * If there was a card present and now it's gone, immediately schedule
570639f59f0SIan Lepore 	 * the task to delete the child device.  No debouncing -- gone is gone,
571639f59f0SIan Lepore 	 * because once power is removed, a full card re-init is needed, and
572639f59f0SIan Lepore 	 * that happens by deleting and recreating the child device.
573639f59f0SIan Lepore 	 */
574639f59f0SIan Lepore 	was_present = slot->dev != NULL;
575639f59f0SIan Lepore 	if (!was_present && is_present) {
576639f59f0SIan Lepore 		taskqueue_enqueue_timeout(taskqueue_swi_giant,
577639f59f0SIan Lepore 		    &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
578639f59f0SIan Lepore 	} else if (was_present && !is_present) {
579639f59f0SIan Lepore 		taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
580639f59f0SIan Lepore 	}
581b8bf08b1SIan Lepore }
582b8bf08b1SIan Lepore 
583b8bf08b1SIan Lepore void
584b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
585b8bf08b1SIan Lepore {
586b8bf08b1SIan Lepore 
587b8bf08b1SIan Lepore 	SDHCI_LOCK(slot);
588b8bf08b1SIan Lepore 	sdhci_handle_card_present_locked(slot, is_present);
589639f59f0SIan Lepore 	SDHCI_UNLOCK(slot);
590639f59f0SIan Lepore }
591639f59f0SIan Lepore 
592639f59f0SIan Lepore static void
593639f59f0SIan Lepore sdhci_card_poll(void *arg)
594639f59f0SIan Lepore {
595639f59f0SIan Lepore 	struct sdhci_slot *slot = arg;
596639f59f0SIan Lepore 
597639f59f0SIan Lepore 	sdhci_handle_card_present(slot,
598639f59f0SIan Lepore 	    SDHCI_GET_CARD_PRESENT(slot->bus, slot));
599639f59f0SIan Lepore 	callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
600639f59f0SIan Lepore 	    sdhci_card_poll, slot);
601639f59f0SIan Lepore }
602639f59f0SIan Lepore 
603d6b3aaf8SOleksandr Tymoshenko int
604d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
605831f5dcfSAlexander Motin {
6060f34084fSMarius Strobl 	uint32_t caps, caps2, freq, host_caps;
607d6b3aaf8SOleksandr Tymoshenko 	int err;
608831f5dcfSAlexander Motin 
609831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
610d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
611d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
612d6b3aaf8SOleksandr Tymoshenko 
613831f5dcfSAlexander Motin 	/* Allocate DMA tag. */
614831f5dcfSAlexander Motin 	err = bus_dma_tag_create(bus_get_dma_tag(dev),
615831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
616831f5dcfSAlexander Motin 	    BUS_SPACE_MAXADDR, NULL, NULL,
617831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE,
618831f5dcfSAlexander Motin 	    BUS_DMA_ALLOCNOW, NULL, NULL,
619831f5dcfSAlexander Motin 	    &slot->dmatag);
620831f5dcfSAlexander Motin 	if (err != 0) {
621831f5dcfSAlexander Motin 		device_printf(dev, "Can't create DMA tag\n");
622831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
623d6b3aaf8SOleksandr Tymoshenko 		return (err);
624831f5dcfSAlexander Motin 	}
625831f5dcfSAlexander Motin 	/* Allocate DMA memory. */
626831f5dcfSAlexander Motin 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
627831f5dcfSAlexander Motin 	    BUS_DMA_NOWAIT, &slot->dmamap);
628831f5dcfSAlexander Motin 	if (err != 0) {
629831f5dcfSAlexander Motin 		device_printf(dev, "Can't alloc DMA memory\n");
630831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
631d6b3aaf8SOleksandr Tymoshenko 		return (err);
632831f5dcfSAlexander Motin 	}
633831f5dcfSAlexander Motin 	/* Map the memory. */
634831f5dcfSAlexander Motin 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
635831f5dcfSAlexander Motin 	    (void *)slot->dmamem, DMA_BLOCK_SIZE,
636831f5dcfSAlexander Motin 	    sdhci_getaddr, &slot->paddr, 0);
637831f5dcfSAlexander Motin 	if (err != 0 || slot->paddr == 0) {
638831f5dcfSAlexander Motin 		device_printf(dev, "Can't load DMA memory\n");
639831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
640d6b3aaf8SOleksandr Tymoshenko 		if (err)
641d6b3aaf8SOleksandr Tymoshenko 			return (err);
642d6b3aaf8SOleksandr Tymoshenko 		else
643d6b3aaf8SOleksandr Tymoshenko 			return (EFAULT);
644831f5dcfSAlexander Motin 	}
645d6b3aaf8SOleksandr Tymoshenko 
646831f5dcfSAlexander Motin 	/* Initialize slot. */
647831f5dcfSAlexander Motin 	sdhci_init(slot);
648d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
649d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
6500f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) {
6518f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
6520f34084fSMarius Strobl 		caps2 = slot->caps2;
6530f34084fSMarius Strobl 	} else {
654831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
6550f34084fSMarius Strobl 		if (slot->version >= SDHCI_SPEC_300)
6560f34084fSMarius Strobl 			caps2 = RD4(slot, SDHCI_CAPABILITIES2);
6570f34084fSMarius Strobl 		else
6580f34084fSMarius Strobl 			caps2 = 0;
6590f34084fSMarius Strobl 	}
660831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
66133aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
66287a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
66387a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
66433aad34dSOleksandr Tymoshenko 	else
66587a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
66687a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
66787a6a871SIan Lepore 	if (freq != 0)
66887a6a871SIan Lepore 		slot->max_clk = freq * 1000000;
66987a6a871SIan Lepore 	/*
67087a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
67187a6a871SIan Lepore 	 * hasn't already set max_clk we're probably not going to work right
67287a6a871SIan Lepore 	 * with an assumption, so complain about it.
67387a6a871SIan Lepore 	 */
674831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
67587a6a871SIan Lepore 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
676831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify base clock "
6771bacf3beSMarius Strobl 		    "frequency, using %dMHz as default.\n",
6781bacf3beSMarius Strobl 		    SDHCI_DEFAULT_MAX_FREQ);
679831f5dcfSAlexander Motin 	}
680a2832f9fSMarius Strobl 	/* Calculate/set timeout clock frequency. */
6818f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
6828f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
683a2832f9fSMarius Strobl 	} else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) {
684a2832f9fSMarius Strobl 		slot->timeout_clk = 1000;
6858f3b7d56SOleksandr Tymoshenko 	} else {
6861bacf3beSMarius Strobl 		slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >>
6871bacf3beSMarius Strobl 		    SDHCI_TIMEOUT_CLK_SHIFT;
6888f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
6898f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
6908f3b7d56SOleksandr Tymoshenko 	}
69187a6a871SIan Lepore 	/*
69287a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
69387a6a871SIan Lepore 	 * hasn't already set timeout_clk we'll probably work okay using the
69487a6a871SIan Lepore 	 * max timeout, but still mention it.
69587a6a871SIan Lepore 	 */
696831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
697831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify timeout clock "
698ceb9e9f7SIan Lepore 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
699ceb9e9f7SIan Lepore 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
700831f5dcfSAlexander Motin 	}
701831f5dcfSAlexander Motin 
70257677a3aSOleksandr Tymoshenko 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
703831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
704831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
705831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
706831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
707831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
708831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
709831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_180)
710831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
711831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
712831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't report any "
713831f5dcfSAlexander Motin 		    "support voltages.\n");
714831f5dcfSAlexander Motin 	}
7150f34084fSMarius Strobl 	host_caps = MMC_CAP_4_BIT_DATA;
7162d1731b8SIan Lepore 	if (caps & SDHCI_CAN_DO_8BITBUS)
7170f34084fSMarius Strobl 		host_caps |= MMC_CAP_8_BIT_DATA;
718831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
7190f34084fSMarius Strobl 		host_caps |= MMC_CAP_HSPEED;
72072dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC)
7210f34084fSMarius Strobl 		host_caps |= MMC_CAP_BOOT_NOACC;
72272dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY)
7230f34084fSMarius Strobl 		host_caps |= MMC_CAP_WAIT_WHILE_BUSY;
7240f34084fSMarius Strobl 	if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50))
7250f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
7260f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_SDR104) {
7270f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
7280f34084fSMarius Strobl 		if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200))
7290f34084fSMarius Strobl 			host_caps |= MMC_CAP_MMC_HS200;
7300f34084fSMarius Strobl 	} else if (caps2 & SDHCI_CAN_SDR50)
7310f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR50;
7320f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_DDR50 &&
7330f34084fSMarius Strobl 	    !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50))
7340f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_DDR50;
7350f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_DDR52)
7360f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_DDR52;
7370f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 &&
7380f34084fSMarius Strobl 	    caps2 & SDHCI_CAN_MMC_HS400)
7390f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
7400f34084fSMarius Strobl 	host_caps |= MMC_CAP_SIGNALING_330;
7410f34084fSMarius Strobl 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
7420f34084fSMarius Strobl 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50 |
7430f34084fSMarius Strobl 	    MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 |
7440f34084fSMarius Strobl 	    MMC_CAP_MMC_HS400_180))
7450f34084fSMarius Strobl 		host_caps |= MMC_CAP_SIGNALING_180;
746f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_A)
7470f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_A;
748f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_C)
7490f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_C;
750f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_D)
7510f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_D;
7520f34084fSMarius Strobl 	slot->host.caps = host_caps;
7530f34084fSMarius Strobl 
754831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
755831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
756831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
757d6b3aaf8SOleksandr Tymoshenko 
758d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
759831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
760d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
761831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
762a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE)
763a2832f9fSMarius Strobl 		slot->opt |= SDHCI_NON_REMOVABLE;
764831f5dcfSAlexander Motin 
765c3a0f75aSOleksandr Tymoshenko 	/*
766c3a0f75aSOleksandr Tymoshenko 	 * Use platform-provided transfer backend
767c3a0f75aSOleksandr Tymoshenko 	 * with PIO as a fallback mechanism
768c3a0f75aSOleksandr Tymoshenko 	 */
769c3a0f75aSOleksandr Tymoshenko 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
770c3a0f75aSOleksandr Tymoshenko 		slot->opt &= ~SDHCI_HAVE_DMA;
771c3a0f75aSOleksandr Tymoshenko 
7725b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
7730f34084fSMarius Strobl 		slot_printf(slot,
7740f34084fSMarius Strobl 		    "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s\n",
775831f5dcfSAlexander Motin 		    slot->max_clk / 1000000,
776831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
7770f34084fSMarius Strobl 		    (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
7780f34084fSMarius Strobl 			((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
779831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
780831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
781831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "",
7820f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "",
7830f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "",
784f8b883c1SImre Vadász 		    (caps2 & SDHCI_CAN_DRIVE_TYPE_A) ? "A" : "",
785f8b883c1SImre Vadász 		    (caps2 & SDHCI_CAN_DRIVE_TYPE_C) ? "C" : "",
786f8b883c1SImre Vadász 		    (caps2 & SDHCI_CAN_DRIVE_TYPE_D) ? "D" : "",
787831f5dcfSAlexander Motin 		    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO");
7880f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 |
7890f34084fSMarius Strobl 		    MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE))
7900f34084fSMarius Strobl 			slot_printf(slot, "eMMC:%s%s%s%s\n",
7910f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "",
7920f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "",
7930f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "",
7940f34084fSMarius Strobl 			    ((host_caps &
7950f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ==
7960f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ?
7970f34084fSMarius Strobl 			    " HS400ES" : "");
7980f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
7990f34084fSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104))
8000f34084fSMarius Strobl 			slot_printf(slot, "UHS-I:%s%s%s%s%s\n",
8010f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "",
8020f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "",
8030f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "",
8040f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "",
8050f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : "");
806831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
807831f5dcfSAlexander Motin 	}
808831f5dcfSAlexander Motin 
809ba6fc1c7SLuiz Otavio O Souza 	slot->timeout = 10;
810ba6fc1c7SLuiz Otavio O Souza 	SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
811ba6fc1c7SLuiz Otavio O Souza 	    SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
812ba6fc1c7SLuiz Otavio O Souza 	    "timeout", CTLFLAG_RW, &slot->timeout, 0,
813ba6fc1c7SLuiz Otavio O Souza 	    "Maximum timeout for SDHCI transfers (in secs)");
814831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
815639f59f0SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
816639f59f0SIan Lepore 		sdhci_card_task, slot);
817639f59f0SIan Lepore 	callout_init(&slot->card_poll_callout, 1);
818e64f01a9SIan Lepore 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
819ba6fc1c7SLuiz Otavio O Souza 
820639f59f0SIan Lepore 	if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
821639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
822639f59f0SIan Lepore 		callout_reset(&slot->card_poll_callout,
823639f59f0SIan Lepore 		    SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
824639f59f0SIan Lepore 	}
825639f59f0SIan Lepore 
826831f5dcfSAlexander Motin 	return (0);
827831f5dcfSAlexander Motin }
828831f5dcfSAlexander Motin 
829d6b3aaf8SOleksandr Tymoshenko void
830d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
831831f5dcfSAlexander Motin {
8327e6ccea3SMarius Strobl 
833d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
834d6b3aaf8SOleksandr Tymoshenko }
835831f5dcfSAlexander Motin 
836d6b3aaf8SOleksandr Tymoshenko int
837d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
838d6b3aaf8SOleksandr Tymoshenko {
839831f5dcfSAlexander Motin 	device_t d;
840831f5dcfSAlexander Motin 
841e64f01a9SIan Lepore 	callout_drain(&slot->timeout_callout);
842639f59f0SIan Lepore 	callout_drain(&slot->card_poll_callout);
843831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
844639f59f0SIan Lepore 	taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
845831f5dcfSAlexander Motin 
846831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
847831f5dcfSAlexander Motin 	d = slot->dev;
848831f5dcfSAlexander Motin 	slot->dev = NULL;
849831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
850831f5dcfSAlexander Motin 	if (d != NULL)
851d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
852831f5dcfSAlexander Motin 
853831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
854831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
855831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
856831f5dcfSAlexander Motin 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
857831f5dcfSAlexander Motin 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
858831f5dcfSAlexander Motin 	bus_dma_tag_destroy(slot->dmatag);
859d6b3aaf8SOleksandr Tymoshenko 
860831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
861d6b3aaf8SOleksandr Tymoshenko 
862831f5dcfSAlexander Motin 	return (0);
863831f5dcfSAlexander Motin }
864831f5dcfSAlexander Motin 
865d6b3aaf8SOleksandr Tymoshenko int
866d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
86792bf0e27SAlexander Motin {
8687e6ccea3SMarius Strobl 
869d6b3aaf8SOleksandr Tymoshenko 	sdhci_reset(slot, SDHCI_RESET_ALL);
87092bf0e27SAlexander Motin 
87192bf0e27SAlexander Motin 	return (0);
87292bf0e27SAlexander Motin }
87392bf0e27SAlexander Motin 
874d6b3aaf8SOleksandr Tymoshenko int
875d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
87692bf0e27SAlexander Motin {
8777e6ccea3SMarius Strobl 
878d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
87992bf0e27SAlexander Motin 
880d6b3aaf8SOleksandr Tymoshenko 	return (0);
88192bf0e27SAlexander Motin }
88292bf0e27SAlexander Motin 
88357677a3aSOleksandr Tymoshenko uint32_t
884b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot)
88557677a3aSOleksandr Tymoshenko {
8867e6ccea3SMarius Strobl 
88757677a3aSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
88857677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
88957677a3aSOleksandr Tymoshenko 	else
89057677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
89157677a3aSOleksandr Tymoshenko }
89257677a3aSOleksandr Tymoshenko 
8936e37fb2bSIan Lepore bool
894b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot)
8956e37fb2bSIan Lepore {
8966e37fb2bSIan Lepore 
897639f59f0SIan Lepore 	if (slot->opt & SDHCI_NON_REMOVABLE)
898639f59f0SIan Lepore 		return true;
899639f59f0SIan Lepore 
9006e37fb2bSIan Lepore 	return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
9016e37fb2bSIan Lepore }
9026e37fb2bSIan Lepore 
9030f34084fSMarius Strobl void
9040f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot)
9050f34084fSMarius Strobl {
9060f34084fSMarius Strobl 	struct mmc_ios *ios;
9070f34084fSMarius Strobl 	uint16_t hostctrl2;
9080f34084fSMarius Strobl 
9090f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
9100f34084fSMarius Strobl 		return;
9110f34084fSMarius Strobl 
9120f34084fSMarius Strobl 	ios = &slot->host.ios;
9130f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
9140f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
9150f34084fSMarius Strobl 	hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK;
9160f34084fSMarius Strobl 	if (ios->timing == bus_timing_mmc_hs400 ||
9170f34084fSMarius Strobl 	    ios->timing == bus_timing_mmc_hs400es)
9180f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_MMC_HS400;
9190f34084fSMarius Strobl 	else if (ios->clock > SD_SDR50_MAX)
9200f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR104;
9210f34084fSMarius Strobl 	else if (ios->clock > SD_SDR25_MAX)
9220f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR50;
9230f34084fSMarius Strobl 	else if (ios->clock > SD_SDR12_MAX) {
9240f34084fSMarius Strobl 		if (ios->timing == bus_timing_uhs_ddr50 ||
9250f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_ddr52)
9260f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_DDR50;
9270f34084fSMarius Strobl 		else
9280f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR25;
9290f34084fSMarius Strobl 	} else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY)
9300f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR12;
9310f34084fSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
9320f34084fSMarius Strobl 	sdhci_set_clock(slot, ios->clock);
9330f34084fSMarius Strobl }
9340f34084fSMarius Strobl 
935d6b3aaf8SOleksandr Tymoshenko int
936d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
937831f5dcfSAlexander Motin {
938831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
939831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
940831f5dcfSAlexander Motin 
941831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
942831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
943831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
944831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
945831f5dcfSAlexander Motin 		sdhci_init(slot);
946831f5dcfSAlexander Motin 	}
947831f5dcfSAlexander Motin 	/* Configure the bus. */
948831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
949831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
9502d1731b8SIan Lepore 	if (ios->bus_width == bus_width_8) {
9512d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
952831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
9532d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_4) {
9542d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
9552d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
9562d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_1) {
9572d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
9582d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
9592d1731b8SIan Lepore 	} else {
9602d1731b8SIan Lepore 		panic("Invalid bus width: %d", ios->bus_width);
9612d1731b8SIan Lepore 	}
9620f34084fSMarius Strobl 	if (ios->clock > SD_SDR12_MAX &&
963bba987dcSIan Lepore 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
964831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
965831f5dcfSAlexander Motin 	else
966831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
967831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
9680f34084fSMarius Strobl 	SDHCI_SET_UHS_TIMING(brdev, slot);
969831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
970d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
971831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
972831f5dcfSAlexander Motin 
973831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
974831f5dcfSAlexander Motin 	return (0);
975831f5dcfSAlexander Motin }
976831f5dcfSAlexander Motin 
9770f34084fSMarius Strobl int
9780f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev)
9790f34084fSMarius Strobl {
9800f34084fSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
9810f34084fSMarius Strobl 	enum mmc_vccq vccq;
9820f34084fSMarius Strobl 	int err;
9830f34084fSMarius Strobl 	uint16_t hostctrl2;
9840f34084fSMarius Strobl 
9850f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
9860f34084fSMarius Strobl 		return (0);
9870f34084fSMarius Strobl 
9880f34084fSMarius Strobl 	err = 0;
9890f34084fSMarius Strobl 	vccq = slot->host.ios.vccq;
9900f34084fSMarius Strobl 	SDHCI_LOCK(slot);
9910f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
9920f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
9930f34084fSMarius Strobl 	switch (vccq) {
9940f34084fSMarius Strobl 	case vccq_330:
9950f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
9960f34084fSMarius Strobl 			goto done;
9970f34084fSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE;
9980f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
9990f34084fSMarius Strobl 		DELAY(5000);
10000f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
10010f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
10020f34084fSMarius Strobl 			goto done;
10030f34084fSMarius Strobl 		err = EAGAIN;
10040f34084fSMarius Strobl 		break;
10050f34084fSMarius Strobl 	case vccq_180:
10060f34084fSMarius Strobl 		if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) {
10070f34084fSMarius Strobl 			err = EINVAL;
10080f34084fSMarius Strobl 			goto done;
10090f34084fSMarius Strobl 		}
10100f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
10110f34084fSMarius Strobl 			goto done;
10120f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_S18_ENABLE;
10130f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
10140f34084fSMarius Strobl 		DELAY(5000);
10150f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
10160f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
10170f34084fSMarius Strobl 			goto done;
10180f34084fSMarius Strobl 		err = EAGAIN;
10190f34084fSMarius Strobl 		break;
10200f34084fSMarius Strobl 	default:
10210f34084fSMarius Strobl 		slot_printf(slot,
10220f34084fSMarius Strobl 		    "Attempt to set unsupported signaling voltage\n");
10230f34084fSMarius Strobl 		err = EINVAL;
10240f34084fSMarius Strobl 		break;
10250f34084fSMarius Strobl 	}
10260f34084fSMarius Strobl done:
10270f34084fSMarius Strobl 	sdhci_set_clock(slot, slot->host.ios.clock);
10280f34084fSMarius Strobl 	SDHCI_UNLOCK(slot);
10290f34084fSMarius Strobl 	return (err);
10300f34084fSMarius Strobl }
10310f34084fSMarius Strobl 
1032831f5dcfSAlexander Motin static void
1033e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot)
1034e64f01a9SIan Lepore {
1035e64f01a9SIan Lepore 	struct mmc_request *req;
1036e64f01a9SIan Lepore 
1037e64f01a9SIan Lepore 	if (slot->req != NULL && slot->curcmd != NULL) {
1038e64f01a9SIan Lepore 		callout_stop(&slot->timeout_callout);
1039e64f01a9SIan Lepore 		req = slot->req;
1040e64f01a9SIan Lepore 		slot->req = NULL;
1041e64f01a9SIan Lepore 		slot->curcmd = NULL;
1042e64f01a9SIan Lepore 		req->done(req);
1043e64f01a9SIan Lepore 	}
1044e64f01a9SIan Lepore }
1045e64f01a9SIan Lepore 
1046e64f01a9SIan Lepore static void
1047e64f01a9SIan Lepore sdhci_timeout(void *arg)
1048e64f01a9SIan Lepore {
1049e64f01a9SIan Lepore 	struct sdhci_slot *slot = arg;
1050e64f01a9SIan Lepore 
1051e64f01a9SIan Lepore 	if (slot->curcmd != NULL) {
10527e586643SIan Lepore 		slot_printf(slot, " Controller timeout\n");
10537e586643SIan Lepore 		sdhci_dumpregs(slot);
1054a6873fd1SIan Lepore 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1055e64f01a9SIan Lepore 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1056e64f01a9SIan Lepore 		sdhci_req_done(slot);
10577e586643SIan Lepore 	} else {
10587e586643SIan Lepore 		slot_printf(slot, " Spurious timeout - no active command\n");
1059e64f01a9SIan Lepore 	}
1060e64f01a9SIan Lepore }
1061e64f01a9SIan Lepore 
1062e64f01a9SIan Lepore static void
1063b440e965SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, struct mmc_data *data)
1064831f5dcfSAlexander Motin {
1065831f5dcfSAlexander Motin 	uint16_t mode;
1066831f5dcfSAlexander Motin 
1067831f5dcfSAlexander Motin 	if (data == NULL)
1068831f5dcfSAlexander Motin 		return;
1069831f5dcfSAlexander Motin 
1070831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
1071831f5dcfSAlexander Motin 	if (data->len > 512)
1072831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
1073831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
1074831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
1075915780d7SLuiz Otavio O Souza 	if (slot->req->stop && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))
1076831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_ACMD12;
1077831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
1078831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
1079831f5dcfSAlexander Motin 
1080831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
1081831f5dcfSAlexander Motin }
1082831f5dcfSAlexander Motin 
1083831f5dcfSAlexander Motin static void
1084831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
1085831f5dcfSAlexander Motin {
1086831f5dcfSAlexander Motin 	int flags, timeout;
108790993663SIan Lepore 	uint32_t mask;
1088831f5dcfSAlexander Motin 
1089831f5dcfSAlexander Motin 	slot->curcmd = cmd;
1090831f5dcfSAlexander Motin 	slot->cmd_done = 0;
1091831f5dcfSAlexander Motin 
1092831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
1093831f5dcfSAlexander Motin 
1094831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
1095831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1096831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
1097831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1098e64f01a9SIan Lepore 		sdhci_req_done(slot);
1099831f5dcfSAlexander Motin 		return;
1100831f5dcfSAlexander Motin 	}
1101831f5dcfSAlexander Motin 
1102b440e965SMarius Strobl 	/*
1103b440e965SMarius Strobl 	 * Do not issue command if there is no card, clock or power.
1104b440e965SMarius Strobl 	 * Controller will not detect timeout without clock active.
1105b440e965SMarius Strobl 	 */
11066e37fb2bSIan Lepore 	if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
1107d8208d9eSAlexander Motin 	    slot->power == 0 ||
1108d8208d9eSAlexander Motin 	    slot->clock == 0) {
1109831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1110e64f01a9SIan Lepore 		sdhci_req_done(slot);
1111831f5dcfSAlexander Motin 		return;
1112831f5dcfSAlexander Motin 	}
1113831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
1114831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
1115831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
1116831f5dcfSAlexander Motin 	if (cmd->data || (cmd->flags & MMC_RSP_BUSY))
1117831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
1118831f5dcfSAlexander Motin 	/* We shouldn't wait for DAT for stop commands. */
1119831f5dcfSAlexander Motin 	if (cmd == slot->req->stop)
1120831f5dcfSAlexander Motin 		mask &= ~SDHCI_DAT_INHIBIT;
11218775ab45SIan Lepore 	/*
11228775ab45SIan Lepore 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
11238775ab45SIan Lepore 	 *  here at all, but when writing a crash dump we may be bypassing the
11248775ab45SIan Lepore 	 *  host platform's interrupt handler, and in some cases that handler
11258775ab45SIan Lepore 	 *  may be working around hardware quirks such as not respecting r1b
11268775ab45SIan Lepore 	 *  busy indications.  In those cases, this wait-loop serves the purpose
11278775ab45SIan Lepore 	 *  of waiting for the prior command and data transfers to be done, and
11288775ab45SIan Lepore 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
11298775ab45SIan Lepore 	 *  (It's usually more like 20-30ms in the real world.)
11308775ab45SIan Lepore 	 */
11318775ab45SIan Lepore 	timeout = 250;
113290993663SIan Lepore 	while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
1133831f5dcfSAlexander Motin 		if (timeout == 0) {
1134831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
1135831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
1136831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
1137831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
1138e64f01a9SIan Lepore 			sdhci_req_done(slot);
1139831f5dcfSAlexander Motin 			return;
1140831f5dcfSAlexander Motin 		}
1141831f5dcfSAlexander Motin 		timeout--;
1142831f5dcfSAlexander Motin 		DELAY(1000);
1143831f5dcfSAlexander Motin 	}
1144831f5dcfSAlexander Motin 
1145831f5dcfSAlexander Motin 	/* Prepare command flags. */
1146831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
1147831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
1148831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
1149831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
1150831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
1151831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
1152831f5dcfSAlexander Motin 	else
1153831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
1154831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
1155831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
1156831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
1157831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
1158831f5dcfSAlexander Motin 	if (cmd->data)
1159831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
1160831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
1161831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
1162831f5dcfSAlexander Motin 	/* Prepare data. */
1163831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
1164831f5dcfSAlexander Motin 	/*
1165831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
1166831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
1167831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
1168831f5dcfSAlexander Motin 	 */
1169831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
1170831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1171831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
1172831f5dcfSAlexander Motin 	}
1173831f5dcfSAlexander Motin 	/* Set command argument. */
1174831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
1175831f5dcfSAlexander Motin 	/* Set data transfer mode. */
1176831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
1177831f5dcfSAlexander Motin 	/* Start command. */
1178d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
1179a6873fd1SIan Lepore 	/* Start timeout callout. */
1180ba6fc1c7SLuiz Otavio O Souza 	callout_reset(&slot->timeout_callout, slot->timeout * hz,
1181ba6fc1c7SLuiz Otavio O Souza 	    sdhci_timeout, slot);
1182831f5dcfSAlexander Motin }
1183831f5dcfSAlexander Motin 
1184831f5dcfSAlexander Motin static void
1185831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
1186831f5dcfSAlexander Motin {
1187831f5dcfSAlexander Motin 	int i;
11881bacf3beSMarius Strobl 	uint32_t val;
11891bacf3beSMarius Strobl 	uint8_t extra;
1190831f5dcfSAlexander Motin 
1191831f5dcfSAlexander Motin 	slot->cmd_done = 1;
119272dec079SMarius Strobl 	/*
119372dec079SMarius Strobl 	 * Interrupt aggregation: Restore command interrupt.
1194831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
119572dec079SMarius Strobl 	 * happened first.
119672dec079SMarius Strobl 	 */
1197831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE);
1198831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
1199831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1200831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1201831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1202831f5dcfSAlexander Motin 		sdhci_start(slot);
1203831f5dcfSAlexander Motin 		return;
1204831f5dcfSAlexander Motin 	}
1205831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
1206831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1207831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
1208831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
12091bacf3beSMarius Strobl 			extra = 0;
1210831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
12111bacf3beSMarius Strobl 				val = RD4(slot, SDHCI_RESPONSE + i * 4);
12121bacf3beSMarius Strobl 				if (slot->quirks &
12131bacf3beSMarius Strobl 				    SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1214677ee494SIan Lepore 					slot->curcmd->resp[3 - i] = val;
1215677ee494SIan Lepore 				else {
1216677ee494SIan Lepore 					slot->curcmd->resp[3 - i] =
1217677ee494SIan Lepore 					    (val << 8) | extra;
1218831f5dcfSAlexander Motin 					extra = val >> 24;
1219831f5dcfSAlexander Motin 				}
1220677ee494SIan Lepore 			}
1221831f5dcfSAlexander Motin 		} else
1222831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1223831f5dcfSAlexander Motin 	}
1224831f5dcfSAlexander Motin 	/* If data ready - finish. */
1225831f5dcfSAlexander Motin 	if (slot->data_done)
1226831f5dcfSAlexander Motin 		sdhci_start(slot);
1227831f5dcfSAlexander Motin }
1228831f5dcfSAlexander Motin 
1229831f5dcfSAlexander Motin static void
1230831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data)
1231831f5dcfSAlexander Motin {
1232831f5dcfSAlexander Motin 	uint32_t target_timeout, current_timeout;
1233831f5dcfSAlexander Motin 	uint8_t div;
1234831f5dcfSAlexander Motin 
1235831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1236831f5dcfSAlexander Motin 		slot->data_done = 1;
1237831f5dcfSAlexander Motin 		return;
1238831f5dcfSAlexander Motin 	}
1239831f5dcfSAlexander Motin 
1240831f5dcfSAlexander Motin 	slot->data_done = 0;
1241831f5dcfSAlexander Motin 
1242831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
1243831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
1244ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1245ceb9e9f7SIan Lepore 		div = 0xE;
1246ceb9e9f7SIan Lepore 	} else {
1247831f5dcfSAlexander Motin 		target_timeout = 1000000;
1248831f5dcfSAlexander Motin 		div = 0;
1249831f5dcfSAlexander Motin 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1250ceb9e9f7SIan Lepore 		while (current_timeout < target_timeout && div < 0xE) {
1251ceb9e9f7SIan Lepore 			++div;
1252831f5dcfSAlexander Motin 			current_timeout <<= 1;
1253831f5dcfSAlexander Motin 		}
1254831f5dcfSAlexander Motin 		/* Compensate for an off-by-one error in the CaFe chip.*/
1255ceb9e9f7SIan Lepore 		if (div < 0xE &&
1256ceb9e9f7SIan Lepore 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1257ceb9e9f7SIan Lepore 			++div;
1258831f5dcfSAlexander Motin 		}
1259ceb9e9f7SIan Lepore 	}
1260831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1261831f5dcfSAlexander Motin 
1262831f5dcfSAlexander Motin 	if (data == NULL)
1263831f5dcfSAlexander Motin 		return;
1264831f5dcfSAlexander Motin 
1265831f5dcfSAlexander Motin 	/* Use DMA if possible. */
1266831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
1267831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
1268831f5dcfSAlexander Motin 	/* If data is small, broken DMA may return zeroes instead of data, */
1269d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1270831f5dcfSAlexander Motin 	    (data->len <= 512))
1271831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1272831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
1273d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1274831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
1275831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1276831f5dcfSAlexander Motin 	/* Load DMA buffer. */
1277831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
1278831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
1279ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1280ecc2d997SRui Paulo 			    BUS_DMASYNC_PREREAD);
1281831f5dcfSAlexander Motin 		else {
1282831f5dcfSAlexander Motin 			memcpy(slot->dmamem, data->data,
1283ecc2d997SRui Paulo 			    (data->len < DMA_BLOCK_SIZE) ?
1284ecc2d997SRui Paulo 			    data->len : DMA_BLOCK_SIZE);
1285ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1286ecc2d997SRui Paulo 			    BUS_DMASYNC_PREWRITE);
1287831f5dcfSAlexander Motin 		}
1288831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1289831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1290831f5dcfSAlexander Motin 		 * for the last page and unmask else. */
1291831f5dcfSAlexander Motin 		if (data->len == DMA_BLOCK_SIZE)
1292831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1293831f5dcfSAlexander Motin 		else
1294831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
1295831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1296831f5dcfSAlexander Motin 	}
1297831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
1298831f5dcfSAlexander Motin 	slot->offset = 0;
1299831f5dcfSAlexander Motin 	/* Set block size and request IRQ on 4K border. */
13001bacf3beSMarius Strobl 	WR2(slot, SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(DMA_BOUNDARY,
13011bacf3beSMarius Strobl 	    (data->len < 512) ? data->len : 512));
1302831f5dcfSAlexander Motin 	/* Set block count. */
1303831f5dcfSAlexander Motin 	WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
1304831f5dcfSAlexander Motin }
1305831f5dcfSAlexander Motin 
1306c3a0f75aSOleksandr Tymoshenko void
1307831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
1308831f5dcfSAlexander Motin {
1309831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
13107e6ccea3SMarius Strobl 	size_t left;
1311831f5dcfSAlexander Motin 
1312831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
1313ecc2d997SRui Paulo 	 * Auxiliary restore point for the case when data interrupt
1314831f5dcfSAlexander Motin 	 * happened first. */
1315831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
1316831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1317831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
1318831f5dcfSAlexander Motin 	}
1319831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
1320915780d7SLuiz Otavio O Souza 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) &&
1321915780d7SLuiz Otavio O Souza 	    slot->curcmd->data != NULL) {
1322831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
13237e6ccea3SMarius Strobl 			left = data->len - slot->offset;
1324ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1325ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTREAD);
1326831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1327831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
1328831f5dcfSAlexander Motin 		} else
1329ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1330ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTWRITE);
1331831f5dcfSAlexander Motin 	}
1332a98788edSIan Lepore 	slot->data_done = 1;
1333831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
1334831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1335831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1336831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1337831f5dcfSAlexander Motin 		sdhci_start(slot);
1338831f5dcfSAlexander Motin 		return;
1339831f5dcfSAlexander Motin 	}
1340831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
1341831f5dcfSAlexander Motin 	if (slot->cmd_done)
1342831f5dcfSAlexander Motin 		sdhci_start(slot);
1343831f5dcfSAlexander Motin }
1344831f5dcfSAlexander Motin 
1345831f5dcfSAlexander Motin static void
1346831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
1347831f5dcfSAlexander Motin {
1348831f5dcfSAlexander Motin 	struct mmc_request *req;
1349831f5dcfSAlexander Motin 
1350831f5dcfSAlexander Motin 	req = slot->req;
1351831f5dcfSAlexander Motin 	if (req == NULL)
1352831f5dcfSAlexander Motin 		return;
1353831f5dcfSAlexander Motin 
1354831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
1355831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
1356831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
1357831f5dcfSAlexander Motin 		return;
1358831f5dcfSAlexander Motin 	}
1359915780d7SLuiz Otavio O Souza 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) &&
1360915780d7SLuiz Otavio O Souza 	    !(slot->flags & STOP_STARTED) && req->stop) {
1361831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
1362831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
1363831f5dcfSAlexander Motin 		return;
1364831f5dcfSAlexander Motin 	}
13655b69a497SAlexander Motin 	if (sdhci_debug > 1)
13665b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
13675b69a497SAlexander Motin 	if (!req->cmd->error &&
1368915780d7SLuiz Otavio O Souza 	    ((slot->curcmd == req->stop &&
1369915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) ||
1370915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
1371831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1372831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1373831f5dcfSAlexander Motin 	}
1374831f5dcfSAlexander Motin 
1375e64f01a9SIan Lepore 	sdhci_req_done(slot);
1376831f5dcfSAlexander Motin }
1377831f5dcfSAlexander Motin 
1378d6b3aaf8SOleksandr Tymoshenko int
1379b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev,
1380b440e965SMarius Strobl     struct mmc_request *req)
1381831f5dcfSAlexander Motin {
1382831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1383831f5dcfSAlexander Motin 
1384831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1385831f5dcfSAlexander Motin 	if (slot->req != NULL) {
1386831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1387831f5dcfSAlexander Motin 		return (EBUSY);
1388831f5dcfSAlexander Motin 	}
13895b69a497SAlexander Motin 	if (sdhci_debug > 1) {
13901bacf3beSMarius Strobl 		slot_printf(slot,
13911bacf3beSMarius Strobl 		    "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1392831f5dcfSAlexander Motin 		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
13935b69a497SAlexander Motin 		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
13945b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
13955b69a497SAlexander Motin 	}
1396831f5dcfSAlexander Motin 	slot->req = req;
1397831f5dcfSAlexander Motin 	slot->flags = 0;
1398831f5dcfSAlexander Motin 	sdhci_start(slot);
1399831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1400bea2dca2SAlexander Motin 	if (dumping) {
1401bea2dca2SAlexander Motin 		while (slot->req != NULL) {
1402d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
1403bea2dca2SAlexander Motin 			DELAY(10);
1404bea2dca2SAlexander Motin 		}
1405bea2dca2SAlexander Motin 	}
1406831f5dcfSAlexander Motin 	return (0);
1407831f5dcfSAlexander Motin }
1408831f5dcfSAlexander Motin 
1409d6b3aaf8SOleksandr Tymoshenko int
1410b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev)
1411831f5dcfSAlexander Motin {
1412831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1413831f5dcfSAlexander Motin 	uint32_t val;
1414831f5dcfSAlexander Motin 
1415831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1416831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
1417831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1418831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
1419831f5dcfSAlexander Motin }
1420831f5dcfSAlexander Motin 
1421d6b3aaf8SOleksandr Tymoshenko int
1422b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev)
1423831f5dcfSAlexander Motin {
1424831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1425831f5dcfSAlexander Motin 	int err = 0;
1426831f5dcfSAlexander Motin 
1427831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1428831f5dcfSAlexander Motin 	while (slot->bus_busy)
1429d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
1430831f5dcfSAlexander Motin 	slot->bus_busy++;
1431831f5dcfSAlexander Motin 	/* Activate led. */
1432831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
1433831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1434831f5dcfSAlexander Motin 	return (err);
1435831f5dcfSAlexander Motin }
1436831f5dcfSAlexander Motin 
1437d6b3aaf8SOleksandr Tymoshenko int
1438b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev)
1439831f5dcfSAlexander Motin {
1440831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1441831f5dcfSAlexander Motin 
1442831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1443831f5dcfSAlexander Motin 	/* Deactivate led. */
1444831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
1445831f5dcfSAlexander Motin 	slot->bus_busy--;
1446831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1447d493985aSAlexander Motin 	wakeup(slot);
1448831f5dcfSAlexander Motin 	return (0);
1449831f5dcfSAlexander Motin }
1450831f5dcfSAlexander Motin 
1451831f5dcfSAlexander Motin static void
1452831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
1453831f5dcfSAlexander Motin {
1454831f5dcfSAlexander Motin 
1455831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1456831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
1457831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1458831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1459831f5dcfSAlexander Motin 		return;
1460831f5dcfSAlexander Motin 	}
1461831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
1462831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1463831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
1464831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1465831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
1466831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
1467831f5dcfSAlexander Motin 
1468831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
1469831f5dcfSAlexander Motin }
1470831f5dcfSAlexander Motin 
1471831f5dcfSAlexander Motin static void
1472831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
1473831f5dcfSAlexander Motin {
14741bacf3beSMarius Strobl 	struct mmc_data *data;
14751bacf3beSMarius Strobl 	size_t left;
1476831f5dcfSAlexander Motin 
1477831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1478831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1479831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1480831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1481831f5dcfSAlexander Motin 		return;
1482831f5dcfSAlexander Motin 	}
1483831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
1484831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1485831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1486831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
1487831f5dcfSAlexander Motin 		    intmask);
1488831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1489831f5dcfSAlexander Motin 		return;
1490831f5dcfSAlexander Motin 	}
1491831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
1492831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1493acbaa69fSOleksandr Tymoshenko 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1494831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1495831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
1496831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
1497831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
1498831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1499831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
1500831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1501831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
1502831f5dcfSAlexander Motin 	}
1503831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1504831f5dcfSAlexander Motin 		/* No need to continue after any error. */
1505a98788edSIan Lepore 		goto done;
1506831f5dcfSAlexander Motin 	}
1507831f5dcfSAlexander Motin 
1508831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
1509c3a0f75aSOleksandr Tymoshenko 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
1510c3a0f75aSOleksandr Tymoshenko 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
1511c3a0f75aSOleksandr Tymoshenko 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
15121bacf3beSMarius Strobl 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot,
15131bacf3beSMarius Strobl 			    &intmask);
1514c3a0f75aSOleksandr Tymoshenko 			slot->flags |= PLATFORM_DATA_STARTED;
1515c3a0f75aSOleksandr Tymoshenko 		} else
1516831f5dcfSAlexander Motin 			sdhci_transfer_pio(slot);
1517c3a0f75aSOleksandr Tymoshenko 	}
1518831f5dcfSAlexander Motin 	/* Handle DMA border. */
1519831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
15201bacf3beSMarius Strobl 		data = slot->curcmd->data;
1521831f5dcfSAlexander Motin 
1522831f5dcfSAlexander Motin 		/* Unload DMA buffer ... */
1523831f5dcfSAlexander Motin 		left = data->len - slot->offset;
1524831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
1525831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1526831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
1527831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1528831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
1529831f5dcfSAlexander Motin 		} else {
1530831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1531831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
1532831f5dcfSAlexander Motin 		}
1533831f5dcfSAlexander Motin 		/* ... and reload it again. */
1534831f5dcfSAlexander Motin 		slot->offset += DMA_BLOCK_SIZE;
1535831f5dcfSAlexander Motin 		left = data->len - slot->offset;
1536831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
1537831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1538831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREREAD);
1539831f5dcfSAlexander Motin 		} else {
1540831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
1541831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)? left : DMA_BLOCK_SIZE);
1542831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1543831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
1544831f5dcfSAlexander Motin 		}
1545831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1546831f5dcfSAlexander Motin 		 * for the last page. */
1547831f5dcfSAlexander Motin 		if (left == DMA_BLOCK_SIZE) {
1548831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1549831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1550831f5dcfSAlexander Motin 		}
1551831f5dcfSAlexander Motin 		/* Restart DMA. */
1552831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1553831f5dcfSAlexander Motin 	}
1554831f5dcfSAlexander Motin 	/* We have got all data. */
1555c3a0f75aSOleksandr Tymoshenko 	if (intmask & SDHCI_INT_DATA_END) {
1556c3a0f75aSOleksandr Tymoshenko 		if (slot->flags & PLATFORM_DATA_STARTED) {
1557c3a0f75aSOleksandr Tymoshenko 			slot->flags &= ~PLATFORM_DATA_STARTED;
1558c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1559c3a0f75aSOleksandr Tymoshenko 		} else
1560831f5dcfSAlexander Motin 			sdhci_finish_data(slot);
1561831f5dcfSAlexander Motin 	}
1562a98788edSIan Lepore done:
1563a98788edSIan Lepore 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
1564a98788edSIan Lepore 		if (slot->flags & PLATFORM_DATA_STARTED) {
1565a98788edSIan Lepore 			slot->flags &= ~PLATFORM_DATA_STARTED;
1566a98788edSIan Lepore 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1567a98788edSIan Lepore 		} else
1568a98788edSIan Lepore 			sdhci_finish_data(slot);
1569a98788edSIan Lepore 	}
1570c3a0f75aSOleksandr Tymoshenko }
1571831f5dcfSAlexander Motin 
1572831f5dcfSAlexander Motin static void
1573831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot)
1574831f5dcfSAlexander Motin {
1575831f5dcfSAlexander Motin 	uint16_t err;
1576831f5dcfSAlexander Motin 
1577831f5dcfSAlexander Motin 	err = RD4(slot, SDHCI_ACMD12_ERR);
1578831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1579831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
1580831f5dcfSAlexander Motin 		    "there is no active command.\n", err);
1581831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1582831f5dcfSAlexander Motin 		return;
1583831f5dcfSAlexander Motin 	}
1584831f5dcfSAlexander Motin 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err);
1585831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_CMD);
1586831f5dcfSAlexander Motin }
1587831f5dcfSAlexander Motin 
1588d6b3aaf8SOleksandr Tymoshenko void
1589d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
1590831f5dcfSAlexander Motin {
15912b96b955SJustin Hibbits 	uint32_t intmask, present;
1592831f5dcfSAlexander Motin 
1593831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1594831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
1595831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
1596831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
1597831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1598d6b3aaf8SOleksandr Tymoshenko 		return;
1599831f5dcfSAlexander Motin 	}
16005b69a497SAlexander Motin 	if (sdhci_debug > 2)
16015b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
16025b69a497SAlexander Motin 
1603831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
1604831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1605639f59f0SIan Lepore 		present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
16062b96b955SJustin Hibbits 		slot->intmask &=
16072b96b955SJustin Hibbits 		    ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
16082b96b955SJustin Hibbits 		slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
16092b96b955SJustin Hibbits 		    SDHCI_INT_CARD_INSERT;
16102b96b955SJustin Hibbits 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
16112b96b955SJustin Hibbits 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1612831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
1613831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
1614b8bf08b1SIan Lepore 		sdhci_handle_card_present_locked(slot, present);
1615831f5dcfSAlexander Motin 		intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1616831f5dcfSAlexander Motin 	}
1617831f5dcfSAlexander Motin 	/* Handle command interrupts. */
1618831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
1619831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
1620831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
1621831f5dcfSAlexander Motin 	}
1622831f5dcfSAlexander Motin 	/* Handle data interrupts. */
1623831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
1624831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
16257e6ccea3SMarius Strobl 		/* Don't call data_irq in case of errored command. */
16267e586643SIan Lepore 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
1627831f5dcfSAlexander Motin 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
1628831f5dcfSAlexander Motin 	}
1629831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
1630831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
1631831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
1632831f5dcfSAlexander Motin 		sdhci_acmd_irq(slot);
1633831f5dcfSAlexander Motin 	}
1634831f5dcfSAlexander Motin 	intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1635831f5dcfSAlexander Motin 	intmask &= ~SDHCI_INT_ACMD12ERR;
1636831f5dcfSAlexander Motin 	intmask &= ~SDHCI_INT_ERROR;
1637831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
1638831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
1639831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
1640831f5dcfSAlexander Motin 		slot_printf(slot,
1641831f5dcfSAlexander Motin 		    "Card is consuming too much power!\n");
1642831f5dcfSAlexander Motin 		intmask &= ~SDHCI_INT_BUS_POWER;
1643831f5dcfSAlexander Motin 	}
1644831f5dcfSAlexander Motin 	/* The rest is unknown. */
1645831f5dcfSAlexander Motin 	if (intmask) {
1646831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
1647831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
1648831f5dcfSAlexander Motin 		    intmask);
1649831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1650831f5dcfSAlexander Motin 	}
1651831f5dcfSAlexander Motin 
1652831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1653831f5dcfSAlexander Motin }
1654831f5dcfSAlexander Motin 
1655d6b3aaf8SOleksandr Tymoshenko int
16561bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which,
16571bacf3beSMarius Strobl     uintptr_t *result)
1658831f5dcfSAlexander Motin {
1659831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
1660831f5dcfSAlexander Motin 
1661831f5dcfSAlexander Motin 	switch (which) {
1662831f5dcfSAlexander Motin 	default:
1663831f5dcfSAlexander Motin 		return (EINVAL);
1664831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
1665bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
1666831f5dcfSAlexander Motin 		break;
1667831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
1668bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
1669831f5dcfSAlexander Motin 		break;
1670831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
1671bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
1672831f5dcfSAlexander Motin 		break;
1673831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
1674bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
1675831f5dcfSAlexander Motin 		break;
1676831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
1677bcd91d25SJayachandran C. 		*result = slot->host.f_min;
1678831f5dcfSAlexander Motin 		break;
1679831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
1680bcd91d25SJayachandran C. 		*result = slot->host.f_max;
1681831f5dcfSAlexander Motin 		break;
1682831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
1683bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
1684831f5dcfSAlexander Motin 		break;
1685831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
1686bcd91d25SJayachandran C. 		*result = slot->host.mode;
1687831f5dcfSAlexander Motin 		break;
1688831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
1689bcd91d25SJayachandran C. 		*result = slot->host.ocr;
1690831f5dcfSAlexander Motin 		break;
1691831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
1692bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
1693831f5dcfSAlexander Motin 		break;
1694831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
1695bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
1696831f5dcfSAlexander Motin 		break;
16970f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
16980f34084fSMarius Strobl 		*result = slot->host.ios.vccq;
16990f34084fSMarius Strobl 		break;
1700831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
1701bcd91d25SJayachandran C. 		*result = slot->host.caps;
1702831f5dcfSAlexander Motin 		break;
1703831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
1704bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
1705831f5dcfSAlexander Motin 		break;
17063a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
1707bcd91d25SJayachandran C. 		*result = 65535;
17083a4a2557SAlexander Motin 		break;
170972dec079SMarius Strobl 	case MMCBR_IVAR_MAX_BUSY_TIMEOUT:
171072dec079SMarius Strobl 		/*
171172dec079SMarius Strobl 		 * Currently, sdhci_start_data() hardcodes 1 s for all CMDs.
171272dec079SMarius Strobl 		 */
171372dec079SMarius Strobl 		*result = 1000000;
171472dec079SMarius Strobl 		break;
1715831f5dcfSAlexander Motin 	}
1716831f5dcfSAlexander Motin 	return (0);
1717831f5dcfSAlexander Motin }
1718831f5dcfSAlexander Motin 
1719d6b3aaf8SOleksandr Tymoshenko int
17201bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which,
17211bacf3beSMarius Strobl     uintptr_t value)
1722831f5dcfSAlexander Motin {
1723831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
1724b440e965SMarius Strobl 	uint32_t clock, max_clock;
1725b440e965SMarius Strobl 	int i;
1726831f5dcfSAlexander Motin 
1727831f5dcfSAlexander Motin 	switch (which) {
1728831f5dcfSAlexander Motin 	default:
1729831f5dcfSAlexander Motin 		return (EINVAL);
1730831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
1731831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
1732831f5dcfSAlexander Motin 		break;
1733831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
1734831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
1735831f5dcfSAlexander Motin 		break;
1736831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
1737831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
1738831f5dcfSAlexander Motin 		break;
1739831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
1740831f5dcfSAlexander Motin 		if (value > 0) {
174157677a3aSOleksandr Tymoshenko 			max_clock = slot->max_clk;
174257677a3aSOleksandr Tymoshenko 			clock = max_clock;
174357677a3aSOleksandr Tymoshenko 
174457677a3aSOleksandr Tymoshenko 			if (slot->version < SDHCI_SPEC_300) {
174557677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
174657677a3aSOleksandr Tymoshenko 				    i <<= 1) {
1747831f5dcfSAlexander Motin 					if (clock <= value)
1748831f5dcfSAlexander Motin 						break;
1749831f5dcfSAlexander Motin 					clock >>= 1;
1750831f5dcfSAlexander Motin 				}
1751b440e965SMarius Strobl 			} else {
175257677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
175357677a3aSOleksandr Tymoshenko 				    i += 2) {
175457677a3aSOleksandr Tymoshenko 					if (clock <= value)
175557677a3aSOleksandr Tymoshenko 						break;
175657677a3aSOleksandr Tymoshenko 					clock = max_clock / (i + 2);
175757677a3aSOleksandr Tymoshenko 				}
175857677a3aSOleksandr Tymoshenko 			}
175957677a3aSOleksandr Tymoshenko 
1760831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
1761831f5dcfSAlexander Motin 		} else
1762831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
1763831f5dcfSAlexander Motin 		break;
1764831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
1765831f5dcfSAlexander Motin 		slot->host.mode = value;
1766831f5dcfSAlexander Motin 		break;
1767831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
1768831f5dcfSAlexander Motin 		slot->host.ocr = value;
1769831f5dcfSAlexander Motin 		break;
1770831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
1771831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
1772831f5dcfSAlexander Motin 		break;
1773831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
1774831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
1775831f5dcfSAlexander Motin 		break;
17760f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
17770f34084fSMarius Strobl 		slot->host.ios.vccq = value;
17780f34084fSMarius Strobl 		break;
1779831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
1780831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
1781831f5dcfSAlexander Motin 		break;
1782831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
1783831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
1784831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
1785831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
17863a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
1787831f5dcfSAlexander Motin 		return (EINVAL);
1788831f5dcfSAlexander Motin 	}
1789831f5dcfSAlexander Motin 	return (0);
1790831f5dcfSAlexander Motin }
1791831f5dcfSAlexander Motin 
1792d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1);
1793