xref: /freebsd/sys/dev/sdhci/sdhci.c (revision 915780d7642db2677a54d56cc3cfbbfe3f849b6a)
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 {
369831f5dcfSAlexander Motin 	uint8_t pwr;
370831f5dcfSAlexander Motin 
371831f5dcfSAlexander Motin 	if (slot->power == power)
372831f5dcfSAlexander Motin 		return;
373d6b3aaf8SOleksandr Tymoshenko 
374831f5dcfSAlexander Motin 	slot->power = power;
375831f5dcfSAlexander Motin 
376831f5dcfSAlexander Motin 	/* Turn off the power. */
377831f5dcfSAlexander Motin 	pwr = 0;
378831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
379b440e965SMarius Strobl 	/* If power down requested - leave it so. */
380831f5dcfSAlexander Motin 	if (power == 0)
381831f5dcfSAlexander Motin 		return;
382831f5dcfSAlexander Motin 	/* Set voltage. */
383831f5dcfSAlexander Motin 	switch (1 << power) {
384831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
385831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
386831f5dcfSAlexander Motin 		break;
387831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
388831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
389831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
390831f5dcfSAlexander Motin 		break;
391831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
392831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
393831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
394831f5dcfSAlexander Motin 		break;
395831f5dcfSAlexander Motin 	}
396831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
397831f5dcfSAlexander Motin 	/* Turn on the power. */
398831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
399831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
400a2832f9fSMarius Strobl 
401a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) {
402a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10);
403a2832f9fSMarius Strobl 		DELAY(10);
404a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
405a2832f9fSMarius Strobl 		DELAY(300);
406a2832f9fSMarius Strobl 	}
407831f5dcfSAlexander Motin }
408831f5dcfSAlexander Motin 
409831f5dcfSAlexander Motin static void
410831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
411831f5dcfSAlexander Motin {
412831f5dcfSAlexander Motin 	uint32_t data;
413831f5dcfSAlexander Motin 	char *buffer;
414831f5dcfSAlexander Motin 	size_t left;
415831f5dcfSAlexander Motin 
416831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
417831f5dcfSAlexander Motin 	buffer += slot->offset;
418831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
419831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
420831f5dcfSAlexander Motin 	slot->offset += left;
421831f5dcfSAlexander Motin 
422831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
423d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
424831f5dcfSAlexander Motin 		DELAY(10);
425ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
426831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
427831f5dcfSAlexander Motin 		while (left > 3) {
428831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
429831f5dcfSAlexander Motin 			buffer[0] = data;
430831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
431831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
432831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
433831f5dcfSAlexander Motin 			buffer += 4;
434831f5dcfSAlexander Motin 			left -= 4;
435831f5dcfSAlexander Motin 		}
436831f5dcfSAlexander Motin 	} else {
437d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
438831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
439831f5dcfSAlexander Motin 		left &= 3;
440831f5dcfSAlexander Motin 	}
441831f5dcfSAlexander Motin 	/* Handle uneven size case. */
442831f5dcfSAlexander Motin 	if (left > 0) {
443831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
444831f5dcfSAlexander Motin 		while (left > 0) {
445831f5dcfSAlexander Motin 			*(buffer++) = data;
446831f5dcfSAlexander Motin 			data >>= 8;
447831f5dcfSAlexander Motin 			left--;
448831f5dcfSAlexander Motin 		}
449831f5dcfSAlexander Motin 	}
450831f5dcfSAlexander Motin }
451831f5dcfSAlexander Motin 
452831f5dcfSAlexander Motin static void
453831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
454831f5dcfSAlexander Motin {
455831f5dcfSAlexander Motin 	uint32_t data = 0;
456831f5dcfSAlexander Motin 	char *buffer;
457831f5dcfSAlexander Motin 	size_t left;
458831f5dcfSAlexander Motin 
459831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
460831f5dcfSAlexander Motin 	buffer += slot->offset;
461831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
462831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
463831f5dcfSAlexander Motin 	slot->offset += left;
464831f5dcfSAlexander Motin 
465ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
466831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
467831f5dcfSAlexander Motin 		while (left > 3) {
468831f5dcfSAlexander Motin 			data = buffer[0] +
469831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
470831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
471831f5dcfSAlexander Motin 			    (buffer[3] << 24);
472831f5dcfSAlexander Motin 			left -= 4;
473831f5dcfSAlexander Motin 			buffer += 4;
474831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
475831f5dcfSAlexander Motin 		}
476831f5dcfSAlexander Motin 	} else {
477d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
478831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
479831f5dcfSAlexander Motin 		left &= 3;
480831f5dcfSAlexander Motin 	}
481831f5dcfSAlexander Motin 	/* Handle uneven size case. */
482831f5dcfSAlexander Motin 	if (left > 0) {
483831f5dcfSAlexander Motin 		while (left > 0) {
484831f5dcfSAlexander Motin 			data <<= 8;
485831f5dcfSAlexander Motin 			data += *(buffer++);
486831f5dcfSAlexander Motin 			left--;
487831f5dcfSAlexander Motin 		}
488831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
489831f5dcfSAlexander Motin 	}
490831f5dcfSAlexander Motin }
491831f5dcfSAlexander Motin 
492831f5dcfSAlexander Motin static void
493831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
494831f5dcfSAlexander Motin {
495831f5dcfSAlexander Motin 
496831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
497831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
498831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
499831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
500831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
501831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
502831f5dcfSAlexander Motin 				break;
503831f5dcfSAlexander Motin 		}
504831f5dcfSAlexander Motin 	} else {
505831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
506831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
507831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
508831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
509831f5dcfSAlexander Motin 				break;
510831f5dcfSAlexander Motin 		}
511831f5dcfSAlexander Motin 	}
512831f5dcfSAlexander Motin }
513831f5dcfSAlexander Motin 
514831f5dcfSAlexander Motin static void
5157e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused)
516831f5dcfSAlexander Motin {
517831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
5187e6ccea3SMarius Strobl 	device_t d;
519831f5dcfSAlexander Motin 
520831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
5216e37fb2bSIan Lepore 	if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
522831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
523831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
524639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
525639f59f0SIan Lepore 				slot_printf(slot, "Card inserted\n");
526d6b3aaf8SOleksandr Tymoshenko 			slot->dev = device_add_child(slot->bus, "mmc", -1);
527831f5dcfSAlexander Motin 			device_set_ivars(slot->dev, slot);
528831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
529831f5dcfSAlexander Motin 			device_probe_and_attach(slot->dev);
530831f5dcfSAlexander Motin 		} else
531831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
532831f5dcfSAlexander Motin 	} else {
533831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
534831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
535639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
536639f59f0SIan Lepore 				slot_printf(slot, "Card removed\n");
5377e6ccea3SMarius Strobl 			d = slot->dev;
538831f5dcfSAlexander Motin 			slot->dev = NULL;
539831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
540d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
541831f5dcfSAlexander Motin 		} else
542831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
543831f5dcfSAlexander Motin 	}
544831f5dcfSAlexander Motin }
545831f5dcfSAlexander Motin 
546b8bf08b1SIan Lepore static void
547b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
548639f59f0SIan Lepore {
549639f59f0SIan Lepore 	bool was_present;
550639f59f0SIan Lepore 
551639f59f0SIan Lepore 	/*
552639f59f0SIan Lepore 	 * If there was no card and now there is one, schedule the task to
553639f59f0SIan Lepore 	 * create the child device after a short delay.  The delay is to
554639f59f0SIan Lepore 	 * debounce the card insert (sometimes the card detect pin stabilizes
555639f59f0SIan Lepore 	 * before the other pins have made good contact).
556639f59f0SIan Lepore 	 *
557639f59f0SIan Lepore 	 * If there was a card present and now it's gone, immediately schedule
558639f59f0SIan Lepore 	 * the task to delete the child device.  No debouncing -- gone is gone,
559639f59f0SIan Lepore 	 * because once power is removed, a full card re-init is needed, and
560639f59f0SIan Lepore 	 * that happens by deleting and recreating the child device.
561639f59f0SIan Lepore 	 */
562639f59f0SIan Lepore 	was_present = slot->dev != NULL;
563639f59f0SIan Lepore 	if (!was_present && is_present) {
564639f59f0SIan Lepore 		taskqueue_enqueue_timeout(taskqueue_swi_giant,
565639f59f0SIan Lepore 		    &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
566639f59f0SIan Lepore 	} else if (was_present && !is_present) {
567639f59f0SIan Lepore 		taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
568639f59f0SIan Lepore 	}
569b8bf08b1SIan Lepore }
570b8bf08b1SIan Lepore 
571b8bf08b1SIan Lepore void
572b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
573b8bf08b1SIan Lepore {
574b8bf08b1SIan Lepore 
575b8bf08b1SIan Lepore 	SDHCI_LOCK(slot);
576b8bf08b1SIan Lepore 	sdhci_handle_card_present_locked(slot, is_present);
577639f59f0SIan Lepore 	SDHCI_UNLOCK(slot);
578639f59f0SIan Lepore }
579639f59f0SIan Lepore 
580639f59f0SIan Lepore static void
581639f59f0SIan Lepore sdhci_card_poll(void *arg)
582639f59f0SIan Lepore {
583639f59f0SIan Lepore 	struct sdhci_slot *slot = arg;
584639f59f0SIan Lepore 
585639f59f0SIan Lepore 	sdhci_handle_card_present(slot,
586639f59f0SIan Lepore 	    SDHCI_GET_CARD_PRESENT(slot->bus, slot));
587639f59f0SIan Lepore 	callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
588639f59f0SIan Lepore 	    sdhci_card_poll, slot);
589639f59f0SIan Lepore }
590639f59f0SIan Lepore 
591d6b3aaf8SOleksandr Tymoshenko int
592d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
593831f5dcfSAlexander Motin {
5940f34084fSMarius Strobl 	uint32_t caps, caps2, freq, host_caps;
595d6b3aaf8SOleksandr Tymoshenko 	int err;
596831f5dcfSAlexander Motin 
597831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
598d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
599d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
600d6b3aaf8SOleksandr Tymoshenko 
601831f5dcfSAlexander Motin 	/* Allocate DMA tag. */
602831f5dcfSAlexander Motin 	err = bus_dma_tag_create(bus_get_dma_tag(dev),
603831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
604831f5dcfSAlexander Motin 	    BUS_SPACE_MAXADDR, NULL, NULL,
605831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE,
606831f5dcfSAlexander Motin 	    BUS_DMA_ALLOCNOW, NULL, NULL,
607831f5dcfSAlexander Motin 	    &slot->dmatag);
608831f5dcfSAlexander Motin 	if (err != 0) {
609831f5dcfSAlexander Motin 		device_printf(dev, "Can't create DMA tag\n");
610831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
611d6b3aaf8SOleksandr Tymoshenko 		return (err);
612831f5dcfSAlexander Motin 	}
613831f5dcfSAlexander Motin 	/* Allocate DMA memory. */
614831f5dcfSAlexander Motin 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
615831f5dcfSAlexander Motin 	    BUS_DMA_NOWAIT, &slot->dmamap);
616831f5dcfSAlexander Motin 	if (err != 0) {
617831f5dcfSAlexander Motin 		device_printf(dev, "Can't alloc DMA memory\n");
618831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
619d6b3aaf8SOleksandr Tymoshenko 		return (err);
620831f5dcfSAlexander Motin 	}
621831f5dcfSAlexander Motin 	/* Map the memory. */
622831f5dcfSAlexander Motin 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
623831f5dcfSAlexander Motin 	    (void *)slot->dmamem, DMA_BLOCK_SIZE,
624831f5dcfSAlexander Motin 	    sdhci_getaddr, &slot->paddr, 0);
625831f5dcfSAlexander Motin 	if (err != 0 || slot->paddr == 0) {
626831f5dcfSAlexander Motin 		device_printf(dev, "Can't load DMA memory\n");
627831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
628d6b3aaf8SOleksandr Tymoshenko 		if (err)
629d6b3aaf8SOleksandr Tymoshenko 			return (err);
630d6b3aaf8SOleksandr Tymoshenko 		else
631d6b3aaf8SOleksandr Tymoshenko 			return (EFAULT);
632831f5dcfSAlexander Motin 	}
633d6b3aaf8SOleksandr Tymoshenko 
634831f5dcfSAlexander Motin 	/* Initialize slot. */
635831f5dcfSAlexander Motin 	sdhci_init(slot);
636d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
637d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
6380f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) {
6398f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
6400f34084fSMarius Strobl 		caps2 = slot->caps2;
6410f34084fSMarius Strobl 	} else {
642831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
6430f34084fSMarius Strobl 		if (slot->version >= SDHCI_SPEC_300)
6440f34084fSMarius Strobl 			caps2 = RD4(slot, SDHCI_CAPABILITIES2);
6450f34084fSMarius Strobl 		else
6460f34084fSMarius Strobl 			caps2 = 0;
6470f34084fSMarius Strobl 	}
648831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
64933aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
65087a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
65187a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
65233aad34dSOleksandr Tymoshenko 	else
65387a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
65487a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
65587a6a871SIan Lepore 	if (freq != 0)
65687a6a871SIan Lepore 		slot->max_clk = freq * 1000000;
65787a6a871SIan Lepore 	/*
65887a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
65987a6a871SIan Lepore 	 * hasn't already set max_clk we're probably not going to work right
66087a6a871SIan Lepore 	 * with an assumption, so complain about it.
66187a6a871SIan Lepore 	 */
662831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
66387a6a871SIan Lepore 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
664831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify base clock "
6651bacf3beSMarius Strobl 		    "frequency, using %dMHz as default.\n",
6661bacf3beSMarius Strobl 		    SDHCI_DEFAULT_MAX_FREQ);
667831f5dcfSAlexander Motin 	}
668a2832f9fSMarius Strobl 	/* Calculate/set timeout clock frequency. */
6698f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
6708f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
671a2832f9fSMarius Strobl 	} else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) {
672a2832f9fSMarius Strobl 		slot->timeout_clk = 1000;
6738f3b7d56SOleksandr Tymoshenko 	} else {
6741bacf3beSMarius Strobl 		slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >>
6751bacf3beSMarius Strobl 		    SDHCI_TIMEOUT_CLK_SHIFT;
6768f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
6778f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
6788f3b7d56SOleksandr Tymoshenko 	}
67987a6a871SIan Lepore 	/*
68087a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
68187a6a871SIan Lepore 	 * hasn't already set timeout_clk we'll probably work okay using the
68287a6a871SIan Lepore 	 * max timeout, but still mention it.
68387a6a871SIan Lepore 	 */
684831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
685831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify timeout clock "
686ceb9e9f7SIan Lepore 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
687ceb9e9f7SIan Lepore 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
688831f5dcfSAlexander Motin 	}
689831f5dcfSAlexander Motin 
69057677a3aSOleksandr Tymoshenko 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
691831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
692831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
693831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
694831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
695831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
696831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
697831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_180)
698831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
699831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
700831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't report any "
701831f5dcfSAlexander Motin 		    "support voltages.\n");
702831f5dcfSAlexander Motin 	}
7030f34084fSMarius Strobl 	host_caps = MMC_CAP_4_BIT_DATA;
7042d1731b8SIan Lepore 	if (caps & SDHCI_CAN_DO_8BITBUS)
7050f34084fSMarius Strobl 		host_caps |= MMC_CAP_8_BIT_DATA;
706831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
7070f34084fSMarius Strobl 		host_caps |= MMC_CAP_HSPEED;
70872dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC)
7090f34084fSMarius Strobl 		host_caps |= MMC_CAP_BOOT_NOACC;
71072dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY)
7110f34084fSMarius Strobl 		host_caps |= MMC_CAP_WAIT_WHILE_BUSY;
7120f34084fSMarius Strobl 	if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50))
7130f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
7140f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_SDR104) {
7150f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
7160f34084fSMarius Strobl 		if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200))
7170f34084fSMarius Strobl 			host_caps |= MMC_CAP_MMC_HS200;
7180f34084fSMarius Strobl 	} else if (caps2 & SDHCI_CAN_SDR50)
7190f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR50;
7200f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_DDR50 &&
7210f34084fSMarius Strobl 	    !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50))
7220f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_DDR50;
7230f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_DDR52)
7240f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_DDR52;
7250f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 &&
7260f34084fSMarius Strobl 	    caps2 & SDHCI_CAN_MMC_HS400)
7270f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
7280f34084fSMarius Strobl 	host_caps |= MMC_CAP_SIGNALING_330;
7290f34084fSMarius Strobl 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
7300f34084fSMarius Strobl 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50 |
7310f34084fSMarius Strobl 	    MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 |
7320f34084fSMarius Strobl 	    MMC_CAP_MMC_HS400_180))
7330f34084fSMarius Strobl 		host_caps |= MMC_CAP_SIGNALING_180;
7340f34084fSMarius Strobl 	if (caps & SDHCI_CTRL2_DRIVER_TYPE_A)
7350f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_A;
7360f34084fSMarius Strobl 	if (caps & SDHCI_CTRL2_DRIVER_TYPE_C)
7370f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_C;
7380f34084fSMarius Strobl 	if (caps & SDHCI_CTRL2_DRIVER_TYPE_D)
7390f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_D;
7400f34084fSMarius Strobl 	slot->host.caps = host_caps;
7410f34084fSMarius Strobl 
742831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
743831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
744831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
745d6b3aaf8SOleksandr Tymoshenko 
746d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
747831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
748d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
749831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
750a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE)
751a2832f9fSMarius Strobl 		slot->opt |= SDHCI_NON_REMOVABLE;
752831f5dcfSAlexander Motin 
753c3a0f75aSOleksandr Tymoshenko 	/*
754c3a0f75aSOleksandr Tymoshenko 	 * Use platform-provided transfer backend
755c3a0f75aSOleksandr Tymoshenko 	 * with PIO as a fallback mechanism
756c3a0f75aSOleksandr Tymoshenko 	 */
757c3a0f75aSOleksandr Tymoshenko 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
758c3a0f75aSOleksandr Tymoshenko 		slot->opt &= ~SDHCI_HAVE_DMA;
759c3a0f75aSOleksandr Tymoshenko 
7605b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
7610f34084fSMarius Strobl 		slot_printf(slot,
7620f34084fSMarius Strobl 		    "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s\n",
763831f5dcfSAlexander Motin 		    slot->max_clk / 1000000,
764831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
7650f34084fSMarius Strobl 		    (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
7660f34084fSMarius Strobl 			((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
767831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
768831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
769831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "",
7700f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "",
7710f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "",
7720f34084fSMarius Strobl 		    (caps & SDHCI_CTRL2_DRIVER_TYPE_A) ? "A" : "",
7730f34084fSMarius Strobl 		    (caps & SDHCI_CTRL2_DRIVER_TYPE_C) ? "C" : "",
7740f34084fSMarius Strobl 		    (caps & SDHCI_CTRL2_DRIVER_TYPE_D) ? "D" : "",
775831f5dcfSAlexander Motin 		    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO");
7760f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 |
7770f34084fSMarius Strobl 		    MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE))
7780f34084fSMarius Strobl 			slot_printf(slot, "eMMC:%s%s%s%s\n",
7790f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "",
7800f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "",
7810f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "",
7820f34084fSMarius Strobl 			    ((host_caps &
7830f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ==
7840f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ?
7850f34084fSMarius Strobl 			    " HS400ES" : "");
7860f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
7870f34084fSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104))
7880f34084fSMarius Strobl 			slot_printf(slot, "UHS-I:%s%s%s%s%s\n",
7890f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "",
7900f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "",
7910f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "",
7920f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "",
7930f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : "");
794831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
795831f5dcfSAlexander Motin 	}
796831f5dcfSAlexander Motin 
797ba6fc1c7SLuiz Otavio O Souza 	slot->timeout = 10;
798ba6fc1c7SLuiz Otavio O Souza 	SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
799ba6fc1c7SLuiz Otavio O Souza 	    SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
800ba6fc1c7SLuiz Otavio O Souza 	    "timeout", CTLFLAG_RW, &slot->timeout, 0,
801ba6fc1c7SLuiz Otavio O Souza 	    "Maximum timeout for SDHCI transfers (in secs)");
802831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
803639f59f0SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
804639f59f0SIan Lepore 		sdhci_card_task, slot);
805639f59f0SIan Lepore 	callout_init(&slot->card_poll_callout, 1);
806e64f01a9SIan Lepore 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
807ba6fc1c7SLuiz Otavio O Souza 
808639f59f0SIan Lepore 	if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
809639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
810639f59f0SIan Lepore 		callout_reset(&slot->card_poll_callout,
811639f59f0SIan Lepore 		    SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
812639f59f0SIan Lepore 	}
813639f59f0SIan Lepore 
814831f5dcfSAlexander Motin 	return (0);
815831f5dcfSAlexander Motin }
816831f5dcfSAlexander Motin 
817d6b3aaf8SOleksandr Tymoshenko void
818d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
819831f5dcfSAlexander Motin {
8207e6ccea3SMarius Strobl 
821d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
822d6b3aaf8SOleksandr Tymoshenko }
823831f5dcfSAlexander Motin 
824d6b3aaf8SOleksandr Tymoshenko int
825d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
826d6b3aaf8SOleksandr Tymoshenko {
827831f5dcfSAlexander Motin 	device_t d;
828831f5dcfSAlexander Motin 
829e64f01a9SIan Lepore 	callout_drain(&slot->timeout_callout);
830639f59f0SIan Lepore 	callout_drain(&slot->card_poll_callout);
831831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
832639f59f0SIan Lepore 	taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
833831f5dcfSAlexander Motin 
834831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
835831f5dcfSAlexander Motin 	d = slot->dev;
836831f5dcfSAlexander Motin 	slot->dev = NULL;
837831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
838831f5dcfSAlexander Motin 	if (d != NULL)
839d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
840831f5dcfSAlexander Motin 
841831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
842831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
843831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
844831f5dcfSAlexander Motin 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
845831f5dcfSAlexander Motin 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
846831f5dcfSAlexander Motin 	bus_dma_tag_destroy(slot->dmatag);
847d6b3aaf8SOleksandr Tymoshenko 
848831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
849d6b3aaf8SOleksandr Tymoshenko 
850831f5dcfSAlexander Motin 	return (0);
851831f5dcfSAlexander Motin }
852831f5dcfSAlexander Motin 
853d6b3aaf8SOleksandr Tymoshenko int
854d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
85592bf0e27SAlexander Motin {
8567e6ccea3SMarius Strobl 
857d6b3aaf8SOleksandr Tymoshenko 	sdhci_reset(slot, SDHCI_RESET_ALL);
85892bf0e27SAlexander Motin 
85992bf0e27SAlexander Motin 	return (0);
86092bf0e27SAlexander Motin }
86192bf0e27SAlexander Motin 
862d6b3aaf8SOleksandr Tymoshenko int
863d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
86492bf0e27SAlexander Motin {
8657e6ccea3SMarius Strobl 
866d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
86792bf0e27SAlexander Motin 
868d6b3aaf8SOleksandr Tymoshenko 	return (0);
86992bf0e27SAlexander Motin }
87092bf0e27SAlexander Motin 
87157677a3aSOleksandr Tymoshenko uint32_t
872b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot)
87357677a3aSOleksandr Tymoshenko {
8747e6ccea3SMarius Strobl 
87557677a3aSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
87657677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
87757677a3aSOleksandr Tymoshenko 	else
87857677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
87957677a3aSOleksandr Tymoshenko }
88057677a3aSOleksandr Tymoshenko 
8816e37fb2bSIan Lepore bool
882b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot)
8836e37fb2bSIan Lepore {
8846e37fb2bSIan Lepore 
885639f59f0SIan Lepore 	if (slot->opt & SDHCI_NON_REMOVABLE)
886639f59f0SIan Lepore 		return true;
887639f59f0SIan Lepore 
8886e37fb2bSIan Lepore 	return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
8896e37fb2bSIan Lepore }
8906e37fb2bSIan Lepore 
8910f34084fSMarius Strobl void
8920f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot)
8930f34084fSMarius Strobl {
8940f34084fSMarius Strobl 	struct mmc_ios *ios;
8950f34084fSMarius Strobl 	uint16_t hostctrl2;
8960f34084fSMarius Strobl 
8970f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
8980f34084fSMarius Strobl 		return;
8990f34084fSMarius Strobl 
9000f34084fSMarius Strobl 	ios = &slot->host.ios;
9010f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
9020f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
9030f34084fSMarius Strobl 	hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK;
9040f34084fSMarius Strobl 	if (ios->timing == bus_timing_mmc_hs400 ||
9050f34084fSMarius Strobl 	    ios->timing == bus_timing_mmc_hs400es)
9060f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_MMC_HS400;
9070f34084fSMarius Strobl 	else if (ios->clock > SD_SDR50_MAX)
9080f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR104;
9090f34084fSMarius Strobl 	else if (ios->clock > SD_SDR25_MAX)
9100f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR50;
9110f34084fSMarius Strobl 	else if (ios->clock > SD_SDR12_MAX) {
9120f34084fSMarius Strobl 		if (ios->timing == bus_timing_uhs_ddr50 ||
9130f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_ddr52)
9140f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_DDR50;
9150f34084fSMarius Strobl 		else
9160f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR25;
9170f34084fSMarius Strobl 	} else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY)
9180f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR12;
9190f34084fSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
9200f34084fSMarius Strobl 	sdhci_set_clock(slot, ios->clock);
9210f34084fSMarius Strobl }
9220f34084fSMarius Strobl 
923d6b3aaf8SOleksandr Tymoshenko int
924d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
925831f5dcfSAlexander Motin {
926831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
927831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
928831f5dcfSAlexander Motin 
929831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
930831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
931831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
932831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
933831f5dcfSAlexander Motin 		sdhci_init(slot);
934831f5dcfSAlexander Motin 	}
935831f5dcfSAlexander Motin 	/* Configure the bus. */
936831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
937831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
9382d1731b8SIan Lepore 	if (ios->bus_width == bus_width_8) {
9392d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
940831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
9412d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_4) {
9422d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
9432d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
9442d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_1) {
9452d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
9462d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
9472d1731b8SIan Lepore 	} else {
9482d1731b8SIan Lepore 		panic("Invalid bus width: %d", ios->bus_width);
9492d1731b8SIan Lepore 	}
9500f34084fSMarius Strobl 	if (ios->clock > SD_SDR12_MAX &&
951bba987dcSIan Lepore 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
952831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
953831f5dcfSAlexander Motin 	else
954831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
955831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
9560f34084fSMarius Strobl 	SDHCI_SET_UHS_TIMING(brdev, slot);
957831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
958d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
959831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
960831f5dcfSAlexander Motin 
961831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
962831f5dcfSAlexander Motin 	return (0);
963831f5dcfSAlexander Motin }
964831f5dcfSAlexander Motin 
9650f34084fSMarius Strobl int
9660f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev)
9670f34084fSMarius Strobl {
9680f34084fSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
9690f34084fSMarius Strobl 	enum mmc_vccq vccq;
9700f34084fSMarius Strobl 	int err;
9710f34084fSMarius Strobl 	uint16_t hostctrl2;
9720f34084fSMarius Strobl 
9730f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
9740f34084fSMarius Strobl 		return (0);
9750f34084fSMarius Strobl 
9760f34084fSMarius Strobl 	err = 0;
9770f34084fSMarius Strobl 	vccq = slot->host.ios.vccq;
9780f34084fSMarius Strobl 	SDHCI_LOCK(slot);
9790f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
9800f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
9810f34084fSMarius Strobl 	switch (vccq) {
9820f34084fSMarius Strobl 	case vccq_330:
9830f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
9840f34084fSMarius Strobl 			goto done;
9850f34084fSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE;
9860f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
9870f34084fSMarius Strobl 		DELAY(5000);
9880f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
9890f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
9900f34084fSMarius Strobl 			goto done;
9910f34084fSMarius Strobl 		err = EAGAIN;
9920f34084fSMarius Strobl 		break;
9930f34084fSMarius Strobl 	case vccq_180:
9940f34084fSMarius Strobl 		if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) {
9950f34084fSMarius Strobl 			err = EINVAL;
9960f34084fSMarius Strobl 			goto done;
9970f34084fSMarius Strobl 		}
9980f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
9990f34084fSMarius Strobl 			goto done;
10000f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_S18_ENABLE;
10010f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
10020f34084fSMarius Strobl 		DELAY(5000);
10030f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
10040f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
10050f34084fSMarius Strobl 			goto done;
10060f34084fSMarius Strobl 		err = EAGAIN;
10070f34084fSMarius Strobl 		break;
10080f34084fSMarius Strobl 	default:
10090f34084fSMarius Strobl 		slot_printf(slot,
10100f34084fSMarius Strobl 		    "Attempt to set unsupported signaling voltage\n");
10110f34084fSMarius Strobl 		err = EINVAL;
10120f34084fSMarius Strobl 		break;
10130f34084fSMarius Strobl 	}
10140f34084fSMarius Strobl done:
10150f34084fSMarius Strobl 	sdhci_set_clock(slot, slot->host.ios.clock);
10160f34084fSMarius Strobl 	SDHCI_UNLOCK(slot);
10170f34084fSMarius Strobl 	return (err);
10180f34084fSMarius Strobl }
10190f34084fSMarius Strobl 
1020831f5dcfSAlexander Motin static void
1021e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot)
1022e64f01a9SIan Lepore {
1023e64f01a9SIan Lepore 	struct mmc_request *req;
1024e64f01a9SIan Lepore 
1025e64f01a9SIan Lepore 	if (slot->req != NULL && slot->curcmd != NULL) {
1026e64f01a9SIan Lepore 		callout_stop(&slot->timeout_callout);
1027e64f01a9SIan Lepore 		req = slot->req;
1028e64f01a9SIan Lepore 		slot->req = NULL;
1029e64f01a9SIan Lepore 		slot->curcmd = NULL;
1030e64f01a9SIan Lepore 		req->done(req);
1031e64f01a9SIan Lepore 	}
1032e64f01a9SIan Lepore }
1033e64f01a9SIan Lepore 
1034e64f01a9SIan Lepore static void
1035e64f01a9SIan Lepore sdhci_timeout(void *arg)
1036e64f01a9SIan Lepore {
1037e64f01a9SIan Lepore 	struct sdhci_slot *slot = arg;
1038e64f01a9SIan Lepore 
1039e64f01a9SIan Lepore 	if (slot->curcmd != NULL) {
10407e586643SIan Lepore 		slot_printf(slot, " Controller timeout\n");
10417e586643SIan Lepore 		sdhci_dumpregs(slot);
1042a6873fd1SIan Lepore 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1043e64f01a9SIan Lepore 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1044e64f01a9SIan Lepore 		sdhci_req_done(slot);
10457e586643SIan Lepore 	} else {
10467e586643SIan Lepore 		slot_printf(slot, " Spurious timeout - no active command\n");
1047e64f01a9SIan Lepore 	}
1048e64f01a9SIan Lepore }
1049e64f01a9SIan Lepore 
1050e64f01a9SIan Lepore static void
1051b440e965SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, struct mmc_data *data)
1052831f5dcfSAlexander Motin {
1053831f5dcfSAlexander Motin 	uint16_t mode;
1054831f5dcfSAlexander Motin 
1055831f5dcfSAlexander Motin 	if (data == NULL)
1056831f5dcfSAlexander Motin 		return;
1057831f5dcfSAlexander Motin 
1058831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
1059831f5dcfSAlexander Motin 	if (data->len > 512)
1060831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
1061831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
1062831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
1063*915780d7SLuiz Otavio O Souza 	if (slot->req->stop && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))
1064831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_ACMD12;
1065831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
1066831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
1067831f5dcfSAlexander Motin 
1068831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
1069831f5dcfSAlexander Motin }
1070831f5dcfSAlexander Motin 
1071831f5dcfSAlexander Motin static void
1072831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
1073831f5dcfSAlexander Motin {
1074831f5dcfSAlexander Motin 	int flags, timeout;
107590993663SIan Lepore 	uint32_t mask;
1076831f5dcfSAlexander Motin 
1077831f5dcfSAlexander Motin 	slot->curcmd = cmd;
1078831f5dcfSAlexander Motin 	slot->cmd_done = 0;
1079831f5dcfSAlexander Motin 
1080831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
1081831f5dcfSAlexander Motin 
1082831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
1083831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1084831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
1085831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1086e64f01a9SIan Lepore 		sdhci_req_done(slot);
1087831f5dcfSAlexander Motin 		return;
1088831f5dcfSAlexander Motin 	}
1089831f5dcfSAlexander Motin 
1090b440e965SMarius Strobl 	/*
1091b440e965SMarius Strobl 	 * Do not issue command if there is no card, clock or power.
1092b440e965SMarius Strobl 	 * Controller will not detect timeout without clock active.
1093b440e965SMarius Strobl 	 */
10946e37fb2bSIan Lepore 	if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
1095d8208d9eSAlexander Motin 	    slot->power == 0 ||
1096d8208d9eSAlexander Motin 	    slot->clock == 0) {
1097831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1098e64f01a9SIan Lepore 		sdhci_req_done(slot);
1099831f5dcfSAlexander Motin 		return;
1100831f5dcfSAlexander Motin 	}
1101831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
1102831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
1103831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
1104831f5dcfSAlexander Motin 	if (cmd->data || (cmd->flags & MMC_RSP_BUSY))
1105831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
1106831f5dcfSAlexander Motin 	/* We shouldn't wait for DAT for stop commands. */
1107831f5dcfSAlexander Motin 	if (cmd == slot->req->stop)
1108831f5dcfSAlexander Motin 		mask &= ~SDHCI_DAT_INHIBIT;
11098775ab45SIan Lepore 	/*
11108775ab45SIan Lepore 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
11118775ab45SIan Lepore 	 *  here at all, but when writing a crash dump we may be bypassing the
11128775ab45SIan Lepore 	 *  host platform's interrupt handler, and in some cases that handler
11138775ab45SIan Lepore 	 *  may be working around hardware quirks such as not respecting r1b
11148775ab45SIan Lepore 	 *  busy indications.  In those cases, this wait-loop serves the purpose
11158775ab45SIan Lepore 	 *  of waiting for the prior command and data transfers to be done, and
11168775ab45SIan Lepore 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
11178775ab45SIan Lepore 	 *  (It's usually more like 20-30ms in the real world.)
11188775ab45SIan Lepore 	 */
11198775ab45SIan Lepore 	timeout = 250;
112090993663SIan Lepore 	while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
1121831f5dcfSAlexander Motin 		if (timeout == 0) {
1122831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
1123831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
1124831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
1125831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
1126e64f01a9SIan Lepore 			sdhci_req_done(slot);
1127831f5dcfSAlexander Motin 			return;
1128831f5dcfSAlexander Motin 		}
1129831f5dcfSAlexander Motin 		timeout--;
1130831f5dcfSAlexander Motin 		DELAY(1000);
1131831f5dcfSAlexander Motin 	}
1132831f5dcfSAlexander Motin 
1133831f5dcfSAlexander Motin 	/* Prepare command flags. */
1134831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
1135831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
1136831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
1137831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
1138831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
1139831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
1140831f5dcfSAlexander Motin 	else
1141831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
1142831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
1143831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
1144831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
1145831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
1146831f5dcfSAlexander Motin 	if (cmd->data)
1147831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
1148831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
1149831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
1150831f5dcfSAlexander Motin 	/* Prepare data. */
1151831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
1152831f5dcfSAlexander Motin 	/*
1153831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
1154831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
1155831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
1156831f5dcfSAlexander Motin 	 */
1157831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
1158831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1159831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
1160831f5dcfSAlexander Motin 	}
1161831f5dcfSAlexander Motin 	/* Set command argument. */
1162831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
1163831f5dcfSAlexander Motin 	/* Set data transfer mode. */
1164831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
1165831f5dcfSAlexander Motin 	/* Start command. */
1166d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
1167a6873fd1SIan Lepore 	/* Start timeout callout. */
1168ba6fc1c7SLuiz Otavio O Souza 	callout_reset(&slot->timeout_callout, slot->timeout * hz,
1169ba6fc1c7SLuiz Otavio O Souza 	    sdhci_timeout, slot);
1170831f5dcfSAlexander Motin }
1171831f5dcfSAlexander Motin 
1172831f5dcfSAlexander Motin static void
1173831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
1174831f5dcfSAlexander Motin {
1175831f5dcfSAlexander Motin 	int i;
11761bacf3beSMarius Strobl 	uint32_t val;
11771bacf3beSMarius Strobl 	uint8_t extra;
1178831f5dcfSAlexander Motin 
1179831f5dcfSAlexander Motin 	slot->cmd_done = 1;
118072dec079SMarius Strobl 	/*
118172dec079SMarius Strobl 	 * Interrupt aggregation: Restore command interrupt.
1182831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
118372dec079SMarius Strobl 	 * happened first.
118472dec079SMarius Strobl 	 */
1185831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE);
1186831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
1187831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1188831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1189831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1190831f5dcfSAlexander Motin 		sdhci_start(slot);
1191831f5dcfSAlexander Motin 		return;
1192831f5dcfSAlexander Motin 	}
1193831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
1194831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1195831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
1196831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
11971bacf3beSMarius Strobl 			extra = 0;
1198831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
11991bacf3beSMarius Strobl 				val = RD4(slot, SDHCI_RESPONSE + i * 4);
12001bacf3beSMarius Strobl 				if (slot->quirks &
12011bacf3beSMarius Strobl 				    SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1202677ee494SIan Lepore 					slot->curcmd->resp[3 - i] = val;
1203677ee494SIan Lepore 				else {
1204677ee494SIan Lepore 					slot->curcmd->resp[3 - i] =
1205677ee494SIan Lepore 					    (val << 8) | extra;
1206831f5dcfSAlexander Motin 					extra = val >> 24;
1207831f5dcfSAlexander Motin 				}
1208677ee494SIan Lepore 			}
1209831f5dcfSAlexander Motin 		} else
1210831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1211831f5dcfSAlexander Motin 	}
1212831f5dcfSAlexander Motin 	/* If data ready - finish. */
1213831f5dcfSAlexander Motin 	if (slot->data_done)
1214831f5dcfSAlexander Motin 		sdhci_start(slot);
1215831f5dcfSAlexander Motin }
1216831f5dcfSAlexander Motin 
1217831f5dcfSAlexander Motin static void
1218831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data)
1219831f5dcfSAlexander Motin {
1220831f5dcfSAlexander Motin 	uint32_t target_timeout, current_timeout;
1221831f5dcfSAlexander Motin 	uint8_t div;
1222831f5dcfSAlexander Motin 
1223831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1224831f5dcfSAlexander Motin 		slot->data_done = 1;
1225831f5dcfSAlexander Motin 		return;
1226831f5dcfSAlexander Motin 	}
1227831f5dcfSAlexander Motin 
1228831f5dcfSAlexander Motin 	slot->data_done = 0;
1229831f5dcfSAlexander Motin 
1230831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
1231831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
1232ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1233ceb9e9f7SIan Lepore 		div = 0xE;
1234ceb9e9f7SIan Lepore 	} else {
1235831f5dcfSAlexander Motin 		target_timeout = 1000000;
1236831f5dcfSAlexander Motin 		div = 0;
1237831f5dcfSAlexander Motin 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1238ceb9e9f7SIan Lepore 		while (current_timeout < target_timeout && div < 0xE) {
1239ceb9e9f7SIan Lepore 			++div;
1240831f5dcfSAlexander Motin 			current_timeout <<= 1;
1241831f5dcfSAlexander Motin 		}
1242831f5dcfSAlexander Motin 		/* Compensate for an off-by-one error in the CaFe chip.*/
1243ceb9e9f7SIan Lepore 		if (div < 0xE &&
1244ceb9e9f7SIan Lepore 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1245ceb9e9f7SIan Lepore 			++div;
1246831f5dcfSAlexander Motin 		}
1247ceb9e9f7SIan Lepore 	}
1248831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1249831f5dcfSAlexander Motin 
1250831f5dcfSAlexander Motin 	if (data == NULL)
1251831f5dcfSAlexander Motin 		return;
1252831f5dcfSAlexander Motin 
1253831f5dcfSAlexander Motin 	/* Use DMA if possible. */
1254831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
1255831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
1256831f5dcfSAlexander Motin 	/* If data is small, broken DMA may return zeroes instead of data, */
1257d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1258831f5dcfSAlexander Motin 	    (data->len <= 512))
1259831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1260831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
1261d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1262831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
1263831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1264831f5dcfSAlexander Motin 	/* Load DMA buffer. */
1265831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
1266831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
1267ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1268ecc2d997SRui Paulo 			    BUS_DMASYNC_PREREAD);
1269831f5dcfSAlexander Motin 		else {
1270831f5dcfSAlexander Motin 			memcpy(slot->dmamem, data->data,
1271ecc2d997SRui Paulo 			    (data->len < DMA_BLOCK_SIZE) ?
1272ecc2d997SRui Paulo 			    data->len : DMA_BLOCK_SIZE);
1273ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1274ecc2d997SRui Paulo 			    BUS_DMASYNC_PREWRITE);
1275831f5dcfSAlexander Motin 		}
1276831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1277831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1278831f5dcfSAlexander Motin 		 * for the last page and unmask else. */
1279831f5dcfSAlexander Motin 		if (data->len == DMA_BLOCK_SIZE)
1280831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1281831f5dcfSAlexander Motin 		else
1282831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
1283831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1284831f5dcfSAlexander Motin 	}
1285831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
1286831f5dcfSAlexander Motin 	slot->offset = 0;
1287831f5dcfSAlexander Motin 	/* Set block size and request IRQ on 4K border. */
12881bacf3beSMarius Strobl 	WR2(slot, SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(DMA_BOUNDARY,
12891bacf3beSMarius Strobl 	    (data->len < 512) ? data->len : 512));
1290831f5dcfSAlexander Motin 	/* Set block count. */
1291831f5dcfSAlexander Motin 	WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
1292831f5dcfSAlexander Motin }
1293831f5dcfSAlexander Motin 
1294c3a0f75aSOleksandr Tymoshenko void
1295831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
1296831f5dcfSAlexander Motin {
1297831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
12987e6ccea3SMarius Strobl 	size_t left;
1299831f5dcfSAlexander Motin 
1300831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
1301ecc2d997SRui Paulo 	 * Auxiliary restore point for the case when data interrupt
1302831f5dcfSAlexander Motin 	 * happened first. */
1303831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
1304831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1305831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
1306831f5dcfSAlexander Motin 	}
1307831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
1308*915780d7SLuiz Otavio O Souza 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) &&
1309*915780d7SLuiz Otavio O Souza 	    slot->curcmd->data != NULL) {
1310831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
13117e6ccea3SMarius Strobl 			left = data->len - slot->offset;
1312ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1313ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTREAD);
1314831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1315831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
1316831f5dcfSAlexander Motin 		} else
1317ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1318ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTWRITE);
1319831f5dcfSAlexander Motin 	}
1320a98788edSIan Lepore 	slot->data_done = 1;
1321831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
1322831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1323831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1324831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1325831f5dcfSAlexander Motin 		sdhci_start(slot);
1326831f5dcfSAlexander Motin 		return;
1327831f5dcfSAlexander Motin 	}
1328831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
1329831f5dcfSAlexander Motin 	if (slot->cmd_done)
1330831f5dcfSAlexander Motin 		sdhci_start(slot);
1331831f5dcfSAlexander Motin }
1332831f5dcfSAlexander Motin 
1333831f5dcfSAlexander Motin static void
1334831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
1335831f5dcfSAlexander Motin {
1336831f5dcfSAlexander Motin 	struct mmc_request *req;
1337831f5dcfSAlexander Motin 
1338831f5dcfSAlexander Motin 	req = slot->req;
1339831f5dcfSAlexander Motin 	if (req == NULL)
1340831f5dcfSAlexander Motin 		return;
1341831f5dcfSAlexander Motin 
1342831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
1343831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
1344831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
1345831f5dcfSAlexander Motin 		return;
1346831f5dcfSAlexander Motin 	}
1347*915780d7SLuiz Otavio O Souza 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) &&
1348*915780d7SLuiz Otavio O Souza 	    !(slot->flags & STOP_STARTED) && req->stop) {
1349831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
1350831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
1351831f5dcfSAlexander Motin 		return;
1352831f5dcfSAlexander Motin 	}
13535b69a497SAlexander Motin 	if (sdhci_debug > 1)
13545b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
13555b69a497SAlexander Motin 	if (!req->cmd->error &&
1356*915780d7SLuiz Otavio O Souza 	    ((slot->curcmd == req->stop &&
1357*915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) ||
1358*915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
1359831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1360831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1361831f5dcfSAlexander Motin 	}
1362831f5dcfSAlexander Motin 
1363e64f01a9SIan Lepore 	sdhci_req_done(slot);
1364831f5dcfSAlexander Motin }
1365831f5dcfSAlexander Motin 
1366d6b3aaf8SOleksandr Tymoshenko int
1367b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev,
1368b440e965SMarius Strobl     struct mmc_request *req)
1369831f5dcfSAlexander Motin {
1370831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1371831f5dcfSAlexander Motin 
1372831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1373831f5dcfSAlexander Motin 	if (slot->req != NULL) {
1374831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1375831f5dcfSAlexander Motin 		return (EBUSY);
1376831f5dcfSAlexander Motin 	}
13775b69a497SAlexander Motin 	if (sdhci_debug > 1) {
13781bacf3beSMarius Strobl 		slot_printf(slot,
13791bacf3beSMarius Strobl 		    "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1380831f5dcfSAlexander Motin 		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
13815b69a497SAlexander Motin 		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
13825b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
13835b69a497SAlexander Motin 	}
1384831f5dcfSAlexander Motin 	slot->req = req;
1385831f5dcfSAlexander Motin 	slot->flags = 0;
1386831f5dcfSAlexander Motin 	sdhci_start(slot);
1387831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1388bea2dca2SAlexander Motin 	if (dumping) {
1389bea2dca2SAlexander Motin 		while (slot->req != NULL) {
1390d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
1391bea2dca2SAlexander Motin 			DELAY(10);
1392bea2dca2SAlexander Motin 		}
1393bea2dca2SAlexander Motin 	}
1394831f5dcfSAlexander Motin 	return (0);
1395831f5dcfSAlexander Motin }
1396831f5dcfSAlexander Motin 
1397d6b3aaf8SOleksandr Tymoshenko int
1398b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev)
1399831f5dcfSAlexander Motin {
1400831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1401831f5dcfSAlexander Motin 	uint32_t val;
1402831f5dcfSAlexander Motin 
1403831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1404831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
1405831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1406831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
1407831f5dcfSAlexander Motin }
1408831f5dcfSAlexander Motin 
1409d6b3aaf8SOleksandr Tymoshenko int
1410b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev)
1411831f5dcfSAlexander Motin {
1412831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1413831f5dcfSAlexander Motin 	int err = 0;
1414831f5dcfSAlexander Motin 
1415831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1416831f5dcfSAlexander Motin 	while (slot->bus_busy)
1417d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
1418831f5dcfSAlexander Motin 	slot->bus_busy++;
1419831f5dcfSAlexander Motin 	/* Activate led. */
1420831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
1421831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1422831f5dcfSAlexander Motin 	return (err);
1423831f5dcfSAlexander Motin }
1424831f5dcfSAlexander Motin 
1425d6b3aaf8SOleksandr Tymoshenko int
1426b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev)
1427831f5dcfSAlexander Motin {
1428831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1429831f5dcfSAlexander Motin 
1430831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1431831f5dcfSAlexander Motin 	/* Deactivate led. */
1432831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
1433831f5dcfSAlexander Motin 	slot->bus_busy--;
1434831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1435d493985aSAlexander Motin 	wakeup(slot);
1436831f5dcfSAlexander Motin 	return (0);
1437831f5dcfSAlexander Motin }
1438831f5dcfSAlexander Motin 
1439831f5dcfSAlexander Motin static void
1440831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
1441831f5dcfSAlexander Motin {
1442831f5dcfSAlexander Motin 
1443831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1444831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
1445831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1446831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1447831f5dcfSAlexander Motin 		return;
1448831f5dcfSAlexander Motin 	}
1449831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
1450831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1451831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
1452831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1453831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
1454831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
1455831f5dcfSAlexander Motin 
1456831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
1457831f5dcfSAlexander Motin }
1458831f5dcfSAlexander Motin 
1459831f5dcfSAlexander Motin static void
1460831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
1461831f5dcfSAlexander Motin {
14621bacf3beSMarius Strobl 	struct mmc_data *data;
14631bacf3beSMarius Strobl 	size_t left;
1464831f5dcfSAlexander Motin 
1465831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1466831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1467831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1468831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1469831f5dcfSAlexander Motin 		return;
1470831f5dcfSAlexander Motin 	}
1471831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
1472831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1473831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1474831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
1475831f5dcfSAlexander Motin 		    intmask);
1476831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1477831f5dcfSAlexander Motin 		return;
1478831f5dcfSAlexander Motin 	}
1479831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
1480831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1481acbaa69fSOleksandr Tymoshenko 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1482831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1483831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
1484831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
1485831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
1486831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1487831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
1488831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1489831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
1490831f5dcfSAlexander Motin 	}
1491831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1492831f5dcfSAlexander Motin 		/* No need to continue after any error. */
1493a98788edSIan Lepore 		goto done;
1494831f5dcfSAlexander Motin 	}
1495831f5dcfSAlexander Motin 
1496831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
1497c3a0f75aSOleksandr Tymoshenko 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
1498c3a0f75aSOleksandr Tymoshenko 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
1499c3a0f75aSOleksandr Tymoshenko 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
15001bacf3beSMarius Strobl 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot,
15011bacf3beSMarius Strobl 			    &intmask);
1502c3a0f75aSOleksandr Tymoshenko 			slot->flags |= PLATFORM_DATA_STARTED;
1503c3a0f75aSOleksandr Tymoshenko 		} else
1504831f5dcfSAlexander Motin 			sdhci_transfer_pio(slot);
1505c3a0f75aSOleksandr Tymoshenko 	}
1506831f5dcfSAlexander Motin 	/* Handle DMA border. */
1507831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
15081bacf3beSMarius Strobl 		data = slot->curcmd->data;
1509831f5dcfSAlexander Motin 
1510831f5dcfSAlexander Motin 		/* Unload DMA buffer ... */
1511831f5dcfSAlexander Motin 		left = data->len - slot->offset;
1512831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
1513831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1514831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
1515831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1516831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
1517831f5dcfSAlexander Motin 		} else {
1518831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1519831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
1520831f5dcfSAlexander Motin 		}
1521831f5dcfSAlexander Motin 		/* ... and reload it again. */
1522831f5dcfSAlexander Motin 		slot->offset += DMA_BLOCK_SIZE;
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_PREREAD);
1527831f5dcfSAlexander Motin 		} else {
1528831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
1529831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)? left : DMA_BLOCK_SIZE);
1530831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1531831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
1532831f5dcfSAlexander Motin 		}
1533831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1534831f5dcfSAlexander Motin 		 * for the last page. */
1535831f5dcfSAlexander Motin 		if (left == DMA_BLOCK_SIZE) {
1536831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1537831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1538831f5dcfSAlexander Motin 		}
1539831f5dcfSAlexander Motin 		/* Restart DMA. */
1540831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1541831f5dcfSAlexander Motin 	}
1542831f5dcfSAlexander Motin 	/* We have got all data. */
1543c3a0f75aSOleksandr Tymoshenko 	if (intmask & SDHCI_INT_DATA_END) {
1544c3a0f75aSOleksandr Tymoshenko 		if (slot->flags & PLATFORM_DATA_STARTED) {
1545c3a0f75aSOleksandr Tymoshenko 			slot->flags &= ~PLATFORM_DATA_STARTED;
1546c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1547c3a0f75aSOleksandr Tymoshenko 		} else
1548831f5dcfSAlexander Motin 			sdhci_finish_data(slot);
1549831f5dcfSAlexander Motin 	}
1550a98788edSIan Lepore done:
1551a98788edSIan Lepore 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
1552a98788edSIan Lepore 		if (slot->flags & PLATFORM_DATA_STARTED) {
1553a98788edSIan Lepore 			slot->flags &= ~PLATFORM_DATA_STARTED;
1554a98788edSIan Lepore 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1555a98788edSIan Lepore 		} else
1556a98788edSIan Lepore 			sdhci_finish_data(slot);
1557a98788edSIan Lepore 	}
1558c3a0f75aSOleksandr Tymoshenko }
1559831f5dcfSAlexander Motin 
1560831f5dcfSAlexander Motin static void
1561831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot)
1562831f5dcfSAlexander Motin {
1563831f5dcfSAlexander Motin 	uint16_t err;
1564831f5dcfSAlexander Motin 
1565831f5dcfSAlexander Motin 	err = RD4(slot, SDHCI_ACMD12_ERR);
1566831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1567831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
1568831f5dcfSAlexander Motin 		    "there is no active command.\n", err);
1569831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1570831f5dcfSAlexander Motin 		return;
1571831f5dcfSAlexander Motin 	}
1572831f5dcfSAlexander Motin 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err);
1573831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_CMD);
1574831f5dcfSAlexander Motin }
1575831f5dcfSAlexander Motin 
1576d6b3aaf8SOleksandr Tymoshenko void
1577d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
1578831f5dcfSAlexander Motin {
15792b96b955SJustin Hibbits 	uint32_t intmask, present;
1580831f5dcfSAlexander Motin 
1581831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1582831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
1583831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
1584831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
1585831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1586d6b3aaf8SOleksandr Tymoshenko 		return;
1587831f5dcfSAlexander Motin 	}
15885b69a497SAlexander Motin 	if (sdhci_debug > 2)
15895b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
15905b69a497SAlexander Motin 
1591831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
1592831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1593639f59f0SIan Lepore 		present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
15942b96b955SJustin Hibbits 		slot->intmask &=
15952b96b955SJustin Hibbits 		    ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
15962b96b955SJustin Hibbits 		slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
15972b96b955SJustin Hibbits 		    SDHCI_INT_CARD_INSERT;
15982b96b955SJustin Hibbits 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
15992b96b955SJustin Hibbits 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1600831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
1601831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
1602b8bf08b1SIan Lepore 		sdhci_handle_card_present_locked(slot, present);
1603831f5dcfSAlexander Motin 		intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1604831f5dcfSAlexander Motin 	}
1605831f5dcfSAlexander Motin 	/* Handle command interrupts. */
1606831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
1607831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
1608831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
1609831f5dcfSAlexander Motin 	}
1610831f5dcfSAlexander Motin 	/* Handle data interrupts. */
1611831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
1612831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
16137e6ccea3SMarius Strobl 		/* Don't call data_irq in case of errored command. */
16147e586643SIan Lepore 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
1615831f5dcfSAlexander Motin 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
1616831f5dcfSAlexander Motin 	}
1617831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
1618831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
1619831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
1620831f5dcfSAlexander Motin 		sdhci_acmd_irq(slot);
1621831f5dcfSAlexander Motin 	}
1622831f5dcfSAlexander Motin 	intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1623831f5dcfSAlexander Motin 	intmask &= ~SDHCI_INT_ACMD12ERR;
1624831f5dcfSAlexander Motin 	intmask &= ~SDHCI_INT_ERROR;
1625831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
1626831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
1627831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
1628831f5dcfSAlexander Motin 		slot_printf(slot,
1629831f5dcfSAlexander Motin 		    "Card is consuming too much power!\n");
1630831f5dcfSAlexander Motin 		intmask &= ~SDHCI_INT_BUS_POWER;
1631831f5dcfSAlexander Motin 	}
1632831f5dcfSAlexander Motin 	/* The rest is unknown. */
1633831f5dcfSAlexander Motin 	if (intmask) {
1634831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
1635831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
1636831f5dcfSAlexander Motin 		    intmask);
1637831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1638831f5dcfSAlexander Motin 	}
1639831f5dcfSAlexander Motin 
1640831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1641831f5dcfSAlexander Motin }
1642831f5dcfSAlexander Motin 
1643d6b3aaf8SOleksandr Tymoshenko int
16441bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which,
16451bacf3beSMarius Strobl     uintptr_t *result)
1646831f5dcfSAlexander Motin {
1647831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
1648831f5dcfSAlexander Motin 
1649831f5dcfSAlexander Motin 	switch (which) {
1650831f5dcfSAlexander Motin 	default:
1651831f5dcfSAlexander Motin 		return (EINVAL);
1652831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
1653bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
1654831f5dcfSAlexander Motin 		break;
1655831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
1656bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
1657831f5dcfSAlexander Motin 		break;
1658831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
1659bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
1660831f5dcfSAlexander Motin 		break;
1661831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
1662bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
1663831f5dcfSAlexander Motin 		break;
1664831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
1665bcd91d25SJayachandran C. 		*result = slot->host.f_min;
1666831f5dcfSAlexander Motin 		break;
1667831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
1668bcd91d25SJayachandran C. 		*result = slot->host.f_max;
1669831f5dcfSAlexander Motin 		break;
1670831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
1671bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
1672831f5dcfSAlexander Motin 		break;
1673831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
1674bcd91d25SJayachandran C. 		*result = slot->host.mode;
1675831f5dcfSAlexander Motin 		break;
1676831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
1677bcd91d25SJayachandran C. 		*result = slot->host.ocr;
1678831f5dcfSAlexander Motin 		break;
1679831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
1680bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
1681831f5dcfSAlexander Motin 		break;
1682831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
1683bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
1684831f5dcfSAlexander Motin 		break;
16850f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
16860f34084fSMarius Strobl 		*result = slot->host.ios.vccq;
16870f34084fSMarius Strobl 		break;
1688831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
1689bcd91d25SJayachandran C. 		*result = slot->host.caps;
1690831f5dcfSAlexander Motin 		break;
1691831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
1692bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
1693831f5dcfSAlexander Motin 		break;
16943a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
1695bcd91d25SJayachandran C. 		*result = 65535;
16963a4a2557SAlexander Motin 		break;
169772dec079SMarius Strobl 	case MMCBR_IVAR_MAX_BUSY_TIMEOUT:
169872dec079SMarius Strobl 		/*
169972dec079SMarius Strobl 		 * Currently, sdhci_start_data() hardcodes 1 s for all CMDs.
170072dec079SMarius Strobl 		 */
170172dec079SMarius Strobl 		*result = 1000000;
170272dec079SMarius Strobl 		break;
1703831f5dcfSAlexander Motin 	}
1704831f5dcfSAlexander Motin 	return (0);
1705831f5dcfSAlexander Motin }
1706831f5dcfSAlexander Motin 
1707d6b3aaf8SOleksandr Tymoshenko int
17081bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which,
17091bacf3beSMarius Strobl     uintptr_t value)
1710831f5dcfSAlexander Motin {
1711831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
1712b440e965SMarius Strobl 	uint32_t clock, max_clock;
1713b440e965SMarius Strobl 	int i;
1714831f5dcfSAlexander Motin 
1715831f5dcfSAlexander Motin 	switch (which) {
1716831f5dcfSAlexander Motin 	default:
1717831f5dcfSAlexander Motin 		return (EINVAL);
1718831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
1719831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
1720831f5dcfSAlexander Motin 		break;
1721831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
1722831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
1723831f5dcfSAlexander Motin 		break;
1724831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
1725831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
1726831f5dcfSAlexander Motin 		break;
1727831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
1728831f5dcfSAlexander Motin 		if (value > 0) {
172957677a3aSOleksandr Tymoshenko 			max_clock = slot->max_clk;
173057677a3aSOleksandr Tymoshenko 			clock = max_clock;
173157677a3aSOleksandr Tymoshenko 
173257677a3aSOleksandr Tymoshenko 			if (slot->version < SDHCI_SPEC_300) {
173357677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
173457677a3aSOleksandr Tymoshenko 				    i <<= 1) {
1735831f5dcfSAlexander Motin 					if (clock <= value)
1736831f5dcfSAlexander Motin 						break;
1737831f5dcfSAlexander Motin 					clock >>= 1;
1738831f5dcfSAlexander Motin 				}
1739b440e965SMarius Strobl 			} else {
174057677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
174157677a3aSOleksandr Tymoshenko 				    i += 2) {
174257677a3aSOleksandr Tymoshenko 					if (clock <= value)
174357677a3aSOleksandr Tymoshenko 						break;
174457677a3aSOleksandr Tymoshenko 					clock = max_clock / (i + 2);
174557677a3aSOleksandr Tymoshenko 				}
174657677a3aSOleksandr Tymoshenko 			}
174757677a3aSOleksandr Tymoshenko 
1748831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
1749831f5dcfSAlexander Motin 		} else
1750831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
1751831f5dcfSAlexander Motin 		break;
1752831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
1753831f5dcfSAlexander Motin 		slot->host.mode = value;
1754831f5dcfSAlexander Motin 		break;
1755831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
1756831f5dcfSAlexander Motin 		slot->host.ocr = value;
1757831f5dcfSAlexander Motin 		break;
1758831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
1759831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
1760831f5dcfSAlexander Motin 		break;
1761831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
1762831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
1763831f5dcfSAlexander Motin 		break;
17640f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
17650f34084fSMarius Strobl 		slot->host.ios.vccq = value;
17660f34084fSMarius Strobl 		break;
1767831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
1768831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
1769831f5dcfSAlexander Motin 		break;
1770831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
1771831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
1772831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
1773831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
17743a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
1775831f5dcfSAlexander Motin 		return (EINVAL);
1776831f5dcfSAlexander Motin 	}
1777831f5dcfSAlexander Motin 	return (0);
1778831f5dcfSAlexander Motin }
1779831f5dcfSAlexander Motin 
1780d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1);
1781