xref: /freebsd/sys/dev/sdhci/sdhci.c (revision aca38eab8a124f1acd26a6cf1eb4b91c4fb7d4f6)
1831f5dcfSAlexander Motin /*-
2831f5dcfSAlexander Motin  * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
3*aca38eabSMarius Strobl  * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
4831f5dcfSAlexander Motin  * All rights reserved.
5831f5dcfSAlexander Motin  *
6831f5dcfSAlexander Motin  * Redistribution and use in source and binary forms, with or without
7831f5dcfSAlexander Motin  * modification, are permitted provided that the following conditions
8831f5dcfSAlexander Motin  * are met:
9831f5dcfSAlexander Motin  * 1. Redistributions of source code must retain the above copyright
10831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer.
11831f5dcfSAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
12831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
13831f5dcfSAlexander Motin  *    documentation and/or other materials provided with the distribution.
14831f5dcfSAlexander Motin  *
15831f5dcfSAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16831f5dcfSAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17831f5dcfSAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18831f5dcfSAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19831f5dcfSAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20831f5dcfSAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21831f5dcfSAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22831f5dcfSAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23831f5dcfSAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24831f5dcfSAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25831f5dcfSAlexander Motin  */
26831f5dcfSAlexander Motin 
27831f5dcfSAlexander Motin #include <sys/cdefs.h>
28831f5dcfSAlexander Motin __FBSDID("$FreeBSD$");
29831f5dcfSAlexander Motin 
30831f5dcfSAlexander Motin #include <sys/param.h>
31831f5dcfSAlexander Motin #include <sys/systm.h>
32831f5dcfSAlexander Motin #include <sys/bus.h>
33e64f01a9SIan Lepore #include <sys/callout.h>
34831f5dcfSAlexander Motin #include <sys/conf.h>
35831f5dcfSAlexander Motin #include <sys/kernel.h>
36*aca38eabSMarius Strobl #include <sys/kobj.h>
37831f5dcfSAlexander Motin #include <sys/lock.h>
38*aca38eabSMarius Strobl #include <sys/malloc.h>
39831f5dcfSAlexander Motin #include <sys/module.h>
40831f5dcfSAlexander Motin #include <sys/mutex.h>
41831f5dcfSAlexander Motin #include <sys/resource.h>
42831f5dcfSAlexander Motin #include <sys/rman.h>
435b69a497SAlexander Motin #include <sys/sysctl.h>
44831f5dcfSAlexander Motin #include <sys/taskqueue.h>
45831f5dcfSAlexander Motin 
46831f5dcfSAlexander Motin #include <machine/bus.h>
47831f5dcfSAlexander Motin #include <machine/resource.h>
48831f5dcfSAlexander Motin #include <machine/stdarg.h>
49831f5dcfSAlexander Motin 
50831f5dcfSAlexander Motin #include <dev/mmc/bridge.h>
51831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h>
52831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h>
53831f5dcfSAlexander Motin 
54*aca38eabSMarius Strobl #include <dev/sdhci/sdhci.h>
55*aca38eabSMarius Strobl 
56a94a63f0SWarner Losh #include <cam/cam.h>
57a94a63f0SWarner Losh #include <cam/cam_ccb.h>
58a94a63f0SWarner Losh #include <cam/cam_debug.h>
59a94a63f0SWarner Losh #include <cam/cam_sim.h>
60a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h>
61a94a63f0SWarner Losh 
62831f5dcfSAlexander Motin #include "mmcbr_if.h"
63d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h"
64831f5dcfSAlexander Motin 
65a94a63f0SWarner Losh #include "opt_mmccam.h"
66a94a63f0SWarner Losh 
67f0d2731dSMarius Strobl SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver");
68831f5dcfSAlexander Motin 
69a94a63f0SWarner Losh static int sdhci_debug = 0;
707e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0,
717e6ccea3SMarius Strobl     "Debug level");
720f34084fSMarius Strobl u_int sdhci_quirk_clear = 0;
730f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear,
740f34084fSMarius Strobl     0, "Mask of quirks to clear");
750f34084fSMarius Strobl u_int sdhci_quirk_set = 0;
760f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0,
770f34084fSMarius Strobl     "Mask of quirks to set");
785b69a497SAlexander Motin 
79d6b3aaf8SOleksandr Tymoshenko #define	RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
80d6b3aaf8SOleksandr Tymoshenko #define	RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
81d6b3aaf8SOleksandr Tymoshenko #define	RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
82d6b3aaf8SOleksandr Tymoshenko #define	RD_MULTI_4(slot, off, ptr, count)	\
83d6b3aaf8SOleksandr Tymoshenko     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
84831f5dcfSAlexander Motin 
85d6b3aaf8SOleksandr Tymoshenko #define	WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
86d6b3aaf8SOleksandr Tymoshenko #define	WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
87d6b3aaf8SOleksandr Tymoshenko #define	WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
88d6b3aaf8SOleksandr Tymoshenko #define	WR_MULTI_4(slot, off, ptr, count)	\
89d6b3aaf8SOleksandr Tymoshenko     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
90831f5dcfSAlexander Motin 
91*aca38eabSMarius Strobl static void sdhci_card_poll(void *arg);
92*aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending);
93*aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset);
94*aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req);
95*aca38eabSMarius Strobl static void sdhci_retune(void *arg);
96831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
97831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot);
98831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data);
99831f5dcfSAlexander Motin 
10015c440e1SWarner Losh #ifdef MMCCAM
101a94a63f0SWarner Losh /* CAM-related */
102a94a63f0SWarner Losh int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock);
103a94a63f0SWarner Losh static int sdhci_cam_update_ios(struct sdhci_slot *slot);
104a94a63f0SWarner Losh static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb);
105a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb);
106a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim);
107a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb);
10815c440e1SWarner Losh #endif
109a94a63f0SWarner Losh 
110831f5dcfSAlexander Motin /* helper routines */
1110f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot);
1120f34084fSMarius Strobl static int slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
1130f34084fSMarius Strobl     __printflike(2, 3);
114*aca38eabSMarius Strobl static uint32_t sdhci_tuning_intmask(struct sdhci_slot *slot);
1150f34084fSMarius Strobl 
116831f5dcfSAlexander Motin #define	SDHCI_LOCK(_slot)		mtx_lock(&(_slot)->mtx)
117831f5dcfSAlexander Motin #define	SDHCI_UNLOCK(_slot)		mtx_unlock(&(_slot)->mtx)
118831f5dcfSAlexander Motin #define	SDHCI_LOCK_INIT(_slot) \
119831f5dcfSAlexander Motin 	mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
120831f5dcfSAlexander Motin #define	SDHCI_LOCK_DESTROY(_slot)	mtx_destroy(&_slot->mtx);
121831f5dcfSAlexander Motin #define	SDHCI_ASSERT_LOCKED(_slot)	mtx_assert(&_slot->mtx, MA_OWNED);
122831f5dcfSAlexander Motin #define	SDHCI_ASSERT_UNLOCKED(_slot)	mtx_assert(&_slot->mtx, MA_NOTOWNED);
123831f5dcfSAlexander Motin 
12433aad34dSOleksandr Tymoshenko #define	SDHCI_DEFAULT_MAX_FREQ	50
12533aad34dSOleksandr Tymoshenko 
12657677a3aSOleksandr Tymoshenko #define	SDHCI_200_MAX_DIVIDER	256
12757677a3aSOleksandr Tymoshenko #define	SDHCI_300_MAX_DIVIDER	2046
12857677a3aSOleksandr Tymoshenko 
129639f59f0SIan Lepore #define	SDHCI_CARD_PRESENT_TICKS	(hz / 5)
130639f59f0SIan Lepore #define	SDHCI_INSERT_DELAY_TICKS	(hz / 2)
131639f59f0SIan Lepore 
13293efdc63SAdrian Chadd /*
13393efdc63SAdrian Chadd  * Broadcom BCM577xx Controller Constants
13493efdc63SAdrian Chadd  */
1351bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */
1361bacf3beSMarius Strobl #define	BCM577XX_DEFAULT_MAX_DIVIDER	256
1371bacf3beSMarius Strobl /* Alternative clock's base frequency. */
1381bacf3beSMarius Strobl #define	BCM577XX_ALT_CLOCK_BASE		63000000
13993efdc63SAdrian Chadd 
14093efdc63SAdrian Chadd #define	BCM577XX_HOST_CONTROL		0x198
14193efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_MASK	0xFFFFCFFF
14293efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_SHIFT	12
14393efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_DEFAULT	0x0
14493efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_64MHZ	0x3
14593efdc63SAdrian Chadd 
146831f5dcfSAlexander Motin static void
147831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
148831f5dcfSAlexander Motin {
1497e6ccea3SMarius Strobl 
150831f5dcfSAlexander Motin 	if (error != 0) {
151831f5dcfSAlexander Motin 		printf("getaddr: error %d\n", error);
152831f5dcfSAlexander Motin 		return;
153831f5dcfSAlexander Motin 	}
154831f5dcfSAlexander Motin 	*(bus_addr_t *)arg = segs[0].ds_addr;
155831f5dcfSAlexander Motin }
156831f5dcfSAlexander Motin 
157d6b3aaf8SOleksandr Tymoshenko static int
158d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
159d6b3aaf8SOleksandr Tymoshenko {
160d6b3aaf8SOleksandr Tymoshenko 	va_list ap;
161d6b3aaf8SOleksandr Tymoshenko 	int retval;
162d6b3aaf8SOleksandr Tymoshenko 
163d6b3aaf8SOleksandr Tymoshenko 	retval = printf("%s-slot%d: ",
164d6b3aaf8SOleksandr Tymoshenko 	    device_get_nameunit(slot->bus), slot->num);
165d6b3aaf8SOleksandr Tymoshenko 
166d6b3aaf8SOleksandr Tymoshenko 	va_start(ap, fmt);
167d6b3aaf8SOleksandr Tymoshenko 	retval += vprintf(fmt, ap);
168d6b3aaf8SOleksandr Tymoshenko 	va_end(ap);
169d6b3aaf8SOleksandr Tymoshenko 	return (retval);
170d6b3aaf8SOleksandr Tymoshenko }
171d6b3aaf8SOleksandr Tymoshenko 
172831f5dcfSAlexander Motin static void
173831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot)
174831f5dcfSAlexander Motin {
1757e6ccea3SMarius Strobl 
176831f5dcfSAlexander Motin 	slot_printf(slot,
177831f5dcfSAlexander Motin 	    "============== REGISTER DUMP ==============\n");
178831f5dcfSAlexander Motin 
179831f5dcfSAlexander Motin 	slot_printf(slot, "Sys addr: 0x%08x | Version:  0x%08x\n",
180831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
181831f5dcfSAlexander Motin 	slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
182831f5dcfSAlexander Motin 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
183831f5dcfSAlexander Motin 	slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
184831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
185831f5dcfSAlexander Motin 	slot_printf(slot, "Present:  0x%08x | Host ctl: 0x%08x\n",
186831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
187831f5dcfSAlexander Motin 	slot_printf(slot, "Power:    0x%08x | Blk gap:  0x%08x\n",
188831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
189831f5dcfSAlexander Motin 	slot_printf(slot, "Wake-up:  0x%08x | Clock:    0x%08x\n",
190831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
191831f5dcfSAlexander Motin 	slot_printf(slot, "Timeout:  0x%08x | Int stat: 0x%08x\n",
192831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
193831f5dcfSAlexander Motin 	slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
194831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
1959dbf8c46SMarius Strobl 	slot_printf(slot, "AC12 err: 0x%08x | Host ctl2:0x%08x\n",
1969dbf8c46SMarius Strobl 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2));
1979dbf8c46SMarius Strobl 	slot_printf(slot, "Caps:     0x%08x | Caps2:    0x%08x\n",
1989dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2));
1999dbf8c46SMarius Strobl 	slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n",
2009dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR));
2019dbf8c46SMarius Strobl 	slot_printf(slot, "ADMA addr:0x%08x | Slot int: 0x%08x\n",
2029dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS));
203831f5dcfSAlexander Motin 
204831f5dcfSAlexander Motin 	slot_printf(slot,
205831f5dcfSAlexander Motin 	    "===========================================\n");
206831f5dcfSAlexander Motin }
207831f5dcfSAlexander Motin 
208831f5dcfSAlexander Motin static void
209831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
210831f5dcfSAlexander Motin {
211831f5dcfSAlexander Motin 	int timeout;
212b440e965SMarius Strobl 	uint32_t clock;
213831f5dcfSAlexander Motin 
214d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
2156e37fb2bSIan Lepore 		if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
216831f5dcfSAlexander Motin 			return;
217831f5dcfSAlexander Motin 	}
218831f5dcfSAlexander Motin 
219831f5dcfSAlexander Motin 	/* Some controllers need this kick or reset won't work. */
220831f5dcfSAlexander Motin 	if ((mask & SDHCI_RESET_ALL) == 0 &&
221d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
222831f5dcfSAlexander Motin 		/* This is to force an update */
223831f5dcfSAlexander Motin 		clock = slot->clock;
224831f5dcfSAlexander Motin 		slot->clock = 0;
225831f5dcfSAlexander Motin 		sdhci_set_clock(slot, clock);
226831f5dcfSAlexander Motin 	}
227831f5dcfSAlexander Motin 
228d8208d9eSAlexander Motin 	if (mask & SDHCI_RESET_ALL) {
229831f5dcfSAlexander Motin 		slot->clock = 0;
230d8208d9eSAlexander Motin 		slot->power = 0;
231d8208d9eSAlexander Motin 	}
232831f5dcfSAlexander Motin 
23361bc42f7SIan Lepore 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
23461bc42f7SIan Lepore 
23561bc42f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
23661bc42f7SIan Lepore 		/*
23761bc42f7SIan Lepore 		 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
23861bc42f7SIan Lepore 		 * specification.  The reset bit has internal propagation delay,
23961bc42f7SIan Lepore 		 * so a fast read after write returns 0 even if reset process is
24061bc42f7SIan Lepore 		 * in progress.  The workaround is to poll for 1 before polling
24161bc42f7SIan Lepore 		 * for 0.  In the worst case, if we miss seeing it asserted the
24261bc42f7SIan Lepore 		 * time we spent waiting is enough to ensure the reset finishes.
24361bc42f7SIan Lepore 		 */
24461bc42f7SIan Lepore 		timeout = 10000;
24561bc42f7SIan Lepore 		while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
24661bc42f7SIan Lepore 			if (timeout <= 0)
24761bc42f7SIan Lepore 				break;
24861bc42f7SIan Lepore 			timeout--;
24961bc42f7SIan Lepore 			DELAY(1);
25061bc42f7SIan Lepore 		}
25161bc42f7SIan Lepore 	}
25261bc42f7SIan Lepore 
253831f5dcfSAlexander Motin 	/* Wait max 100 ms */
25461bc42f7SIan Lepore 	timeout = 10000;
255831f5dcfSAlexander Motin 	/* Controller clears the bits when it's done */
25661bc42f7SIan Lepore 	while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
25761bc42f7SIan Lepore 		if (timeout <= 0) {
25861bc42f7SIan Lepore 			slot_printf(slot, "Reset 0x%x never completed.\n",
25961bc42f7SIan Lepore 			    mask);
260831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
261831f5dcfSAlexander Motin 			return;
262831f5dcfSAlexander Motin 		}
263831f5dcfSAlexander Motin 		timeout--;
26461bc42f7SIan Lepore 		DELAY(10);
265831f5dcfSAlexander Motin 	}
266831f5dcfSAlexander Motin }
267831f5dcfSAlexander Motin 
268*aca38eabSMarius Strobl static uint32_t
269*aca38eabSMarius Strobl sdhci_tuning_intmask(struct sdhci_slot *slot)
270*aca38eabSMarius Strobl {
271*aca38eabSMarius Strobl 	uint32_t intmask;
272*aca38eabSMarius Strobl 
273*aca38eabSMarius Strobl 	intmask = 0;
274*aca38eabSMarius Strobl 	if (slot->opt & SDHCI_TUNING_SUPPORTED) {
275*aca38eabSMarius Strobl 		intmask |= SDHCI_INT_TUNEERR;
276*aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_2 ||
277*aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_3)
278*aca38eabSMarius Strobl 			intmask |= SDHCI_INT_RETUNE;
279*aca38eabSMarius Strobl 	}
280*aca38eabSMarius Strobl 	return (intmask);
281*aca38eabSMarius Strobl }
282*aca38eabSMarius Strobl 
283831f5dcfSAlexander Motin static void
284831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot)
285831f5dcfSAlexander Motin {
286831f5dcfSAlexander Motin 
287831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
288831f5dcfSAlexander Motin 
289831f5dcfSAlexander Motin 	/* Enable interrupts. */
290831f5dcfSAlexander Motin 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
291831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
292831f5dcfSAlexander Motin 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
293831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
294831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
2953685b398SWarner Losh 	    SDHCI_INT_ACMD12ERR;
296639f59f0SIan Lepore 
297639f59f0SIan Lepore 	if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
298639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
299639f59f0SIan Lepore 		slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
300639f59f0SIan Lepore 	}
301639f59f0SIan Lepore 
302*aca38eabSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask | sdhci_tuning_intmask(slot));
303831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
304831f5dcfSAlexander Motin }
305831f5dcfSAlexander Motin 
306831f5dcfSAlexander Motin static void
307831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
308831f5dcfSAlexander Motin {
30993efdc63SAdrian Chadd 	uint32_t clk_base;
31093efdc63SAdrian Chadd 	uint32_t clk_sel;
311831f5dcfSAlexander Motin 	uint32_t res;
312831f5dcfSAlexander Motin 	uint16_t clk;
3138f3b7d56SOleksandr Tymoshenko 	uint16_t div;
314831f5dcfSAlexander Motin 	int timeout;
315831f5dcfSAlexander Motin 
316831f5dcfSAlexander Motin 	if (clock == slot->clock)
317831f5dcfSAlexander Motin 		return;
318831f5dcfSAlexander Motin 	slot->clock = clock;
319831f5dcfSAlexander Motin 
320831f5dcfSAlexander Motin 	/* Turn off the clock. */
3214ddc0172SIan Lepore 	clk = RD2(slot, SDHCI_CLOCK_CONTROL);
3224ddc0172SIan Lepore 	WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
323b440e965SMarius Strobl 	/* If no clock requested - leave it so. */
324831f5dcfSAlexander Motin 	if (clock == 0)
325831f5dcfSAlexander Motin 		return;
326ceb9e9f7SIan Lepore 
32793efdc63SAdrian Chadd 	/* Determine the clock base frequency */
32893efdc63SAdrian Chadd 	clk_base = slot->max_clk;
32993efdc63SAdrian Chadd 	if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
3301bacf3beSMarius Strobl 		clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) &
3311bacf3beSMarius Strobl 		    BCM577XX_CTRL_CLKSEL_MASK;
33293efdc63SAdrian Chadd 
3331bacf3beSMarius Strobl 		/*
3341bacf3beSMarius Strobl 		 * Select clock source appropriate for the requested frequency.
3351bacf3beSMarius Strobl 		 */
33693efdc63SAdrian Chadd 		if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
33793efdc63SAdrian Chadd 			clk_base = BCM577XX_ALT_CLOCK_BASE;
3381bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ <<
3391bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
34093efdc63SAdrian Chadd 		} else {
3411bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT <<
3421bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
34393efdc63SAdrian Chadd 		}
34493efdc63SAdrian Chadd 
34593efdc63SAdrian Chadd 		WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
34693efdc63SAdrian Chadd 	}
34793efdc63SAdrian Chadd 
348ceb9e9f7SIan Lepore 	/* Recalculate timeout clock frequency based on the new sd clock. */
349ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
350ceb9e9f7SIan Lepore 		slot->timeout_clk = slot->clock / 1000;
351ceb9e9f7SIan Lepore 
3528f3b7d56SOleksandr Tymoshenko 	if (slot->version < SDHCI_SPEC_300) {
353831f5dcfSAlexander Motin 		/* Looking for highest freq <= clock. */
35493efdc63SAdrian Chadd 		res = clk_base;
35557677a3aSOleksandr Tymoshenko 		for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
356831f5dcfSAlexander Motin 			if (res <= clock)
357831f5dcfSAlexander Motin 				break;
358831f5dcfSAlexander Motin 			res >>= 1;
359831f5dcfSAlexander Motin 		}
360831f5dcfSAlexander Motin 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
3618f3b7d56SOleksandr Tymoshenko 		div >>= 1;
362c11bbc7dSMarius Strobl 	} else {
3638f3b7d56SOleksandr Tymoshenko 		/* Version 3.0 divisors are multiples of two up to 1023 * 2 */
36493efdc63SAdrian Chadd 		if (clock >= clk_base)
36557677a3aSOleksandr Tymoshenko 			div = 0;
3668f3b7d56SOleksandr Tymoshenko 		else {
36757677a3aSOleksandr Tymoshenko 			for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
36893efdc63SAdrian Chadd 				if ((clk_base / div) <= clock)
3698f3b7d56SOleksandr Tymoshenko 					break;
3708f3b7d56SOleksandr Tymoshenko 			}
3718f3b7d56SOleksandr Tymoshenko 		}
3728f3b7d56SOleksandr Tymoshenko 		div >>= 1;
3738f3b7d56SOleksandr Tymoshenko 	}
3748f3b7d56SOleksandr Tymoshenko 
3758f3b7d56SOleksandr Tymoshenko 	if (bootverbose || sdhci_debug)
37693efdc63SAdrian Chadd 		slot_printf(slot, "Divider %d for freq %d (base %d)\n",
37793efdc63SAdrian Chadd 			div, clock, clk_base);
3788f3b7d56SOleksandr Tymoshenko 
379831f5dcfSAlexander Motin 	/* Now we have got divider, set it. */
3808f3b7d56SOleksandr Tymoshenko 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
3818f3b7d56SOleksandr Tymoshenko 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
3828f3b7d56SOleksandr Tymoshenko 		<< SDHCI_DIVIDER_HI_SHIFT;
3838f3b7d56SOleksandr Tymoshenko 
384831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
385831f5dcfSAlexander Motin 	/* Enable clock. */
386831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_INT_EN;
387831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
388831f5dcfSAlexander Motin 	/* Wait up to 10 ms until it stabilize. */
389831f5dcfSAlexander Motin 	timeout = 10;
390831f5dcfSAlexander Motin 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
391831f5dcfSAlexander Motin 		& SDHCI_CLOCK_INT_STABLE)) {
392831f5dcfSAlexander Motin 		if (timeout == 0) {
393831f5dcfSAlexander Motin 			slot_printf(slot,
394831f5dcfSAlexander Motin 			    "Internal clock never stabilised.\n");
395831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
396831f5dcfSAlexander Motin 			return;
397831f5dcfSAlexander Motin 		}
398831f5dcfSAlexander Motin 		timeout--;
399831f5dcfSAlexander Motin 		DELAY(1000);
400831f5dcfSAlexander Motin 	}
401831f5dcfSAlexander Motin 	/* Pass clock signal to the bus. */
402831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_CARD_EN;
403831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
404831f5dcfSAlexander Motin }
405831f5dcfSAlexander Motin 
406831f5dcfSAlexander Motin static void
407831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power)
408831f5dcfSAlexander Motin {
40985083a80SMarius Strobl 	int i;
410831f5dcfSAlexander Motin 	uint8_t pwr;
411831f5dcfSAlexander Motin 
412831f5dcfSAlexander Motin 	if (slot->power == power)
413831f5dcfSAlexander Motin 		return;
414d6b3aaf8SOleksandr Tymoshenko 
415831f5dcfSAlexander Motin 	slot->power = power;
416831f5dcfSAlexander Motin 
417831f5dcfSAlexander Motin 	/* Turn off the power. */
418831f5dcfSAlexander Motin 	pwr = 0;
419831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
420b440e965SMarius Strobl 	/* If power down requested - leave it so. */
421831f5dcfSAlexander Motin 	if (power == 0)
422831f5dcfSAlexander Motin 		return;
423831f5dcfSAlexander Motin 	/* Set voltage. */
424831f5dcfSAlexander Motin 	switch (1 << power) {
425831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
426831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
427831f5dcfSAlexander Motin 		break;
428831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
429831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
430831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
431831f5dcfSAlexander Motin 		break;
432831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
433831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
434831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
435831f5dcfSAlexander Motin 		break;
436831f5dcfSAlexander Motin 	}
437831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
43885083a80SMarius Strobl 	/*
43985083a80SMarius Strobl 	 * Turn on VDD1 power.  Note that at least some Intel controllers can
44085083a80SMarius Strobl 	 * fail to enable bus power on the first try after transiting from D3
4418022c8ebSMarius Strobl 	 * to D0, so we give them up to 2 ms.
44285083a80SMarius Strobl 	 */
443831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
44485083a80SMarius Strobl 	for (i = 0; i < 20; i++) {
445831f5dcfSAlexander Motin 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
44685083a80SMarius Strobl 		if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)
44785083a80SMarius Strobl 			break;
44885083a80SMarius Strobl 		DELAY(100);
44985083a80SMarius Strobl 	}
45085083a80SMarius Strobl 	if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON))
45185083a80SMarius Strobl 		slot_printf(slot, "Bus power failed to enable");
452a2832f9fSMarius Strobl 
453a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) {
454a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10);
455a2832f9fSMarius Strobl 		DELAY(10);
456a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
457a2832f9fSMarius Strobl 		DELAY(300);
458a2832f9fSMarius Strobl 	}
459831f5dcfSAlexander Motin }
460831f5dcfSAlexander Motin 
461831f5dcfSAlexander Motin static void
462831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
463831f5dcfSAlexander Motin {
464831f5dcfSAlexander Motin 	uint32_t data;
465831f5dcfSAlexander Motin 	char *buffer;
466831f5dcfSAlexander Motin 	size_t left;
467831f5dcfSAlexander Motin 
468831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
469831f5dcfSAlexander Motin 	buffer += slot->offset;
470831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
471831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
472831f5dcfSAlexander Motin 	slot->offset += left;
473831f5dcfSAlexander Motin 
474831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
475d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
476831f5dcfSAlexander Motin 		DELAY(10);
477ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
478831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
479831f5dcfSAlexander Motin 		while (left > 3) {
480831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
481831f5dcfSAlexander Motin 			buffer[0] = data;
482831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
483831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
484831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
485831f5dcfSAlexander Motin 			buffer += 4;
486831f5dcfSAlexander Motin 			left -= 4;
487831f5dcfSAlexander Motin 		}
488831f5dcfSAlexander Motin 	} else {
489d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
490831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
491831f5dcfSAlexander Motin 		left &= 3;
492831f5dcfSAlexander Motin 	}
493831f5dcfSAlexander Motin 	/* Handle uneven size case. */
494831f5dcfSAlexander Motin 	if (left > 0) {
495831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
496831f5dcfSAlexander Motin 		while (left > 0) {
497831f5dcfSAlexander Motin 			*(buffer++) = data;
498831f5dcfSAlexander Motin 			data >>= 8;
499831f5dcfSAlexander Motin 			left--;
500831f5dcfSAlexander Motin 		}
501831f5dcfSAlexander Motin 	}
502831f5dcfSAlexander Motin }
503831f5dcfSAlexander Motin 
504831f5dcfSAlexander Motin static void
505831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
506831f5dcfSAlexander Motin {
507831f5dcfSAlexander Motin 	uint32_t data = 0;
508831f5dcfSAlexander Motin 	char *buffer;
509831f5dcfSAlexander Motin 	size_t left;
510831f5dcfSAlexander Motin 
511831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
512831f5dcfSAlexander Motin 	buffer += slot->offset;
513831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
514831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
515831f5dcfSAlexander Motin 	slot->offset += left;
516831f5dcfSAlexander Motin 
517ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
518831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
519831f5dcfSAlexander Motin 		while (left > 3) {
520831f5dcfSAlexander Motin 			data = buffer[0] +
521831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
522831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
523831f5dcfSAlexander Motin 			    (buffer[3] << 24);
524831f5dcfSAlexander Motin 			left -= 4;
525831f5dcfSAlexander Motin 			buffer += 4;
526831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
527831f5dcfSAlexander Motin 		}
528831f5dcfSAlexander Motin 	} else {
529d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
530831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
531831f5dcfSAlexander Motin 		left &= 3;
532831f5dcfSAlexander Motin 	}
533831f5dcfSAlexander Motin 	/* Handle uneven size case. */
534831f5dcfSAlexander Motin 	if (left > 0) {
535831f5dcfSAlexander Motin 		while (left > 0) {
536831f5dcfSAlexander Motin 			data <<= 8;
537831f5dcfSAlexander Motin 			data += *(buffer++);
538831f5dcfSAlexander Motin 			left--;
539831f5dcfSAlexander Motin 		}
540831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
541831f5dcfSAlexander Motin 	}
542831f5dcfSAlexander Motin }
543831f5dcfSAlexander Motin 
544831f5dcfSAlexander Motin static void
545831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
546831f5dcfSAlexander Motin {
547831f5dcfSAlexander Motin 
548831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
549831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
550831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
551831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
552831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
553831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
554831f5dcfSAlexander Motin 				break;
555831f5dcfSAlexander Motin 		}
556831f5dcfSAlexander Motin 	} else {
557831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
558831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
559831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
560831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
561831f5dcfSAlexander Motin 				break;
562831f5dcfSAlexander Motin 		}
563831f5dcfSAlexander Motin 	}
564831f5dcfSAlexander Motin }
565831f5dcfSAlexander Motin 
566831f5dcfSAlexander Motin static void
5677e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused)
568831f5dcfSAlexander Motin {
569831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
5707e6ccea3SMarius Strobl 	device_t d;
571831f5dcfSAlexander Motin 
572831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
5736e37fb2bSIan Lepore 	if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
574a94a63f0SWarner Losh #ifdef MMCCAM
575a94a63f0SWarner Losh 		if (slot->card_present == 0) {
576a94a63f0SWarner Losh #else
577831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
578a94a63f0SWarner Losh #endif
579831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
580639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
581639f59f0SIan Lepore 				slot_printf(slot, "Card inserted\n");
582a94a63f0SWarner Losh #ifdef MMCCAM
583a94a63f0SWarner Losh 			slot->card_present = 1;
584a94a63f0SWarner Losh 			union ccb *ccb;
585a94a63f0SWarner Losh 			uint32_t pathid;
586a94a63f0SWarner Losh 			pathid = cam_sim_path(slot->sim);
587a94a63f0SWarner Losh 			ccb = xpt_alloc_ccb_nowait();
588a94a63f0SWarner Losh 			if (ccb == NULL) {
589a94a63f0SWarner Losh 				slot_printf(slot, "Unable to alloc CCB for rescan\n");
590a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
591a94a63f0SWarner Losh 				return;
592a94a63f0SWarner Losh 			}
593a94a63f0SWarner Losh 
594a94a63f0SWarner Losh 			/*
595a94a63f0SWarner Losh 			 * We create a rescan request for BUS:0:0, since the card
596a94a63f0SWarner Losh 			 * will be at lun 0.
597a94a63f0SWarner Losh 			 */
598a94a63f0SWarner Losh 			if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
599a94a63f0SWarner Losh 					    /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
600a94a63f0SWarner Losh 				slot_printf(slot, "Unable to create path for rescan\n");
601a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
602a94a63f0SWarner Losh 				xpt_free_ccb(ccb);
603a94a63f0SWarner Losh 				return;
604a94a63f0SWarner Losh 			}
605a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
606a94a63f0SWarner Losh 			xpt_rescan(ccb);
607a94a63f0SWarner Losh #else
608*aca38eabSMarius Strobl 			d = slot->dev = device_add_child(slot->bus, "mmc", -1);
609831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
610*aca38eabSMarius Strobl 			if (d) {
611*aca38eabSMarius Strobl 				device_set_ivars(d, slot);
612*aca38eabSMarius Strobl 				(void)device_probe_and_attach(d);
613*aca38eabSMarius Strobl 			}
614a94a63f0SWarner Losh #endif
615831f5dcfSAlexander Motin 		} else
616831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
617831f5dcfSAlexander Motin 	} else {
618a94a63f0SWarner Losh #ifdef MMCCAM
619a94a63f0SWarner Losh 		if (slot->card_present == 1) {
620a94a63f0SWarner Losh #else
621831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
622a94a63f0SWarner Losh #endif
623831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
624639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
625639f59f0SIan Lepore 				slot_printf(slot, "Card removed\n");
6267e6ccea3SMarius Strobl 			d = slot->dev;
627831f5dcfSAlexander Motin 			slot->dev = NULL;
628a94a63f0SWarner Losh #ifdef MMCCAM
629a94a63f0SWarner Losh 			slot->card_present = 0;
630a94a63f0SWarner Losh 			union ccb *ccb;
631a94a63f0SWarner Losh 			uint32_t pathid;
632a94a63f0SWarner Losh 			pathid = cam_sim_path(slot->sim);
633a94a63f0SWarner Losh 			ccb = xpt_alloc_ccb_nowait();
634a94a63f0SWarner Losh 			if (ccb == NULL) {
635a94a63f0SWarner Losh 				slot_printf(slot, "Unable to alloc CCB for rescan\n");
636a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
637a94a63f0SWarner Losh 				return;
638a94a63f0SWarner Losh 			}
639a94a63f0SWarner Losh 
640a94a63f0SWarner Losh 			/*
641a94a63f0SWarner Losh 			 * We create a rescan request for BUS:0:0, since the card
642a94a63f0SWarner Losh 			 * will be at lun 0.
643a94a63f0SWarner Losh 			 */
644a94a63f0SWarner Losh 			if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
645a94a63f0SWarner Losh 					    /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
646a94a63f0SWarner Losh 				slot_printf(slot, "Unable to create path for rescan\n");
647a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
648a94a63f0SWarner Losh 				xpt_free_ccb(ccb);
649a94a63f0SWarner Losh 				return;
650a94a63f0SWarner Losh 			}
651a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
652a94a63f0SWarner Losh 			xpt_rescan(ccb);
653a94a63f0SWarner Losh #else
654*aca38eabSMarius Strobl 			slot->intmask &= ~sdhci_tuning_intmask(slot);
655*aca38eabSMarius Strobl 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
656*aca38eabSMarius Strobl 			slot->opt &= ~SDHCI_TUNING_ENABLED;
657831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
658*aca38eabSMarius Strobl 			callout_drain(&slot->retune_callout);
659d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
660a94a63f0SWarner Losh #endif
661831f5dcfSAlexander Motin 		} else
662831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
663831f5dcfSAlexander Motin 	}
664831f5dcfSAlexander Motin }
665831f5dcfSAlexander Motin 
666b8bf08b1SIan Lepore static void
667b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
668639f59f0SIan Lepore {
669639f59f0SIan Lepore 	bool was_present;
670639f59f0SIan Lepore 
671639f59f0SIan Lepore 	/*
672639f59f0SIan Lepore 	 * If there was no card and now there is one, schedule the task to
673639f59f0SIan Lepore 	 * create the child device after a short delay.  The delay is to
674639f59f0SIan Lepore 	 * debounce the card insert (sometimes the card detect pin stabilizes
675639f59f0SIan Lepore 	 * before the other pins have made good contact).
676639f59f0SIan Lepore 	 *
677639f59f0SIan Lepore 	 * If there was a card present and now it's gone, immediately schedule
678639f59f0SIan Lepore 	 * the task to delete the child device.  No debouncing -- gone is gone,
679639f59f0SIan Lepore 	 * because once power is removed, a full card re-init is needed, and
680639f59f0SIan Lepore 	 * that happens by deleting and recreating the child device.
681639f59f0SIan Lepore 	 */
682a94a63f0SWarner Losh #ifdef MMCCAM
683a94a63f0SWarner Losh 	was_present = slot->card_present;
684a94a63f0SWarner Losh #else
685639f59f0SIan Lepore 	was_present = slot->dev != NULL;
686a94a63f0SWarner Losh #endif
687639f59f0SIan Lepore 	if (!was_present && is_present) {
688639f59f0SIan Lepore 		taskqueue_enqueue_timeout(taskqueue_swi_giant,
689639f59f0SIan Lepore 		    &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
690639f59f0SIan Lepore 	} else if (was_present && !is_present) {
691639f59f0SIan Lepore 		taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
692639f59f0SIan Lepore 	}
693b8bf08b1SIan Lepore }
694b8bf08b1SIan Lepore 
695b8bf08b1SIan Lepore void
696b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
697b8bf08b1SIan Lepore {
698b8bf08b1SIan Lepore 
699b8bf08b1SIan Lepore 	SDHCI_LOCK(slot);
700b8bf08b1SIan Lepore 	sdhci_handle_card_present_locked(slot, is_present);
701639f59f0SIan Lepore 	SDHCI_UNLOCK(slot);
702639f59f0SIan Lepore }
703639f59f0SIan Lepore 
704639f59f0SIan Lepore static void
705639f59f0SIan Lepore sdhci_card_poll(void *arg)
706639f59f0SIan Lepore {
707639f59f0SIan Lepore 	struct sdhci_slot *slot = arg;
708639f59f0SIan Lepore 
709639f59f0SIan Lepore 	sdhci_handle_card_present(slot,
710639f59f0SIan Lepore 	    SDHCI_GET_CARD_PRESENT(slot->bus, slot));
711639f59f0SIan Lepore 	callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
712639f59f0SIan Lepore 	    sdhci_card_poll, slot);
713639f59f0SIan Lepore }
714639f59f0SIan Lepore 
715d6b3aaf8SOleksandr Tymoshenko int
716d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
717831f5dcfSAlexander Motin {
718*aca38eabSMarius Strobl 	kobjop_desc_t kobj_desc;
719*aca38eabSMarius Strobl 	kobj_method_t *kobj_method;
7200f34084fSMarius Strobl 	uint32_t caps, caps2, freq, host_caps;
721d6b3aaf8SOleksandr Tymoshenko 	int err;
722831f5dcfSAlexander Motin 
723831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
724a94a63f0SWarner Losh 
725d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
726d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
727d6b3aaf8SOleksandr Tymoshenko 
728831f5dcfSAlexander Motin 	/* Allocate DMA tag. */
729831f5dcfSAlexander Motin 	err = bus_dma_tag_create(bus_get_dma_tag(dev),
730831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
731831f5dcfSAlexander Motin 	    BUS_SPACE_MAXADDR, NULL, NULL,
732831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE,
733831f5dcfSAlexander Motin 	    BUS_DMA_ALLOCNOW, NULL, NULL,
734831f5dcfSAlexander Motin 	    &slot->dmatag);
735831f5dcfSAlexander Motin 	if (err != 0) {
736831f5dcfSAlexander Motin 		device_printf(dev, "Can't create DMA tag\n");
737831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
738d6b3aaf8SOleksandr Tymoshenko 		return (err);
739831f5dcfSAlexander Motin 	}
740831f5dcfSAlexander Motin 	/* Allocate DMA memory. */
741831f5dcfSAlexander Motin 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
742831f5dcfSAlexander Motin 	    BUS_DMA_NOWAIT, &slot->dmamap);
743831f5dcfSAlexander Motin 	if (err != 0) {
744831f5dcfSAlexander Motin 		device_printf(dev, "Can't alloc DMA memory\n");
745831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
746d6b3aaf8SOleksandr Tymoshenko 		return (err);
747831f5dcfSAlexander Motin 	}
748831f5dcfSAlexander Motin 	/* Map the memory. */
749831f5dcfSAlexander Motin 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
750831f5dcfSAlexander Motin 	    (void *)slot->dmamem, DMA_BLOCK_SIZE,
751831f5dcfSAlexander Motin 	    sdhci_getaddr, &slot->paddr, 0);
752831f5dcfSAlexander Motin 	if (err != 0 || slot->paddr == 0) {
753831f5dcfSAlexander Motin 		device_printf(dev, "Can't load DMA memory\n");
754831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
755d6b3aaf8SOleksandr Tymoshenko 		if (err)
756d6b3aaf8SOleksandr Tymoshenko 			return (err);
757d6b3aaf8SOleksandr Tymoshenko 		else
758d6b3aaf8SOleksandr Tymoshenko 			return (EFAULT);
759831f5dcfSAlexander Motin 	}
760d6b3aaf8SOleksandr Tymoshenko 
761d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
762d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
7630f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) {
7648f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
7650f34084fSMarius Strobl 		caps2 = slot->caps2;
7660f34084fSMarius Strobl 	} else {
767831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
7680f34084fSMarius Strobl 		if (slot->version >= SDHCI_SPEC_300)
7690f34084fSMarius Strobl 			caps2 = RD4(slot, SDHCI_CAPABILITIES2);
7700f34084fSMarius Strobl 		else
7710f34084fSMarius Strobl 			caps2 = 0;
7720f34084fSMarius Strobl 	}
773831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
77433aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
77587a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
77687a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
77733aad34dSOleksandr Tymoshenko 	else
77887a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
77987a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
78087a6a871SIan Lepore 	if (freq != 0)
78187a6a871SIan Lepore 		slot->max_clk = freq * 1000000;
78287a6a871SIan Lepore 	/*
78387a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
78487a6a871SIan Lepore 	 * hasn't already set max_clk we're probably not going to work right
78587a6a871SIan Lepore 	 * with an assumption, so complain about it.
78687a6a871SIan Lepore 	 */
787831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
78887a6a871SIan Lepore 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
789831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify base clock "
7901bacf3beSMarius Strobl 		    "frequency, using %dMHz as default.\n",
7911bacf3beSMarius Strobl 		    SDHCI_DEFAULT_MAX_FREQ);
792831f5dcfSAlexander Motin 	}
793a2832f9fSMarius Strobl 	/* Calculate/set timeout clock frequency. */
7948f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
7958f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
796a2832f9fSMarius Strobl 	} else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) {
797a2832f9fSMarius Strobl 		slot->timeout_clk = 1000;
7988f3b7d56SOleksandr Tymoshenko 	} else {
7991bacf3beSMarius Strobl 		slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >>
8001bacf3beSMarius Strobl 		    SDHCI_TIMEOUT_CLK_SHIFT;
8018f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
8028f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
8038f3b7d56SOleksandr Tymoshenko 	}
80487a6a871SIan Lepore 	/*
80587a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
80687a6a871SIan Lepore 	 * hasn't already set timeout_clk we'll probably work okay using the
80787a6a871SIan Lepore 	 * max timeout, but still mention it.
80887a6a871SIan Lepore 	 */
809831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
810831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify timeout clock "
811ceb9e9f7SIan Lepore 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
812ceb9e9f7SIan Lepore 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
813831f5dcfSAlexander Motin 	}
814831f5dcfSAlexander Motin 
81557677a3aSOleksandr Tymoshenko 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
816831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
817831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
818831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
819831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
820831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
821831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
822831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_180)
823831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
824831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
825831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't report any "
826831f5dcfSAlexander Motin 		    "support voltages.\n");
827831f5dcfSAlexander Motin 	}
828*aca38eabSMarius Strobl 
8290f34084fSMarius Strobl 	host_caps = MMC_CAP_4_BIT_DATA;
8302d1731b8SIan Lepore 	if (caps & SDHCI_CAN_DO_8BITBUS)
8310f34084fSMarius Strobl 		host_caps |= MMC_CAP_8_BIT_DATA;
832831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
8330f34084fSMarius Strobl 		host_caps |= MMC_CAP_HSPEED;
83472dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC)
8350f34084fSMarius Strobl 		host_caps |= MMC_CAP_BOOT_NOACC;
83672dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY)
8370f34084fSMarius Strobl 		host_caps |= MMC_CAP_WAIT_WHILE_BUSY;
838*aca38eabSMarius Strobl 
839*aca38eabSMarius Strobl 	/* Determine supported UHS-I and eMMC modes. */
8400f34084fSMarius Strobl 	if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50))
8410f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
8420f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_SDR104) {
8430f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
8440f34084fSMarius Strobl 		if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200))
8450f34084fSMarius Strobl 			host_caps |= MMC_CAP_MMC_HS200;
8460f34084fSMarius Strobl 	} else if (caps2 & SDHCI_CAN_SDR50)
8470f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR50;
8480f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_DDR50 &&
8490f34084fSMarius Strobl 	    !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50))
8500f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_DDR50;
8510f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_DDR52)
8520f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_DDR52;
8530f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 &&
8540f34084fSMarius Strobl 	    caps2 & SDHCI_CAN_MMC_HS400)
8550f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
856*aca38eabSMarius Strobl 
857*aca38eabSMarius Strobl 	/*
858*aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes if the set_uhs_timing method is the
859*aca38eabSMarius Strobl 	 * default NULL implementation.
860*aca38eabSMarius Strobl 	 */
861*aca38eabSMarius Strobl 	kobj_desc = &sdhci_set_uhs_timing_desc;
862*aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
863*aca38eabSMarius Strobl 	    kobj_desc);
864*aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
865*aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
866*aca38eabSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
867*aca38eabSMarius Strobl 		    MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400);
868*aca38eabSMarius Strobl 
869*aca38eabSMarius Strobl #define	SDHCI_CAP_MODES_TUNING(caps2)					\
870*aca38eabSMarius Strobl     (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) |		\
871*aca38eabSMarius Strobl     MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 |	\
872*aca38eabSMarius Strobl     MMC_CAP_MMC_HS400)
873*aca38eabSMarius Strobl 
874*aca38eabSMarius Strobl 	/*
875*aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes that require (re-)tuning if either
876*aca38eabSMarius Strobl 	 * the tune or re-tune method is the default NULL implementation.
877*aca38eabSMarius Strobl 	 */
878*aca38eabSMarius Strobl 	kobj_desc = &mmcbr_tune_desc;
879*aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
880*aca38eabSMarius Strobl 	    kobj_desc);
881*aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
882*aca38eabSMarius Strobl 		goto no_tuning;
883*aca38eabSMarius Strobl 	kobj_desc = &mmcbr_retune_desc;
884*aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
885*aca38eabSMarius Strobl 	    kobj_desc);
886*aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt) {
887*aca38eabSMarius Strobl no_tuning:
888*aca38eabSMarius Strobl 		host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2));
889*aca38eabSMarius Strobl 	}
890*aca38eabSMarius Strobl 
891*aca38eabSMarius Strobl 	/* Allocate tuning structures and determine tuning parameters. */
892*aca38eabSMarius Strobl 	if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) {
893*aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_SUPPORTED;
894*aca38eabSMarius Strobl 		slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF,
895*aca38eabSMarius Strobl 		    M_WAITOK);
896*aca38eabSMarius Strobl 		slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF,
897*aca38eabSMarius Strobl 		    M_WAITOK);
898*aca38eabSMarius Strobl 		slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF,
899*aca38eabSMarius Strobl 		    M_WAITOK);
900*aca38eabSMarius Strobl 		if (caps2 & SDHCI_TUNE_SDR50)
901*aca38eabSMarius Strobl 			slot->opt |= SDHCI_SDR50_NEEDS_TUNING;
902*aca38eabSMarius Strobl 		slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >>
903*aca38eabSMarius Strobl 		    SDHCI_RETUNE_MODES_SHIFT;
904*aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_1) {
905*aca38eabSMarius Strobl 			slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >>
906*aca38eabSMarius Strobl 			    SDHCI_RETUNE_CNT_SHIFT;
907*aca38eabSMarius Strobl 			if (slot->retune_count > 0xb) {
908*aca38eabSMarius Strobl 				device_printf(dev, "Unknown re-tuning count "
909*aca38eabSMarius Strobl 				    "%x, using 1 sec\n", slot->retune_count);
910*aca38eabSMarius Strobl 				slot->retune_count = 1;
911*aca38eabSMarius Strobl 			} else if (slot->retune_count != 0)
912*aca38eabSMarius Strobl 				slot->retune_count =
913*aca38eabSMarius Strobl 				    1 << (slot->retune_count - 1);
914*aca38eabSMarius Strobl 		}
915*aca38eabSMarius Strobl 	}
916*aca38eabSMarius Strobl 
917*aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING
918*aca38eabSMarius Strobl 
919*aca38eabSMarius Strobl 	/* Determine supported VCCQ signaling levels. */
9200f34084fSMarius Strobl 	host_caps |= MMC_CAP_SIGNALING_330;
9210f34084fSMarius Strobl 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
922*aca38eabSMarius Strobl 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
9230f34084fSMarius Strobl 	    MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 |
9240f34084fSMarius Strobl 	    MMC_CAP_MMC_HS400_180))
925*aca38eabSMarius Strobl 		host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180;
926*aca38eabSMarius Strobl 
927*aca38eabSMarius Strobl 	/*
928*aca38eabSMarius Strobl 	 * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the
929*aca38eabSMarius Strobl 	 * default NULL implementation.  Disable 1.2 V support if it's the
930*aca38eabSMarius Strobl 	 * generic SDHCI implementation.
931*aca38eabSMarius Strobl 	 */
932*aca38eabSMarius Strobl 	kobj_desc = &mmcbr_switch_vccq_desc;
933*aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
934*aca38eabSMarius Strobl 	    kobj_desc);
935*aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
936*aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180);
937*aca38eabSMarius Strobl 	else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq)
938*aca38eabSMarius Strobl 		host_caps &= ~MMC_CAP_SIGNALING_120;
939*aca38eabSMarius Strobl 
940*aca38eabSMarius Strobl 	/* Determine supported driver types (type B is always mandatory). */
941f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_A)
9420f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_A;
943f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_C)
9440f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_C;
945f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_D)
9460f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_D;
9470f34084fSMarius Strobl 	slot->host.caps = host_caps;
9480f34084fSMarius Strobl 
949831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
950831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
951831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
952d6b3aaf8SOleksandr Tymoshenko 
953d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
954831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
955d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
956831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
957a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE)
958a2832f9fSMarius Strobl 		slot->opt |= SDHCI_NON_REMOVABLE;
959831f5dcfSAlexander Motin 
960c3a0f75aSOleksandr Tymoshenko 	/*
961c3a0f75aSOleksandr Tymoshenko 	 * Use platform-provided transfer backend
962c3a0f75aSOleksandr Tymoshenko 	 * with PIO as a fallback mechanism
963c3a0f75aSOleksandr Tymoshenko 	 */
964c3a0f75aSOleksandr Tymoshenko 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
965c3a0f75aSOleksandr Tymoshenko 		slot->opt &= ~SDHCI_HAVE_DMA;
966c3a0f75aSOleksandr Tymoshenko 
9675b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
9680f34084fSMarius Strobl 		slot_printf(slot,
9690f34084fSMarius Strobl 		    "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s\n",
970831f5dcfSAlexander Motin 		    slot->max_clk / 1000000,
971831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
9720f34084fSMarius Strobl 		    (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
9730f34084fSMarius Strobl 			((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
974831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
975831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
976831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "",
9770f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "",
9780f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "",
979*aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "",
980*aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "",
981*aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "",
982831f5dcfSAlexander Motin 		    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO");
9830f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 |
9840f34084fSMarius Strobl 		    MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE))
9850f34084fSMarius Strobl 			slot_printf(slot, "eMMC:%s%s%s%s\n",
9860f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "",
9870f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "",
9880f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "",
9890f34084fSMarius Strobl 			    ((host_caps &
9900f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ==
9910f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ?
9920f34084fSMarius Strobl 			    " HS400ES" : "");
9930f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
9940f34084fSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104))
9950f34084fSMarius Strobl 			slot_printf(slot, "UHS-I:%s%s%s%s%s\n",
9960f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "",
9970f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "",
9980f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "",
9990f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "",
10000f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : "");
1001*aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_SUPPORTED)
1002*aca38eabSMarius Strobl 			slot_printf(slot, "Re-tuning count %d secs, mode %d\n",
1003*aca38eabSMarius Strobl 			    slot->retune_count, slot->retune_mode + 1);
1004831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1005831f5dcfSAlexander Motin 	}
1006831f5dcfSAlexander Motin 
1007ba6fc1c7SLuiz Otavio O Souza 	slot->timeout = 10;
1008ba6fc1c7SLuiz Otavio O Souza 	SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
1009ba6fc1c7SLuiz Otavio O Souza 	    SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
1010ba6fc1c7SLuiz Otavio O Souza 	    "timeout", CTLFLAG_RW, &slot->timeout, 0,
1011ba6fc1c7SLuiz Otavio O Souza 	    "Maximum timeout for SDHCI transfers (in secs)");
1012831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
1013639f59f0SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
1014639f59f0SIan Lepore 		sdhci_card_task, slot);
1015639f59f0SIan Lepore 	callout_init(&slot->card_poll_callout, 1);
1016e64f01a9SIan Lepore 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
1017*aca38eabSMarius Strobl 	callout_init_mtx(&slot->retune_callout, &slot->mtx, 0);
1018ba6fc1c7SLuiz Otavio O Souza 
1019639f59f0SIan Lepore 	if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
1020639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
1021639f59f0SIan Lepore 		callout_reset(&slot->card_poll_callout,
1022639f59f0SIan Lepore 		    SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
1023639f59f0SIan Lepore 	}
1024639f59f0SIan Lepore 
1025*aca38eabSMarius Strobl 	sdhci_init(slot);
1026*aca38eabSMarius Strobl 
1027831f5dcfSAlexander Motin 	return (0);
1028831f5dcfSAlexander Motin }
1029831f5dcfSAlexander Motin 
1030d6b3aaf8SOleksandr Tymoshenko void
1031d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
1032831f5dcfSAlexander Motin {
10337e6ccea3SMarius Strobl 
1034d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
1035d6b3aaf8SOleksandr Tymoshenko }
1036831f5dcfSAlexander Motin 
1037d6b3aaf8SOleksandr Tymoshenko int
1038d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
1039d6b3aaf8SOleksandr Tymoshenko {
1040831f5dcfSAlexander Motin 	device_t d;
1041831f5dcfSAlexander Motin 
1042e64f01a9SIan Lepore 	callout_drain(&slot->timeout_callout);
1043639f59f0SIan Lepore 	callout_drain(&slot->card_poll_callout);
1044*aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1045831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
1046639f59f0SIan Lepore 	taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
1047831f5dcfSAlexander Motin 
1048831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1049831f5dcfSAlexander Motin 	d = slot->dev;
1050831f5dcfSAlexander Motin 	slot->dev = NULL;
1051831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1052831f5dcfSAlexander Motin 	if (d != NULL)
1053d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
1054831f5dcfSAlexander Motin 
1055831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1056831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
1057831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1058831f5dcfSAlexander Motin 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
1059831f5dcfSAlexander Motin 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
1060831f5dcfSAlexander Motin 	bus_dma_tag_destroy(slot->dmatag);
1061*aca38eabSMarius Strobl 	if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1062*aca38eabSMarius Strobl 		free(slot->tune_req, M_DEVBUF);
1063*aca38eabSMarius Strobl 		free(slot->tune_cmd, M_DEVBUF);
1064*aca38eabSMarius Strobl 		free(slot->tune_data, M_DEVBUF);
1065*aca38eabSMarius Strobl 	}
1066d6b3aaf8SOleksandr Tymoshenko 
1067831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
1068d6b3aaf8SOleksandr Tymoshenko 
1069831f5dcfSAlexander Motin 	return (0);
1070831f5dcfSAlexander Motin }
1071831f5dcfSAlexander Motin 
1072d6b3aaf8SOleksandr Tymoshenko int
1073d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
107492bf0e27SAlexander Motin {
10757e6ccea3SMarius Strobl 
1076*aca38eabSMarius Strobl 	/*
1077*aca38eabSMarius Strobl 	 * We expect the MMC layer to issue initial tuning after resume.
1078*aca38eabSMarius Strobl 	 * Otherwise, we'd need to indicate re-tuning including circuit reset
1079*aca38eabSMarius Strobl 	 * being required at least for re-tuning modes 1 and 2 ourselves.
1080*aca38eabSMarius Strobl 	 */
1081*aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1082*aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1083*aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1084d6b3aaf8SOleksandr Tymoshenko 	sdhci_reset(slot, SDHCI_RESET_ALL);
1085*aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
108692bf0e27SAlexander Motin 
108792bf0e27SAlexander Motin 	return (0);
108892bf0e27SAlexander Motin }
108992bf0e27SAlexander Motin 
1090d6b3aaf8SOleksandr Tymoshenko int
1091d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
109292bf0e27SAlexander Motin {
10937e6ccea3SMarius Strobl 
1094*aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1095d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
1096*aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
109792bf0e27SAlexander Motin 
1098d6b3aaf8SOleksandr Tymoshenko 	return (0);
109992bf0e27SAlexander Motin }
110092bf0e27SAlexander Motin 
110157677a3aSOleksandr Tymoshenko uint32_t
1102b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot)
110357677a3aSOleksandr Tymoshenko {
11047e6ccea3SMarius Strobl 
110557677a3aSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
110657677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
110757677a3aSOleksandr Tymoshenko 	else
110857677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
110957677a3aSOleksandr Tymoshenko }
111057677a3aSOleksandr Tymoshenko 
11116e37fb2bSIan Lepore bool
1112b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot)
11136e37fb2bSIan Lepore {
11146e37fb2bSIan Lepore 
1115639f59f0SIan Lepore 	if (slot->opt & SDHCI_NON_REMOVABLE)
1116639f59f0SIan Lepore 		return true;
1117639f59f0SIan Lepore 
11186e37fb2bSIan Lepore 	return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
11196e37fb2bSIan Lepore }
11206e37fb2bSIan Lepore 
11210f34084fSMarius Strobl void
11220f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot)
11230f34084fSMarius Strobl {
11240f34084fSMarius Strobl 	struct mmc_ios *ios;
11250f34084fSMarius Strobl 	uint16_t hostctrl2;
11260f34084fSMarius Strobl 
11270f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
11280f34084fSMarius Strobl 		return;
11290f34084fSMarius Strobl 
1130*aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
11310f34084fSMarius Strobl 	ios = &slot->host.ios;
11320f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
11330f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
11340f34084fSMarius Strobl 	hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK;
1135*aca38eabSMarius Strobl 	if (ios->clock > SD_SDR50_MAX) {
11360f34084fSMarius Strobl 		if (ios->timing == bus_timing_mmc_hs400 ||
11370f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_hs400es)
11380f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_MMC_HS400;
1139*aca38eabSMarius Strobl 		else
11400f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR104;
1141*aca38eabSMarius Strobl 	}
11420f34084fSMarius Strobl 	else if (ios->clock > SD_SDR25_MAX)
11430f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR50;
11440f34084fSMarius Strobl 	else if (ios->clock > SD_SDR12_MAX) {
11450f34084fSMarius Strobl 		if (ios->timing == bus_timing_uhs_ddr50 ||
11460f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_ddr52)
11470f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_DDR50;
11480f34084fSMarius Strobl 		else
11490f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR25;
11500f34084fSMarius Strobl 	} else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY)
11510f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR12;
11520f34084fSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
11530f34084fSMarius Strobl 	sdhci_set_clock(slot, ios->clock);
11540f34084fSMarius Strobl }
11550f34084fSMarius Strobl 
1156d6b3aaf8SOleksandr Tymoshenko int
1157d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
1158831f5dcfSAlexander Motin {
1159831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1160831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
1161831f5dcfSAlexander Motin 
1162831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1163831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
1164831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
1165831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
1166831f5dcfSAlexander Motin 		sdhci_init(slot);
1167831f5dcfSAlexander Motin 	}
1168831f5dcfSAlexander Motin 	/* Configure the bus. */
1169831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
1170831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
11712d1731b8SIan Lepore 	if (ios->bus_width == bus_width_8) {
11722d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
1173831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
11742d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_4) {
11752d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
11762d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
11772d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_1) {
11782d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
11792d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
11802d1731b8SIan Lepore 	} else {
11812d1731b8SIan Lepore 		panic("Invalid bus width: %d", ios->bus_width);
11822d1731b8SIan Lepore 	}
11830f34084fSMarius Strobl 	if (ios->clock > SD_SDR12_MAX &&
1184bba987dcSIan Lepore 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
1185831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
1186831f5dcfSAlexander Motin 	else
1187831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
1188831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
11890f34084fSMarius Strobl 	SDHCI_SET_UHS_TIMING(brdev, slot);
1190831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
1191d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
1192831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1193831f5dcfSAlexander Motin 
1194831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1195831f5dcfSAlexander Motin 	return (0);
1196831f5dcfSAlexander Motin }
1197831f5dcfSAlexander Motin 
11980f34084fSMarius Strobl int
11990f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev)
12000f34084fSMarius Strobl {
12010f34084fSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
12020f34084fSMarius Strobl 	enum mmc_vccq vccq;
12030f34084fSMarius Strobl 	int err;
12040f34084fSMarius Strobl 	uint16_t hostctrl2;
12050f34084fSMarius Strobl 
12060f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
12070f34084fSMarius Strobl 		return (0);
12080f34084fSMarius Strobl 
12090f34084fSMarius Strobl 	err = 0;
12100f34084fSMarius Strobl 	vccq = slot->host.ios.vccq;
12110f34084fSMarius Strobl 	SDHCI_LOCK(slot);
12120f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
12130f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
12140f34084fSMarius Strobl 	switch (vccq) {
12150f34084fSMarius Strobl 	case vccq_330:
12160f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
12170f34084fSMarius Strobl 			goto done;
12180f34084fSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE;
12190f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
12200f34084fSMarius Strobl 		DELAY(5000);
12210f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
12220f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
12230f34084fSMarius Strobl 			goto done;
12240f34084fSMarius Strobl 		err = EAGAIN;
12250f34084fSMarius Strobl 		break;
12260f34084fSMarius Strobl 	case vccq_180:
12270f34084fSMarius Strobl 		if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) {
12280f34084fSMarius Strobl 			err = EINVAL;
12290f34084fSMarius Strobl 			goto done;
12300f34084fSMarius Strobl 		}
12310f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
12320f34084fSMarius Strobl 			goto done;
12330f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_S18_ENABLE;
12340f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
12350f34084fSMarius Strobl 		DELAY(5000);
12360f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
12370f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
12380f34084fSMarius Strobl 			goto done;
12390f34084fSMarius Strobl 		err = EAGAIN;
12400f34084fSMarius Strobl 		break;
12410f34084fSMarius Strobl 	default:
12420f34084fSMarius Strobl 		slot_printf(slot,
12430f34084fSMarius Strobl 		    "Attempt to set unsupported signaling voltage\n");
12440f34084fSMarius Strobl 		err = EINVAL;
12450f34084fSMarius Strobl 		break;
12460f34084fSMarius Strobl 	}
12470f34084fSMarius Strobl done:
12480f34084fSMarius Strobl 	sdhci_set_clock(slot, slot->host.ios.clock);
12490f34084fSMarius Strobl 	SDHCI_UNLOCK(slot);
12500f34084fSMarius Strobl 	return (err);
12510f34084fSMarius Strobl }
12520f34084fSMarius Strobl 
1253*aca38eabSMarius Strobl int
1254*aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400)
1255*aca38eabSMarius Strobl {
1256*aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1257*aca38eabSMarius Strobl 	struct mmc_ios *ios = &slot->host.ios;
1258*aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1259*aca38eabSMarius Strobl 	struct mmc_data *tune_data;
1260*aca38eabSMarius Strobl 	uint32_t opcode;
1261*aca38eabSMarius Strobl 	int err;
1262*aca38eabSMarius Strobl 
1263*aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_SUPPORTED))
1264*aca38eabSMarius Strobl 		return (0);
1265*aca38eabSMarius Strobl 
1266*aca38eabSMarius Strobl 	slot->retune_ticks = slot->retune_count * hz;
1267*aca38eabSMarius Strobl 	opcode = MMC_SEND_TUNING_BLOCK;
1268*aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1269*aca38eabSMarius Strobl 	switch (ios->timing) {
1270*aca38eabSMarius Strobl 	case bus_timing_mmc_hs400:
1271*aca38eabSMarius Strobl 		slot_printf(slot, "HS400 must be tuned in HS200 mode\n");
1272*aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1273*aca38eabSMarius Strobl 		return (EINVAL);
1274*aca38eabSMarius Strobl 	case bus_timing_mmc_hs200:
1275*aca38eabSMarius Strobl 		/*
1276*aca38eabSMarius Strobl 		 * In HS400 mode, controllers use the data strobe line to
1277*aca38eabSMarius Strobl 		 * latch data from the devices so periodic re-tuning isn't
1278*aca38eabSMarius Strobl 		 * expected to be required.
1279*aca38eabSMarius Strobl 		 */
1280*aca38eabSMarius Strobl 		if (hs400)
1281*aca38eabSMarius Strobl 			slot->retune_ticks = 0;
1282*aca38eabSMarius Strobl 		opcode = MMC_SEND_TUNING_BLOCK_HS200;
1283*aca38eabSMarius Strobl 		break;
1284*aca38eabSMarius Strobl 	case bus_timing_uhs_ddr50:
1285*aca38eabSMarius Strobl 	case bus_timing_uhs_sdr104:
1286*aca38eabSMarius Strobl 		break;
1287*aca38eabSMarius Strobl 	case bus_timing_uhs_sdr50:
1288*aca38eabSMarius Strobl 		if (slot->opt & SDHCI_SDR50_NEEDS_TUNING)
1289*aca38eabSMarius Strobl 			break;
1290*aca38eabSMarius Strobl 		/* FALLTHROUGH */
1291*aca38eabSMarius Strobl 	default:
1292*aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1293*aca38eabSMarius Strobl 		return (0);
1294*aca38eabSMarius Strobl 	}
1295*aca38eabSMarius Strobl 
1296*aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1297*aca38eabSMarius Strobl 	memset(tune_cmd, 0, sizeof(*tune_cmd));
1298*aca38eabSMarius Strobl 	tune_cmd->opcode = opcode;
1299*aca38eabSMarius Strobl 	tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1300*aca38eabSMarius Strobl 	tune_data = tune_cmd->data = slot->tune_data;
1301*aca38eabSMarius Strobl 	memset(tune_data, 0, sizeof(*tune_data));
1302*aca38eabSMarius Strobl 	tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
1303*aca38eabSMarius Strobl 	    ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 :
1304*aca38eabSMarius Strobl 	    MMC_TUNING_LEN;
1305*aca38eabSMarius Strobl 	tune_data->flags = MMC_DATA_READ;
1306*aca38eabSMarius Strobl 	tune_data->mrq = tune_cmd->mrq = slot->tune_req;
1307*aca38eabSMarius Strobl 
1308*aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1309*aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, true);
1310*aca38eabSMarius Strobl 	if (err == 0) {
1311*aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_ENABLED;
1312*aca38eabSMarius Strobl 		slot->intmask |= sdhci_tuning_intmask(slot);
1313*aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1314*aca38eabSMarius Strobl 		if (slot->retune_ticks) {
1315*aca38eabSMarius Strobl 			callout_reset(&slot->retune_callout, slot->retune_ticks,
1316*aca38eabSMarius Strobl 			    sdhci_retune, slot);
1317*aca38eabSMarius Strobl 		}
1318*aca38eabSMarius Strobl 	}
1319*aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1320*aca38eabSMarius Strobl 	return (err);
1321*aca38eabSMarius Strobl }
1322*aca38eabSMarius Strobl 
1323*aca38eabSMarius Strobl int
1324*aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset)
1325*aca38eabSMarius Strobl {
1326*aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1327*aca38eabSMarius Strobl 	int err;
1328*aca38eabSMarius Strobl 
1329*aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_ENABLED))
1330*aca38eabSMarius Strobl 		return (0);
1331*aca38eabSMarius Strobl 
1332*aca38eabSMarius Strobl 	/* HS400 must be tuned in HS200 mode. */
1333*aca38eabSMarius Strobl 	if (slot->host.ios.timing == bus_timing_mmc_hs400)
1334*aca38eabSMarius Strobl 		return (EINVAL);
1335*aca38eabSMarius Strobl 
1336*aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1337*aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, reset);
1338*aca38eabSMarius Strobl 	/*
1339*aca38eabSMarius Strobl 	 * There are two ways sdhci_exec_tuning() can fail:
1340*aca38eabSMarius Strobl 	 * EBUSY should not actually happen when requests are only issued
1341*aca38eabSMarius Strobl 	 *	 with the host properly acquired, and
1342*aca38eabSMarius Strobl 	 * EIO   re-tuning failed (but it did work initially).
1343*aca38eabSMarius Strobl 	 *
1344*aca38eabSMarius Strobl 	 * In both cases, we should retry at later point if periodic re-tuning
1345*aca38eabSMarius Strobl 	 * is enabled.  Note that due to slot->retune_req not being cleared in
1346*aca38eabSMarius Strobl 	 * these failure cases, the MMC layer should trigger another attempt at
1347*aca38eabSMarius Strobl 	 * re-tuning with the next request anyway, though.
1348*aca38eabSMarius Strobl 	 */
1349*aca38eabSMarius Strobl 	if (slot->retune_ticks) {
1350*aca38eabSMarius Strobl 		callout_reset(&slot->retune_callout, slot->retune_ticks,
1351*aca38eabSMarius Strobl 		    sdhci_retune, slot);
1352*aca38eabSMarius Strobl 	}
1353*aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1354*aca38eabSMarius Strobl 	return (err);
1355*aca38eabSMarius Strobl }
1356*aca38eabSMarius Strobl 
1357*aca38eabSMarius Strobl static int
1358*aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset)
1359*aca38eabSMarius Strobl {
1360*aca38eabSMarius Strobl 	struct mmc_request *tune_req;
1361*aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1362*aca38eabSMarius Strobl 	int i;
1363*aca38eabSMarius Strobl 	uint32_t intmask;
1364*aca38eabSMarius Strobl 	uint16_t hostctrl2;
1365*aca38eabSMarius Strobl 	u_char opt;
1366*aca38eabSMarius Strobl 
1367*aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
1368*aca38eabSMarius Strobl 	if (slot->req != NULL)
1369*aca38eabSMarius Strobl 		return (EBUSY);
1370*aca38eabSMarius Strobl 
1371*aca38eabSMarius Strobl 	/* Tuning doesn't work with DMA enabled. */
1372*aca38eabSMarius Strobl 	opt = slot->opt;
1373*aca38eabSMarius Strobl 	slot->opt = opt & ~SDHCI_HAVE_DMA;
1374*aca38eabSMarius Strobl 
1375*aca38eabSMarius Strobl 	/*
1376*aca38eabSMarius Strobl 	 * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only
1377*aca38eabSMarius Strobl 	 * kind of interrupt we receive in response to a tuning request.
1378*aca38eabSMarius Strobl 	 */
1379*aca38eabSMarius Strobl 	intmask = slot->intmask;
1380*aca38eabSMarius Strobl 	slot->intmask = SDHCI_INT_DATA_AVAIL;
1381*aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL);
1382*aca38eabSMarius Strobl 
1383*aca38eabSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1384*aca38eabSMarius Strobl 	if (reset)
1385*aca38eabSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK;
1386*aca38eabSMarius Strobl 	else
1387*aca38eabSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK;
1388*aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING);
1389*aca38eabSMarius Strobl 
1390*aca38eabSMarius Strobl 	tune_req = slot->tune_req;
1391*aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1392*aca38eabSMarius Strobl 	for (i = 0; i < MMC_TUNING_MAX; i++) {
1393*aca38eabSMarius Strobl 		memset(tune_req, 0, sizeof(*tune_req));
1394*aca38eabSMarius Strobl 		tune_req->cmd = tune_cmd;
1395*aca38eabSMarius Strobl 		tune_req->done = sdhci_req_wakeup;
1396*aca38eabSMarius Strobl 		tune_req->done_data = slot;
1397*aca38eabSMarius Strobl 		slot->req = tune_req;
1398*aca38eabSMarius Strobl 		slot->flags = 0;
1399*aca38eabSMarius Strobl 		sdhci_start(slot);
1400*aca38eabSMarius Strobl 		while (!(tune_req->flags & MMC_REQ_DONE))
1401*aca38eabSMarius Strobl 			msleep(tune_req, &slot->mtx, 0, "sdhciet", 0);
1402*aca38eabSMarius Strobl 		if (!(tune_req->flags & MMC_TUNE_DONE))
1403*aca38eabSMarius Strobl 			break;
1404*aca38eabSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1405*aca38eabSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING))
1406*aca38eabSMarius Strobl 			break;
1407*aca38eabSMarius Strobl 		if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK)
1408*aca38eabSMarius Strobl 			DELAY(1000);
1409*aca38eabSMarius Strobl 	}
1410*aca38eabSMarius Strobl 
1411*aca38eabSMarius Strobl 	slot->opt = opt;
1412*aca38eabSMarius Strobl 	slot->intmask = intmask;
1413*aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, intmask);
1414*aca38eabSMarius Strobl 
1415*aca38eabSMarius Strobl 	if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING |
1416*aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) {
1417*aca38eabSMarius Strobl 		slot->retune_req = 0;
1418*aca38eabSMarius Strobl 		return (0);
1419*aca38eabSMarius Strobl 	}
1420*aca38eabSMarius Strobl 
1421*aca38eabSMarius Strobl 	slot_printf(slot, "Tuning failed, using fixed sampling clock\n");
1422*aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING |
1423*aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK));
1424*aca38eabSMarius Strobl 	sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1425*aca38eabSMarius Strobl 	return (EIO);
1426*aca38eabSMarius Strobl }
1427*aca38eabSMarius Strobl 
1428*aca38eabSMarius Strobl static void
1429*aca38eabSMarius Strobl sdhci_retune(void *arg)
1430*aca38eabSMarius Strobl {
1431*aca38eabSMarius Strobl 	struct sdhci_slot *slot = arg;
1432*aca38eabSMarius Strobl 
1433*aca38eabSMarius Strobl 	slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
1434*aca38eabSMarius Strobl }
1435*aca38eabSMarius Strobl 
1436a94a63f0SWarner Losh #ifdef MMCCAM
1437a94a63f0SWarner Losh static void
1438a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot)
1439a94a63f0SWarner Losh {
1440a94a63f0SWarner Losh         union ccb *ccb;
144115c440e1SWarner Losh 
1442*aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
144315c440e1SWarner Losh 		slot_printf(slot, "%s\n", __func__);
1444a94a63f0SWarner Losh 	if (slot->ccb != NULL && slot->curcmd != NULL) {
1445a94a63f0SWarner Losh 		callout_stop(&slot->timeout_callout);
1446a94a63f0SWarner Losh                 ccb = slot->ccb;
1447a94a63f0SWarner Losh                 slot->ccb = NULL;
1448a94a63f0SWarner Losh 		slot->curcmd = NULL;
1449a94a63f0SWarner Losh 
1450a94a63f0SWarner Losh                 /* Tell CAM the request is finished */
1451a94a63f0SWarner Losh                 struct ccb_mmcio *mmcio;
1452a94a63f0SWarner Losh                 mmcio = &ccb->mmcio;
1453a94a63f0SWarner Losh 
1454a94a63f0SWarner Losh                 ccb->ccb_h.status =
1455a94a63f0SWarner Losh                         (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR);
1456a94a63f0SWarner Losh                 xpt_done(ccb);
1457a94a63f0SWarner Losh 	}
1458a94a63f0SWarner Losh }
1459a94a63f0SWarner Losh #else
1460831f5dcfSAlexander Motin static void
1461e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot)
1462e64f01a9SIan Lepore {
1463e64f01a9SIan Lepore 	struct mmc_request *req;
1464e64f01a9SIan Lepore 
1465e64f01a9SIan Lepore 	if (slot->req != NULL && slot->curcmd != NULL) {
1466e64f01a9SIan Lepore 		callout_stop(&slot->timeout_callout);
1467e64f01a9SIan Lepore 		req = slot->req;
1468e64f01a9SIan Lepore 		slot->req = NULL;
1469e64f01a9SIan Lepore 		slot->curcmd = NULL;
1470e64f01a9SIan Lepore 		req->done(req);
1471e64f01a9SIan Lepore 	}
1472e64f01a9SIan Lepore }
1473a94a63f0SWarner Losh #endif
1474e64f01a9SIan Lepore 
1475e64f01a9SIan Lepore static void
1476*aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req)
1477*aca38eabSMarius Strobl {
1478*aca38eabSMarius Strobl 	struct sdhci_slot *slot;
1479*aca38eabSMarius Strobl 
1480*aca38eabSMarius Strobl 	slot = req->done_data;
1481*aca38eabSMarius Strobl 	req->flags |= MMC_REQ_DONE;
1482*aca38eabSMarius Strobl 	wakeup(req);
1483*aca38eabSMarius Strobl }
1484*aca38eabSMarius Strobl 
1485*aca38eabSMarius Strobl static void
1486e64f01a9SIan Lepore sdhci_timeout(void *arg)
1487e64f01a9SIan Lepore {
1488e64f01a9SIan Lepore 	struct sdhci_slot *slot = arg;
1489e64f01a9SIan Lepore 
1490e64f01a9SIan Lepore 	if (slot->curcmd != NULL) {
14917e586643SIan Lepore 		slot_printf(slot, "Controller timeout\n");
14927e586643SIan Lepore 		sdhci_dumpregs(slot);
1493a6873fd1SIan Lepore 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1494e64f01a9SIan Lepore 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1495e64f01a9SIan Lepore 		sdhci_req_done(slot);
14967e586643SIan Lepore 	} else {
14977e586643SIan Lepore 		slot_printf(slot, "Spurious timeout - no active command\n");
1498e64f01a9SIan Lepore 	}
1499e64f01a9SIan Lepore }
1500e64f01a9SIan Lepore 
1501e64f01a9SIan Lepore static void
1502b440e965SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, struct mmc_data *data)
1503831f5dcfSAlexander Motin {
1504831f5dcfSAlexander Motin 	uint16_t mode;
1505831f5dcfSAlexander Motin 
1506831f5dcfSAlexander Motin 	if (data == NULL)
1507831f5dcfSAlexander Motin 		return;
1508831f5dcfSAlexander Motin 
1509831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
1510831f5dcfSAlexander Motin 	if (data->len > 512)
1511831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
1512831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
1513831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
1514a94a63f0SWarner Losh #ifdef MMCCAM
1515a94a63f0SWarner Losh 	struct ccb_mmcio *mmcio;
1516a94a63f0SWarner Losh 	mmcio = &slot->ccb->mmcio;
1517a94a63f0SWarner Losh 	if (mmcio->stop.opcode == MMC_STOP_TRANSMISSION
1518a94a63f0SWarner Losh 	    && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))
1519a94a63f0SWarner Losh 		mode |= SDHCI_TRNS_ACMD12;
1520a94a63f0SWarner Losh #else
1521915780d7SLuiz Otavio O Souza 	if (slot->req->stop && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))
1522831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_ACMD12;
1523a94a63f0SWarner Losh #endif
1524831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
1525831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
1526831f5dcfSAlexander Motin 
1527831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
1528831f5dcfSAlexander Motin }
1529831f5dcfSAlexander Motin 
1530831f5dcfSAlexander Motin static void
1531831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
1532831f5dcfSAlexander Motin {
1533831f5dcfSAlexander Motin 	int flags, timeout;
153490993663SIan Lepore 	uint32_t mask;
1535831f5dcfSAlexander Motin 
1536831f5dcfSAlexander Motin 	slot->curcmd = cmd;
1537831f5dcfSAlexander Motin 	slot->cmd_done = 0;
1538831f5dcfSAlexander Motin 
1539831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
1540831f5dcfSAlexander Motin 
1541831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
1542831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1543831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
1544831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1545e64f01a9SIan Lepore 		sdhci_req_done(slot);
1546831f5dcfSAlexander Motin 		return;
1547831f5dcfSAlexander Motin 	}
1548831f5dcfSAlexander Motin 
1549b440e965SMarius Strobl 	/*
1550b440e965SMarius Strobl 	 * Do not issue command if there is no card, clock or power.
1551b440e965SMarius Strobl 	 * Controller will not detect timeout without clock active.
1552b440e965SMarius Strobl 	 */
15536e37fb2bSIan Lepore 	if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
1554d8208d9eSAlexander Motin 	    slot->power == 0 ||
1555d8208d9eSAlexander Motin 	    slot->clock == 0) {
1556a94a63f0SWarner Losh 		slot_printf(slot,
1557a94a63f0SWarner Losh 			    "Cannot issue a command (power=%d clock=%d)",
1558a94a63f0SWarner Losh 			    slot->power, slot->clock);
1559831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1560e64f01a9SIan Lepore 		sdhci_req_done(slot);
1561831f5dcfSAlexander Motin 		return;
1562831f5dcfSAlexander Motin 	}
1563831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
1564831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
1565831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
1566a94a63f0SWarner Losh 	if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY))
1567831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
1568*aca38eabSMarius Strobl 	/*
1569*aca38eabSMarius Strobl 	 * We shouldn't wait for DAT for stop commands or CMD19/CMD21.  Note
1570*aca38eabSMarius Strobl 	 * that these latter are also special in that SDHCI_CMD_DATA should
1571*aca38eabSMarius Strobl 	 * be set below but no actual data is ever read from the controller.
1572*aca38eabSMarius Strobl 	*/
1573a94a63f0SWarner Losh #ifdef MMCCAM
1574*aca38eabSMarius Strobl 	if (cmd == &slot->ccb->mmcio.stop ||
1575a94a63f0SWarner Losh #else
1576*aca38eabSMarius Strobl 	if (cmd == slot->req->stop ||
1577a94a63f0SWarner Losh #endif
1578*aca38eabSMarius Strobl 	    __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1579*aca38eabSMarius Strobl 	    cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))
1580*aca38eabSMarius Strobl 		mask &= ~SDHCI_DAT_INHIBIT;
15818775ab45SIan Lepore 	/*
15828775ab45SIan Lepore 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
15838775ab45SIan Lepore 	 *  here at all, but when writing a crash dump we may be bypassing the
15848775ab45SIan Lepore 	 *  host platform's interrupt handler, and in some cases that handler
15858775ab45SIan Lepore 	 *  may be working around hardware quirks such as not respecting r1b
15868775ab45SIan Lepore 	 *  busy indications.  In those cases, this wait-loop serves the purpose
15878775ab45SIan Lepore 	 *  of waiting for the prior command and data transfers to be done, and
15888775ab45SIan Lepore 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
15898775ab45SIan Lepore 	 *  (It's usually more like 20-30ms in the real world.)
15908775ab45SIan Lepore 	 */
15918775ab45SIan Lepore 	timeout = 250;
159290993663SIan Lepore 	while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
1593831f5dcfSAlexander Motin 		if (timeout == 0) {
1594831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
1595831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
1596831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
1597831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
1598e64f01a9SIan Lepore 			sdhci_req_done(slot);
1599831f5dcfSAlexander Motin 			return;
1600831f5dcfSAlexander Motin 		}
1601831f5dcfSAlexander Motin 		timeout--;
1602831f5dcfSAlexander Motin 		DELAY(1000);
1603831f5dcfSAlexander Motin 	}
1604831f5dcfSAlexander Motin 
1605831f5dcfSAlexander Motin 	/* Prepare command flags. */
1606831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
1607831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
1608831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
1609831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
1610831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
1611831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
1612831f5dcfSAlexander Motin 	else
1613831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
1614831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
1615831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
1616831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
1617831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
1618a94a63f0SWarner Losh 	if (cmd->data != NULL)
1619831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
1620831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
1621831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
1622831f5dcfSAlexander Motin 	/* Prepare data. */
1623831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
1624831f5dcfSAlexander Motin 	/*
1625831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
1626831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
1627831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
1628831f5dcfSAlexander Motin 	 */
1629831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
1630831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1631831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
1632831f5dcfSAlexander Motin 	}
1633831f5dcfSAlexander Motin 	/* Set command argument. */
1634831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
1635831f5dcfSAlexander Motin 	/* Set data transfer mode. */
1636831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
1637*aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1638a94a63f0SWarner Losh 		slot_printf(slot, "Starting command!\n");
1639831f5dcfSAlexander Motin 	/* Start command. */
1640d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
1641a6873fd1SIan Lepore 	/* Start timeout callout. */
1642ba6fc1c7SLuiz Otavio O Souza 	callout_reset(&slot->timeout_callout, slot->timeout * hz,
1643ba6fc1c7SLuiz Otavio O Souza 	    sdhci_timeout, slot);
1644831f5dcfSAlexander Motin }
1645831f5dcfSAlexander Motin 
1646831f5dcfSAlexander Motin static void
1647831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
1648831f5dcfSAlexander Motin {
1649831f5dcfSAlexander Motin 	int i;
16501bacf3beSMarius Strobl 	uint32_t val;
16511bacf3beSMarius Strobl 	uint8_t extra;
1652831f5dcfSAlexander Motin 
1653*aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1654a94a63f0SWarner Losh 		slot_printf(slot, "%s: called, err %d flags %d\n",
1655a94a63f0SWarner Losh 		    __func__, slot->curcmd->error, slot->curcmd->flags);
1656831f5dcfSAlexander Motin 	slot->cmd_done = 1;
165772dec079SMarius Strobl 	/*
165872dec079SMarius Strobl 	 * Interrupt aggregation: Restore command interrupt.
1659831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
166072dec079SMarius Strobl 	 * happened first.
166172dec079SMarius Strobl 	 */
1662*aca38eabSMarius Strobl 	if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK &&
1663*aca38eabSMarius Strobl 	    slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200))
1664*aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |=
1665*aca38eabSMarius Strobl 		    SDHCI_INT_RESPONSE);
1666831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
1667831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1668*aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
1669*aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1670831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1671831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1672831f5dcfSAlexander Motin 		sdhci_start(slot);
1673831f5dcfSAlexander Motin 		return;
1674831f5dcfSAlexander Motin 	}
1675831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
1676831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1677831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
1678831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
16791bacf3beSMarius Strobl 			extra = 0;
1680831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
16811bacf3beSMarius Strobl 				val = RD4(slot, SDHCI_RESPONSE + i * 4);
16821bacf3beSMarius Strobl 				if (slot->quirks &
16831bacf3beSMarius Strobl 				    SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1684677ee494SIan Lepore 					slot->curcmd->resp[3 - i] = val;
1685677ee494SIan Lepore 				else {
1686677ee494SIan Lepore 					slot->curcmd->resp[3 - i] =
1687677ee494SIan Lepore 					    (val << 8) | extra;
1688831f5dcfSAlexander Motin 					extra = val >> 24;
1689831f5dcfSAlexander Motin 				}
1690677ee494SIan Lepore 			}
1691831f5dcfSAlexander Motin 		} else
1692831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1693831f5dcfSAlexander Motin 	}
1694*aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1695a94a63f0SWarner Losh 		printf("Resp: %02x %02x %02x %02x\n",
1696a94a63f0SWarner Losh 		    slot->curcmd->resp[0], slot->curcmd->resp[1],
1697a94a63f0SWarner Losh 		    slot->curcmd->resp[2], slot->curcmd->resp[3]);
1698a94a63f0SWarner Losh 
1699831f5dcfSAlexander Motin 	/* If data ready - finish. */
1700831f5dcfSAlexander Motin 	if (slot->data_done)
1701831f5dcfSAlexander Motin 		sdhci_start(slot);
1702831f5dcfSAlexander Motin }
1703831f5dcfSAlexander Motin 
1704831f5dcfSAlexander Motin static void
1705831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data)
1706831f5dcfSAlexander Motin {
1707831f5dcfSAlexander Motin 	uint32_t target_timeout, current_timeout;
1708831f5dcfSAlexander Motin 	uint8_t div;
1709831f5dcfSAlexander Motin 
1710831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1711831f5dcfSAlexander Motin 		slot->data_done = 1;
1712831f5dcfSAlexander Motin 		return;
1713831f5dcfSAlexander Motin 	}
1714831f5dcfSAlexander Motin 
1715831f5dcfSAlexander Motin 	slot->data_done = 0;
1716831f5dcfSAlexander Motin 
1717831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
1718831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
1719ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1720ceb9e9f7SIan Lepore 		div = 0xE;
1721ceb9e9f7SIan Lepore 	} else {
1722831f5dcfSAlexander Motin 		target_timeout = 1000000;
1723831f5dcfSAlexander Motin 		div = 0;
1724831f5dcfSAlexander Motin 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1725ceb9e9f7SIan Lepore 		while (current_timeout < target_timeout && div < 0xE) {
1726ceb9e9f7SIan Lepore 			++div;
1727831f5dcfSAlexander Motin 			current_timeout <<= 1;
1728831f5dcfSAlexander Motin 		}
1729831f5dcfSAlexander Motin 		/* Compensate for an off-by-one error in the CaFe chip.*/
1730ceb9e9f7SIan Lepore 		if (div < 0xE &&
1731ceb9e9f7SIan Lepore 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1732ceb9e9f7SIan Lepore 			++div;
1733831f5dcfSAlexander Motin 		}
1734ceb9e9f7SIan Lepore 	}
1735831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1736831f5dcfSAlexander Motin 
1737831f5dcfSAlexander Motin 	if (data == NULL)
1738831f5dcfSAlexander Motin 		return;
1739831f5dcfSAlexander Motin 
1740831f5dcfSAlexander Motin 	/* Use DMA if possible. */
1741831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
1742831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
1743831f5dcfSAlexander Motin 	/* If data is small, broken DMA may return zeroes instead of data, */
1744d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1745831f5dcfSAlexander Motin 	    (data->len <= 512))
1746831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1747831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
1748d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1749831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
1750831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1751831f5dcfSAlexander Motin 	/* Load DMA buffer. */
1752831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
1753831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
1754ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1755ecc2d997SRui Paulo 			    BUS_DMASYNC_PREREAD);
1756831f5dcfSAlexander Motin 		else {
1757831f5dcfSAlexander Motin 			memcpy(slot->dmamem, data->data,
1758ecc2d997SRui Paulo 			    (data->len < DMA_BLOCK_SIZE) ?
1759ecc2d997SRui Paulo 			    data->len : DMA_BLOCK_SIZE);
1760ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1761ecc2d997SRui Paulo 			    BUS_DMASYNC_PREWRITE);
1762831f5dcfSAlexander Motin 		}
1763831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1764831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1765831f5dcfSAlexander Motin 		 * for the last page and unmask else. */
1766831f5dcfSAlexander Motin 		if (data->len == DMA_BLOCK_SIZE)
1767831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1768831f5dcfSAlexander Motin 		else
1769831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
1770831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1771831f5dcfSAlexander Motin 	}
1772831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
1773831f5dcfSAlexander Motin 	slot->offset = 0;
1774831f5dcfSAlexander Motin 	/* Set block size and request IRQ on 4K border. */
17751bacf3beSMarius Strobl 	WR2(slot, SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(DMA_BOUNDARY,
17761bacf3beSMarius Strobl 	    (data->len < 512) ? data->len : 512));
1777831f5dcfSAlexander Motin 	/* Set block count. */
1778831f5dcfSAlexander Motin 	WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
1779a94a63f0SWarner Losh 
1780*aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
178115c440e1SWarner Losh 		slot_printf(slot, "Block size: %02x, count %lu\n",
178215c440e1SWarner Losh 		    (unsigned int)SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512) ? data->len : 512),
1783a94a63f0SWarner Losh 		    (unsigned long)(data->len + 511) / 512);
1784831f5dcfSAlexander Motin }
1785831f5dcfSAlexander Motin 
1786c3a0f75aSOleksandr Tymoshenko void
1787831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
1788831f5dcfSAlexander Motin {
1789831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
17907e6ccea3SMarius Strobl 	size_t left;
1791831f5dcfSAlexander Motin 
1792831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
1793ecc2d997SRui Paulo 	 * Auxiliary restore point for the case when data interrupt
1794831f5dcfSAlexander Motin 	 * happened first. */
1795831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
1796831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1797831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
1798831f5dcfSAlexander Motin 	}
1799831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
1800915780d7SLuiz Otavio O Souza 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) &&
1801915780d7SLuiz Otavio O Souza 	    slot->curcmd->data != NULL) {
1802831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
18037e6ccea3SMarius Strobl 			left = data->len - slot->offset;
1804ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1805ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTREAD);
1806831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1807831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
1808831f5dcfSAlexander Motin 		} else
1809ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1810ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTWRITE);
1811831f5dcfSAlexander Motin 	}
1812a98788edSIan Lepore 	slot->data_done = 1;
1813831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
1814831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1815*aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
1816*aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1817831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1818831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1819831f5dcfSAlexander Motin 		sdhci_start(slot);
1820831f5dcfSAlexander Motin 		return;
1821831f5dcfSAlexander Motin 	}
1822831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
1823831f5dcfSAlexander Motin 	if (slot->cmd_done)
1824831f5dcfSAlexander Motin 		sdhci_start(slot);
1825831f5dcfSAlexander Motin }
1826831f5dcfSAlexander Motin 
1827a94a63f0SWarner Losh #ifdef MMCCAM
1828a94a63f0SWarner Losh static void
1829a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot)
1830a94a63f0SWarner Losh {
1831a94a63f0SWarner Losh         union ccb *ccb;
1832a94a63f0SWarner Losh 
1833a94a63f0SWarner Losh 	ccb = slot->ccb;
1834a94a63f0SWarner Losh 	if (ccb == NULL)
1835a94a63f0SWarner Losh 		return;
1836a94a63f0SWarner Losh 
1837a94a63f0SWarner Losh         struct ccb_mmcio *mmcio;
1838a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
1839a94a63f0SWarner Losh 
1840a94a63f0SWarner Losh 	if (!(slot->flags & CMD_STARTED)) {
1841a94a63f0SWarner Losh 		slot->flags |= CMD_STARTED;
1842a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->cmd);
1843a94a63f0SWarner Losh 		return;
1844a94a63f0SWarner Losh 	}
1845a94a63f0SWarner Losh 
1846a94a63f0SWarner Losh 	/*
1847a94a63f0SWarner Losh 	 * Old stack doesn't use this!
1848a94a63f0SWarner Losh 	 * Enabling this code causes significant performance degradation
1849a94a63f0SWarner Losh 	 * and IRQ storms on BBB, Wandboard behaves fine.
1850a94a63f0SWarner Losh 	 * Not using this code does no harm...
1851a94a63f0SWarner Losh 	if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) {
1852a94a63f0SWarner Losh 		slot->flags |= STOP_STARTED;
1853a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->stop);
1854a94a63f0SWarner Losh 		return;
1855a94a63f0SWarner Losh 	}
1856a94a63f0SWarner Losh 	*/
1857*aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1858a94a63f0SWarner Losh 		slot_printf(slot, "result: %d\n", mmcio->cmd.error);
1859a94a63f0SWarner Losh 	if (mmcio->cmd.error == 0 &&
1860a94a63f0SWarner Losh 	    (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
1861a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD);
1862a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_DATA);
1863a94a63f0SWarner Losh 	}
1864a94a63f0SWarner Losh 
1865a94a63f0SWarner Losh 	sdhci_req_done(slot);
1866a94a63f0SWarner Losh }
1867a94a63f0SWarner Losh #else
1868831f5dcfSAlexander Motin static void
1869831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
1870831f5dcfSAlexander Motin {
1871831f5dcfSAlexander Motin 	struct mmc_request *req;
1872831f5dcfSAlexander Motin 
1873831f5dcfSAlexander Motin 	req = slot->req;
1874831f5dcfSAlexander Motin 	if (req == NULL)
1875831f5dcfSAlexander Motin 		return;
1876831f5dcfSAlexander Motin 
1877831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
1878831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
1879831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
1880831f5dcfSAlexander Motin 		return;
1881831f5dcfSAlexander Motin 	}
1882915780d7SLuiz Otavio O Souza 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) &&
1883915780d7SLuiz Otavio O Souza 	    !(slot->flags & STOP_STARTED) && req->stop) {
1884831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
1885831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
1886831f5dcfSAlexander Motin 		return;
1887831f5dcfSAlexander Motin 	}
1888*aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
18895b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
18905b69a497SAlexander Motin 	if (!req->cmd->error &&
1891915780d7SLuiz Otavio O Souza 	    ((slot->curcmd == req->stop &&
1892915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) ||
1893915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
1894831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1895831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1896831f5dcfSAlexander Motin 	}
1897831f5dcfSAlexander Motin 
1898e64f01a9SIan Lepore 	sdhci_req_done(slot);
1899831f5dcfSAlexander Motin }
1900a94a63f0SWarner Losh #endif
1901831f5dcfSAlexander Motin 
1902d6b3aaf8SOleksandr Tymoshenko int
1903b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev,
1904b440e965SMarius Strobl     struct mmc_request *req)
1905831f5dcfSAlexander Motin {
1906831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1907831f5dcfSAlexander Motin 
1908831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1909831f5dcfSAlexander Motin 	if (slot->req != NULL) {
1910831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1911831f5dcfSAlexander Motin 		return (EBUSY);
1912831f5dcfSAlexander Motin 	}
1913*aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
19141bacf3beSMarius Strobl 		slot_printf(slot,
19151bacf3beSMarius Strobl 		    "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1916831f5dcfSAlexander Motin 		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
19175b69a497SAlexander Motin 		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
19185b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
19195b69a497SAlexander Motin 	}
1920831f5dcfSAlexander Motin 	slot->req = req;
1921831f5dcfSAlexander Motin 	slot->flags = 0;
1922831f5dcfSAlexander Motin 	sdhci_start(slot);
1923831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1924bea2dca2SAlexander Motin 	if (dumping) {
1925bea2dca2SAlexander Motin 		while (slot->req != NULL) {
1926d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
1927bea2dca2SAlexander Motin 			DELAY(10);
1928bea2dca2SAlexander Motin 		}
1929bea2dca2SAlexander Motin 	}
1930831f5dcfSAlexander Motin 	return (0);
1931831f5dcfSAlexander Motin }
1932831f5dcfSAlexander Motin 
1933d6b3aaf8SOleksandr Tymoshenko int
1934b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev)
1935831f5dcfSAlexander Motin {
1936831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1937831f5dcfSAlexander Motin 	uint32_t val;
1938831f5dcfSAlexander Motin 
1939831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1940831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
1941831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1942831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
1943831f5dcfSAlexander Motin }
1944831f5dcfSAlexander Motin 
1945d6b3aaf8SOleksandr Tymoshenko int
1946b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev)
1947831f5dcfSAlexander Motin {
1948831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1949831f5dcfSAlexander Motin 	int err = 0;
1950831f5dcfSAlexander Motin 
1951831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1952831f5dcfSAlexander Motin 	while (slot->bus_busy)
1953d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
1954831f5dcfSAlexander Motin 	slot->bus_busy++;
1955831f5dcfSAlexander Motin 	/* Activate led. */
1956831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
1957831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1958831f5dcfSAlexander Motin 	return (err);
1959831f5dcfSAlexander Motin }
1960831f5dcfSAlexander Motin 
1961d6b3aaf8SOleksandr Tymoshenko int
1962b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev)
1963831f5dcfSAlexander Motin {
1964831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1965831f5dcfSAlexander Motin 
1966831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1967831f5dcfSAlexander Motin 	/* Deactivate led. */
1968831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
1969831f5dcfSAlexander Motin 	slot->bus_busy--;
1970831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1971d493985aSAlexander Motin 	wakeup(slot);
1972831f5dcfSAlexander Motin 	return (0);
1973831f5dcfSAlexander Motin }
1974831f5dcfSAlexander Motin 
1975831f5dcfSAlexander Motin static void
1976831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
1977831f5dcfSAlexander Motin {
1978831f5dcfSAlexander Motin 
1979831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1980831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
1981831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1982831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1983831f5dcfSAlexander Motin 		return;
1984831f5dcfSAlexander Motin 	}
1985831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
1986831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1987831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
1988831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1989831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
1990831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
1991831f5dcfSAlexander Motin 
1992831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
1993831f5dcfSAlexander Motin }
1994831f5dcfSAlexander Motin 
1995831f5dcfSAlexander Motin static void
1996831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
1997831f5dcfSAlexander Motin {
19981bacf3beSMarius Strobl 	struct mmc_data *data;
199915c440e1SWarner Losh 	size_t left;
2000831f5dcfSAlexander Motin 
2001831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2002831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2003831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2004831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2005831f5dcfSAlexander Motin 		return;
2006831f5dcfSAlexander Motin 	}
2007831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2008831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
2009831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2010831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
2011831f5dcfSAlexander Motin 		    intmask);
2012831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2013831f5dcfSAlexander Motin 		return;
2014831f5dcfSAlexander Motin 	}
2015831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
2016831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2017acbaa69fSOleksandr Tymoshenko 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
2018831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2019831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2020831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
2021831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
2022831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2023831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
2024831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2025831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
2026831f5dcfSAlexander Motin 	}
2027831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
2028831f5dcfSAlexander Motin 		/* No need to continue after any error. */
2029a98788edSIan Lepore 		goto done;
2030831f5dcfSAlexander Motin 	}
2031831f5dcfSAlexander Motin 
2032*aca38eabSMarius Strobl 	/* Handle tuning completion interrupt. */
2033*aca38eabSMarius Strobl 	if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) &&
2034*aca38eabSMarius Strobl 	    (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK ||
2035*aca38eabSMarius Strobl 	    slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) {
2036*aca38eabSMarius Strobl 		slot->req->flags |= MMC_TUNE_DONE;
2037*aca38eabSMarius Strobl 		sdhci_finish_command(slot);
2038*aca38eabSMarius Strobl 		sdhci_finish_data(slot);
2039*aca38eabSMarius Strobl 		return;
2040*aca38eabSMarius Strobl 	}
2041831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
2042c3a0f75aSOleksandr Tymoshenko 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
2043c3a0f75aSOleksandr Tymoshenko 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
2044c3a0f75aSOleksandr Tymoshenko 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
20451bacf3beSMarius Strobl 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot,
20461bacf3beSMarius Strobl 			    &intmask);
2047c3a0f75aSOleksandr Tymoshenko 			slot->flags |= PLATFORM_DATA_STARTED;
2048c3a0f75aSOleksandr Tymoshenko 		} else
2049831f5dcfSAlexander Motin 			sdhci_transfer_pio(slot);
2050c3a0f75aSOleksandr Tymoshenko 	}
2051831f5dcfSAlexander Motin 	/* Handle DMA border. */
2052831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
20531bacf3beSMarius Strobl 		data = slot->curcmd->data;
2054831f5dcfSAlexander Motin 
2055831f5dcfSAlexander Motin 		/* Unload DMA buffer ... */
2056831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2057831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2058831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2059831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
2060831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
2061831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
2062831f5dcfSAlexander Motin 		} else {
2063831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2064831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
2065831f5dcfSAlexander Motin 		}
2066831f5dcfSAlexander Motin 		/* ... and reload it again. */
2067831f5dcfSAlexander Motin 		slot->offset += DMA_BLOCK_SIZE;
2068831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2069831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2070831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2071831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREREAD);
2072831f5dcfSAlexander Motin 		} else {
2073831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
2074831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)? left : DMA_BLOCK_SIZE);
2075831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2076831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
2077831f5dcfSAlexander Motin 		}
2078831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
2079831f5dcfSAlexander Motin 		 * for the last page. */
2080831f5dcfSAlexander Motin 		if (left == DMA_BLOCK_SIZE) {
2081831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
2082831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2083831f5dcfSAlexander Motin 		}
2084831f5dcfSAlexander Motin 		/* Restart DMA. */
2085831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
2086831f5dcfSAlexander Motin 	}
2087831f5dcfSAlexander Motin 	/* We have got all data. */
2088c3a0f75aSOleksandr Tymoshenko 	if (intmask & SDHCI_INT_DATA_END) {
2089c3a0f75aSOleksandr Tymoshenko 		if (slot->flags & PLATFORM_DATA_STARTED) {
2090c3a0f75aSOleksandr Tymoshenko 			slot->flags &= ~PLATFORM_DATA_STARTED;
2091c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2092c3a0f75aSOleksandr Tymoshenko 		} else
2093831f5dcfSAlexander Motin 			sdhci_finish_data(slot);
2094831f5dcfSAlexander Motin 	}
2095a98788edSIan Lepore done:
2096a98788edSIan Lepore 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
2097a98788edSIan Lepore 		if (slot->flags & PLATFORM_DATA_STARTED) {
2098a98788edSIan Lepore 			slot->flags &= ~PLATFORM_DATA_STARTED;
2099a98788edSIan Lepore 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2100a98788edSIan Lepore 		} else
2101a98788edSIan Lepore 			sdhci_finish_data(slot);
2102a98788edSIan Lepore 	}
2103c3a0f75aSOleksandr Tymoshenko }
2104831f5dcfSAlexander Motin 
2105831f5dcfSAlexander Motin static void
2106831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot)
2107831f5dcfSAlexander Motin {
2108831f5dcfSAlexander Motin 	uint16_t err;
2109831f5dcfSAlexander Motin 
2110831f5dcfSAlexander Motin 	err = RD4(slot, SDHCI_ACMD12_ERR);
2111831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2112831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
2113831f5dcfSAlexander Motin 		    "there is no active command.\n", err);
2114831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2115831f5dcfSAlexander Motin 		return;
2116831f5dcfSAlexander Motin 	}
2117831f5dcfSAlexander Motin 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err);
2118831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_CMD);
2119831f5dcfSAlexander Motin }
2120831f5dcfSAlexander Motin 
2121d6b3aaf8SOleksandr Tymoshenko void
2122d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
2123831f5dcfSAlexander Motin {
21242b96b955SJustin Hibbits 	uint32_t intmask, present;
2125831f5dcfSAlexander Motin 
2126831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2127831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
2128831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
2129831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
2130831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
2131d6b3aaf8SOleksandr Tymoshenko 		return;
2132831f5dcfSAlexander Motin 	}
2133*aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 2))
21345b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
21355b69a497SAlexander Motin 
2136*aca38eabSMarius Strobl 	/* Handle tuning error interrupt. */
2137*aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_TUNEERR)) {
2138*aca38eabSMarius Strobl 		slot_printf(slot, "Tuning error indicated\n");
2139*aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
2140*aca38eabSMarius Strobl 		if (slot->curcmd) {
2141*aca38eabSMarius Strobl 			slot->curcmd->error = MMC_ERR_BADCRC;
2142*aca38eabSMarius Strobl 			sdhci_finish_command(slot);
2143*aca38eabSMarius Strobl 		}
2144*aca38eabSMarius Strobl 	}
2145*aca38eabSMarius Strobl 	/* Handle re-tuning interrupt. */
2146*aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_RETUNE))
2147*aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
2148831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
2149831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2150639f59f0SIan Lepore 		present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
21512b96b955SJustin Hibbits 		slot->intmask &=
21522b96b955SJustin Hibbits 		    ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
21532b96b955SJustin Hibbits 		slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
21542b96b955SJustin Hibbits 		    SDHCI_INT_CARD_INSERT;
21552b96b955SJustin Hibbits 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
21562b96b955SJustin Hibbits 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2157831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
2158831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
2159b8bf08b1SIan Lepore 		sdhci_handle_card_present_locked(slot, present);
2160831f5dcfSAlexander Motin 	}
2161831f5dcfSAlexander Motin 	/* Handle command interrupts. */
2162831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
2163831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
2164831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
2165831f5dcfSAlexander Motin 	}
2166831f5dcfSAlexander Motin 	/* Handle data interrupts. */
2167831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
2168831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
21697e6ccea3SMarius Strobl 		/* Don't call data_irq in case of errored command. */
21707e586643SIan Lepore 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
2171831f5dcfSAlexander Motin 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
2172831f5dcfSAlexander Motin 	}
2173831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
2174831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
2175831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
2176831f5dcfSAlexander Motin 		sdhci_acmd_irq(slot);
2177831f5dcfSAlexander Motin 	}
2178831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
2179831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
2180831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
2181*aca38eabSMarius Strobl 		slot_printf(slot, "Card is consuming too much power!\n");
2182831f5dcfSAlexander Motin 	}
2183*aca38eabSMarius Strobl 	intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE |
2184*aca38eabSMarius Strobl 	    SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK |
2185*aca38eabSMarius Strobl 	    SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER);
2186831f5dcfSAlexander Motin 	/* The rest is unknown. */
2187831f5dcfSAlexander Motin 	if (intmask) {
2188831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
2189831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
2190831f5dcfSAlexander Motin 		    intmask);
2191831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2192831f5dcfSAlexander Motin 	}
2193831f5dcfSAlexander Motin 
2194831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2195831f5dcfSAlexander Motin }
2196831f5dcfSAlexander Motin 
2197d6b3aaf8SOleksandr Tymoshenko int
21981bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which,
21991bacf3beSMarius Strobl     uintptr_t *result)
2200831f5dcfSAlexander Motin {
2201831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
2202831f5dcfSAlexander Motin 
2203831f5dcfSAlexander Motin 	switch (which) {
2204831f5dcfSAlexander Motin 	default:
2205831f5dcfSAlexander Motin 		return (EINVAL);
2206831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2207bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
2208831f5dcfSAlexander Motin 		break;
2209831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2210bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
2211831f5dcfSAlexander Motin 		break;
2212831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2213bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
2214831f5dcfSAlexander Motin 		break;
2215831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2216bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
2217831f5dcfSAlexander Motin 		break;
2218831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2219bcd91d25SJayachandran C. 		*result = slot->host.f_min;
2220831f5dcfSAlexander Motin 		break;
2221831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
2222bcd91d25SJayachandran C. 		*result = slot->host.f_max;
2223831f5dcfSAlexander Motin 		break;
2224831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2225bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
2226831f5dcfSAlexander Motin 		break;
2227831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2228bcd91d25SJayachandran C. 		*result = slot->host.mode;
2229831f5dcfSAlexander Motin 		break;
2230831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2231bcd91d25SJayachandran C. 		*result = slot->host.ocr;
2232831f5dcfSAlexander Motin 		break;
2233831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2234bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
2235831f5dcfSAlexander Motin 		break;
2236831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2237bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
2238831f5dcfSAlexander Motin 		break;
2239*aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2240*aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED) {
2241*aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) {
2242*aca38eabSMarius Strobl 				*result = retune_req_reset;
2243*aca38eabSMarius Strobl 				break;
2244*aca38eabSMarius Strobl 			}
2245*aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) {
2246*aca38eabSMarius Strobl 				*result = retune_req_normal;
2247*aca38eabSMarius Strobl 				break;
2248*aca38eabSMarius Strobl 			}
2249*aca38eabSMarius Strobl 		}
2250*aca38eabSMarius Strobl 		*result = retune_req_none;
2251*aca38eabSMarius Strobl 		break;
22520f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
22530f34084fSMarius Strobl 		*result = slot->host.ios.vccq;
22540f34084fSMarius Strobl 		break;
2255831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2256bcd91d25SJayachandran C. 		*result = slot->host.caps;
2257831f5dcfSAlexander Motin 		break;
2258831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2259bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
2260831f5dcfSAlexander Motin 		break;
22613a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2262*aca38eabSMarius Strobl 		/*
2263*aca38eabSMarius Strobl 		 * Re-tuning modes 1 and 2 restrict the maximum data length
2264*aca38eabSMarius Strobl 		 * per read/write command to 4 MiB.
2265*aca38eabSMarius Strobl 		 */
2266*aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED &&
2267*aca38eabSMarius Strobl 		    (slot->retune_mode == SDHCI_RETUNE_MODE_1 ||
2268*aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_2)) {
2269*aca38eabSMarius Strobl 			*result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE;
2270*aca38eabSMarius Strobl 			break;
2271*aca38eabSMarius Strobl 		}
2272bcd91d25SJayachandran C. 		*result = 65535;
22733a4a2557SAlexander Motin 		break;
227472dec079SMarius Strobl 	case MMCBR_IVAR_MAX_BUSY_TIMEOUT:
227572dec079SMarius Strobl 		/*
227672dec079SMarius Strobl 		 * Currently, sdhci_start_data() hardcodes 1 s for all CMDs.
227772dec079SMarius Strobl 		 */
227872dec079SMarius Strobl 		*result = 1000000;
227972dec079SMarius Strobl 		break;
2280831f5dcfSAlexander Motin 	}
2281831f5dcfSAlexander Motin 	return (0);
2282831f5dcfSAlexander Motin }
2283831f5dcfSAlexander Motin 
2284d6b3aaf8SOleksandr Tymoshenko int
22851bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which,
22861bacf3beSMarius Strobl     uintptr_t value)
2287831f5dcfSAlexander Motin {
2288831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
2289b440e965SMarius Strobl 	uint32_t clock, max_clock;
2290b440e965SMarius Strobl 	int i;
2291831f5dcfSAlexander Motin 
229215c440e1SWarner Losh 	if (sdhci_debug > 1)
229315c440e1SWarner Losh 		slot_printf(slot, "%s: var=%d\n", __func__, which);
2294831f5dcfSAlexander Motin 	switch (which) {
2295831f5dcfSAlexander Motin 	default:
2296831f5dcfSAlexander Motin 		return (EINVAL);
2297831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2298831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
2299831f5dcfSAlexander Motin 		break;
2300831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2301831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
2302831f5dcfSAlexander Motin 		break;
2303831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2304831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
2305831f5dcfSAlexander Motin 		break;
2306831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2307831f5dcfSAlexander Motin 		if (value > 0) {
230857677a3aSOleksandr Tymoshenko 			max_clock = slot->max_clk;
230957677a3aSOleksandr Tymoshenko 			clock = max_clock;
231057677a3aSOleksandr Tymoshenko 
231157677a3aSOleksandr Tymoshenko 			if (slot->version < SDHCI_SPEC_300) {
231257677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
231357677a3aSOleksandr Tymoshenko 				    i <<= 1) {
2314831f5dcfSAlexander Motin 					if (clock <= value)
2315831f5dcfSAlexander Motin 						break;
2316831f5dcfSAlexander Motin 					clock >>= 1;
2317831f5dcfSAlexander Motin 				}
2318b440e965SMarius Strobl 			} else {
231957677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
232057677a3aSOleksandr Tymoshenko 				    i += 2) {
232157677a3aSOleksandr Tymoshenko 					if (clock <= value)
232257677a3aSOleksandr Tymoshenko 						break;
232357677a3aSOleksandr Tymoshenko 					clock = max_clock / (i + 2);
232457677a3aSOleksandr Tymoshenko 				}
232557677a3aSOleksandr Tymoshenko 			}
232657677a3aSOleksandr Tymoshenko 
2327831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
2328831f5dcfSAlexander Motin 		} else
2329831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
2330831f5dcfSAlexander Motin 		break;
2331831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2332831f5dcfSAlexander Motin 		slot->host.mode = value;
2333831f5dcfSAlexander Motin 		break;
2334831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2335831f5dcfSAlexander Motin 		slot->host.ocr = value;
2336831f5dcfSAlexander Motin 		break;
2337831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2338831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
2339831f5dcfSAlexander Motin 		break;
2340831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2341831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
2342831f5dcfSAlexander Motin 		break;
23430f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
23440f34084fSMarius Strobl 		slot->host.ios.vccq = value;
23450f34084fSMarius Strobl 		break;
2346831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2347831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
2348831f5dcfSAlexander Motin 		break;
2349831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2350831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2351831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2352831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
23533a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2354*aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2355831f5dcfSAlexander Motin 		return (EINVAL);
2356831f5dcfSAlexander Motin 	}
2357831f5dcfSAlexander Motin 	return (0);
2358831f5dcfSAlexander Motin }
2359831f5dcfSAlexander Motin 
236015c440e1SWarner Losh #ifdef MMCCAM
2361a94a63f0SWarner Losh void
2362a94a63f0SWarner Losh sdhci_cam_start_slot(struct sdhci_slot *slot)
2363a94a63f0SWarner Losh {
2364a94a63f0SWarner Losh         if ((slot->devq = cam_simq_alloc(1)) == NULL) {
2365a94a63f0SWarner Losh                 goto fail;
2366a94a63f0SWarner Losh         }
2367a94a63f0SWarner Losh 
2368a94a63f0SWarner Losh         mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF);
2369a94a63f0SWarner Losh         slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll,
2370a94a63f0SWarner Losh                                   "sdhci_slot", slot, device_get_unit(slot->bus),
2371a94a63f0SWarner Losh                                   &slot->sim_mtx, 1, 1, slot->devq);
2372a94a63f0SWarner Losh 
2373a94a63f0SWarner Losh         if (slot->sim == NULL) {
2374a94a63f0SWarner Losh                 cam_simq_free(slot->devq);
2375a94a63f0SWarner Losh                 slot_printf(slot, "cannot allocate CAM SIM\n");
2376a94a63f0SWarner Losh                 goto fail;
2377a94a63f0SWarner Losh         }
2378a94a63f0SWarner Losh 
2379a94a63f0SWarner Losh         mtx_lock(&slot->sim_mtx);
2380a94a63f0SWarner Losh         if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) {
2381a94a63f0SWarner Losh                 slot_printf(slot,
2382a94a63f0SWarner Losh                               "cannot register SCSI pass-through bus\n");
2383a94a63f0SWarner Losh                 cam_sim_free(slot->sim, FALSE);
2384a94a63f0SWarner Losh                 cam_simq_free(slot->devq);
2385a94a63f0SWarner Losh                 mtx_unlock(&slot->sim_mtx);
2386a94a63f0SWarner Losh                 goto fail;
2387a94a63f0SWarner Losh         }
2388a94a63f0SWarner Losh 
2389a94a63f0SWarner Losh         mtx_unlock(&slot->sim_mtx);
2390a94a63f0SWarner Losh         /* End CAM-specific init */
2391a94a63f0SWarner Losh 	slot->card_present = 0;
2392a94a63f0SWarner Losh 	sdhci_card_task(slot, 0);
2393a94a63f0SWarner Losh         return;
2394a94a63f0SWarner Losh 
2395a94a63f0SWarner Losh fail:
2396a94a63f0SWarner Losh         if (slot->sim != NULL) {
2397a94a63f0SWarner Losh                 mtx_lock(&slot->sim_mtx);
2398a94a63f0SWarner Losh                 xpt_bus_deregister(cam_sim_path(slot->sim));
2399a94a63f0SWarner Losh                 cam_sim_free(slot->sim, FALSE);
2400a94a63f0SWarner Losh                 mtx_unlock(&slot->sim_mtx);
2401a94a63f0SWarner Losh         }
2402a94a63f0SWarner Losh 
2403a94a63f0SWarner Losh         if (slot->devq != NULL)
2404a94a63f0SWarner Losh                 cam_simq_free(slot->devq);
2405a94a63f0SWarner Losh }
2406a94a63f0SWarner Losh 
2407a94a63f0SWarner Losh static void
2408a94a63f0SWarner Losh sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb)
2409a94a63f0SWarner Losh {
2410a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2411a94a63f0SWarner Losh 
2412a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2413a94a63f0SWarner Losh 
2414a94a63f0SWarner Losh 	sdhci_cam_request(slot, ccb);
2415a94a63f0SWarner Losh }
2416a94a63f0SWarner Losh 
2417a94a63f0SWarner Losh void
2418a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb)
2419a94a63f0SWarner Losh {
2420a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2421a94a63f0SWarner Losh 
2422a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2423a94a63f0SWarner Losh 	if (slot == NULL) {
2424a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2425a94a63f0SWarner Losh 		xpt_done(ccb);
2426a94a63f0SWarner Losh 		return;
2427a94a63f0SWarner Losh 	}
2428a94a63f0SWarner Losh 
2429a94a63f0SWarner Losh 	mtx_assert(&slot->sim_mtx, MA_OWNED);
2430a94a63f0SWarner Losh 
2431a94a63f0SWarner Losh 	switch (ccb->ccb_h.func_code) {
2432a94a63f0SWarner Losh 	case XPT_PATH_INQ:
2433a94a63f0SWarner Losh 	{
2434a94a63f0SWarner Losh 		struct ccb_pathinq *cpi;
2435a94a63f0SWarner Losh 
2436a94a63f0SWarner Losh 		cpi = &ccb->cpi;
2437a94a63f0SWarner Losh 		cpi->version_num = 1;
2438a94a63f0SWarner Losh 		cpi->hba_inquiry = 0;
2439a94a63f0SWarner Losh 		cpi->target_sprt = 0;
2440a94a63f0SWarner Losh 		cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
2441a94a63f0SWarner Losh 		cpi->hba_eng_cnt = 0;
2442a94a63f0SWarner Losh 		cpi->max_target = 0;
2443a94a63f0SWarner Losh 		cpi->max_lun = 0;
2444a94a63f0SWarner Losh 		cpi->initiator_id = 1;
2445a94a63f0SWarner Losh 		cpi->maxio = MAXPHYS;
2446a94a63f0SWarner Losh 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2447a94a63f0SWarner Losh 		strncpy(cpi->hba_vid, "Deglitch Networks", HBA_IDLEN);
2448a94a63f0SWarner Losh 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2449a94a63f0SWarner Losh 		cpi->unit_number = cam_sim_unit(sim);
2450a94a63f0SWarner Losh 		cpi->bus_id = cam_sim_bus(sim);
2451a94a63f0SWarner Losh 		cpi->base_transfer_speed = 100; /* XXX WTF? */
2452a94a63f0SWarner Losh 		cpi->protocol = PROTO_MMCSD;
2453a94a63f0SWarner Losh 		cpi->protocol_version = SCSI_REV_0;
2454a94a63f0SWarner Losh 		cpi->transport = XPORT_MMCSD;
2455a94a63f0SWarner Losh 		cpi->transport_version = 0;
2456a94a63f0SWarner Losh 
2457a94a63f0SWarner Losh 		cpi->ccb_h.status = CAM_REQ_CMP;
2458a94a63f0SWarner Losh 		break;
2459a94a63f0SWarner Losh 	}
2460a94a63f0SWarner Losh 	case XPT_GET_TRAN_SETTINGS:
2461a94a63f0SWarner Losh 	{
2462a94a63f0SWarner Losh 		struct ccb_trans_settings *cts = &ccb->cts;
2463a94a63f0SWarner Losh 
2464a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2465a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n");
2466a94a63f0SWarner Losh 
2467a94a63f0SWarner Losh 		cts->protocol = PROTO_MMCSD;
2468a94a63f0SWarner Losh 		cts->protocol_version = 1;
2469a94a63f0SWarner Losh 		cts->transport = XPORT_MMCSD;
2470a94a63f0SWarner Losh 		cts->transport_version = 1;
2471a94a63f0SWarner Losh 		cts->xport_specific.valid = 0;
2472a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_ocr = slot->host.host_ocr;
2473a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_min = slot->host.f_min;
2474a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_max = slot->host.f_max;
2475a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_caps = slot->host.caps;
2476a94a63f0SWarner Losh 		memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios));
2477a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2478a94a63f0SWarner Losh 		break;
2479a94a63f0SWarner Losh 	}
2480a94a63f0SWarner Losh 	case XPT_SET_TRAN_SETTINGS:
2481a94a63f0SWarner Losh 	{
2482a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2483a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n");
2484a94a63f0SWarner Losh 		sdhci_cam_settran_settings(slot, ccb);
2485a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2486a94a63f0SWarner Losh 		break;
2487a94a63f0SWarner Losh 	}
2488a94a63f0SWarner Losh 	case XPT_RESET_BUS:
2489a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2490a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n");
2491a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2492a94a63f0SWarner Losh 		break;
2493a94a63f0SWarner Losh 	case XPT_MMC_IO:
2494a94a63f0SWarner Losh 		/*
2495a94a63f0SWarner Losh 		 * Here is the HW-dependent part of
2496a94a63f0SWarner Losh 		 * sending the command to the underlying h/w
2497a94a63f0SWarner Losh 		 * At some point in the future an interrupt comes.
2498a94a63f0SWarner Losh 		 * Then the request will be marked as completed.
2499a94a63f0SWarner Losh 		 */
2500*aca38eabSMarius Strobl 		if (__predict_false(sdhci_debug > 1))
2501a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_MMC_IO\n");
2502a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INPROG;
2503a94a63f0SWarner Losh 
2504a94a63f0SWarner Losh 		sdhci_cam_handle_mmcio(sim, ccb);
2505a94a63f0SWarner Losh 		return;
2506a94a63f0SWarner Losh 		/* NOTREACHED */
2507a94a63f0SWarner Losh 		break;
2508a94a63f0SWarner Losh 	default:
2509a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INVALID;
2510a94a63f0SWarner Losh 		break;
2511a94a63f0SWarner Losh 	}
2512a94a63f0SWarner Losh 	xpt_done(ccb);
2513a94a63f0SWarner Losh 	return;
2514a94a63f0SWarner Losh }
2515a94a63f0SWarner Losh 
2516a94a63f0SWarner Losh void
2517a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim)
2518a94a63f0SWarner Losh {
2519a94a63f0SWarner Losh 	return;
2520a94a63f0SWarner Losh }
2521a94a63f0SWarner Losh 
2522a94a63f0SWarner Losh int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock) {
2523a94a63f0SWarner Losh 	int max_clock, clock, i;
2524a94a63f0SWarner Losh 
2525a94a63f0SWarner Losh 	if (proposed_clock == 0)
2526a94a63f0SWarner Losh 		return 0;
2527a94a63f0SWarner Losh 	max_clock = slot->max_clk;
2528a94a63f0SWarner Losh 	clock = max_clock;
2529a94a63f0SWarner Losh 
2530a94a63f0SWarner Losh 	if (slot->version < SDHCI_SPEC_300) {
2531a94a63f0SWarner Losh 		for (i = 0; i < SDHCI_200_MAX_DIVIDER;
2532a94a63f0SWarner Losh 		     i <<= 1) {
2533a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2534a94a63f0SWarner Losh 				break;
2535a94a63f0SWarner Losh 			clock >>= 1;
2536a94a63f0SWarner Losh 		}
2537a94a63f0SWarner Losh 	} else {
2538a94a63f0SWarner Losh 		for (i = 0; i < SDHCI_300_MAX_DIVIDER;
2539a94a63f0SWarner Losh 		     i += 2) {
2540a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2541a94a63f0SWarner Losh 				break;
2542a94a63f0SWarner Losh 			clock = max_clock / (i + 2);
2543a94a63f0SWarner Losh 		}
2544a94a63f0SWarner Losh 	}
2545a94a63f0SWarner Losh 	return clock;
2546a94a63f0SWarner Losh }
2547a94a63f0SWarner Losh 
2548a94a63f0SWarner Losh int
2549a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb)
2550a94a63f0SWarner Losh {
2551a94a63f0SWarner Losh 	struct mmc_ios *ios;
2552a94a63f0SWarner Losh 	struct mmc_ios *new_ios;
2553a94a63f0SWarner Losh 	struct ccb_trans_settings_mmc *cts;
2554a94a63f0SWarner Losh 
2555a94a63f0SWarner Losh 	ios = &slot->host.ios;
2556a94a63f0SWarner Losh 
2557a94a63f0SWarner Losh 	cts = &ccb->cts.proto_specific.mmc;
2558a94a63f0SWarner Losh 	new_ios = &cts->ios;
2559a94a63f0SWarner Losh 
2560a94a63f0SWarner Losh 	/* Update only requested fields */
2561a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CLK) {
2562a94a63f0SWarner Losh 		ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock);
2563a94a63f0SWarner Losh 		slot_printf(slot, "Clock => %d\n", ios->clock);
2564a94a63f0SWarner Losh 	}
2565a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_VDD) {
2566a94a63f0SWarner Losh 		ios->vdd = new_ios->vdd;
2567a94a63f0SWarner Losh 		slot_printf(slot, "VDD => %d\n", ios->vdd);
2568a94a63f0SWarner Losh 	}
2569a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CS) {
2570a94a63f0SWarner Losh 		ios->chip_select = new_ios->chip_select;
2571a94a63f0SWarner Losh 		slot_printf(slot, "CS => %d\n", ios->chip_select);
2572a94a63f0SWarner Losh 	}
2573a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BW) {
2574a94a63f0SWarner Losh 		ios->bus_width = new_ios->bus_width;
2575a94a63f0SWarner Losh 		slot_printf(slot, "Bus width => %d\n", ios->bus_width);
2576a94a63f0SWarner Losh 	}
2577a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_PM) {
2578a94a63f0SWarner Losh 		ios->power_mode = new_ios->power_mode;
2579a94a63f0SWarner Losh 		slot_printf(slot, "Power mode => %d\n", ios->power_mode);
2580a94a63f0SWarner Losh 	}
2581a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BT) {
2582a94a63f0SWarner Losh 		ios->timing = new_ios->timing;
2583a94a63f0SWarner Losh 		slot_printf(slot, "Timing => %d\n", ios->timing);
2584a94a63f0SWarner Losh 	}
2585a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BM) {
2586a94a63f0SWarner Losh 		ios->bus_mode = new_ios->bus_mode;
2587a94a63f0SWarner Losh 		slot_printf(slot, "Bus mode => %d\n", ios->bus_mode);
2588a94a63f0SWarner Losh 	}
2589a94a63f0SWarner Losh 
2590a94a63f0SWarner Losh         /* XXX Provide a way to call a chip-specific IOS update, required for TI */
2591a94a63f0SWarner Losh 	return (sdhci_cam_update_ios(slot));
2592a94a63f0SWarner Losh }
2593a94a63f0SWarner Losh 
2594a94a63f0SWarner Losh int
2595a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot)
2596a94a63f0SWarner Losh {
2597a94a63f0SWarner Losh 	struct mmc_ios *ios = &slot->host.ios;
2598a94a63f0SWarner Losh 
2599a94a63f0SWarner Losh 	slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n",
2600a94a63f0SWarner Losh 		    __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing);
2601a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2602a94a63f0SWarner Losh 	/* Do full reset on bus power down to clear from any state. */
2603a94a63f0SWarner Losh 	if (ios->power_mode == power_off) {
2604a94a63f0SWarner Losh 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
2605a94a63f0SWarner Losh 		sdhci_init(slot);
2606a94a63f0SWarner Losh 	}
2607a94a63f0SWarner Losh 	/* Configure the bus. */
2608a94a63f0SWarner Losh 	sdhci_set_clock(slot, ios->clock);
2609a94a63f0SWarner Losh 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
2610a94a63f0SWarner Losh 	if (ios->bus_width == bus_width_8) {
2611a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
2612a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2613a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_4) {
2614a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2615a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
2616a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_1) {
2617a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2618a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2619a94a63f0SWarner Losh 	} else {
2620a94a63f0SWarner Losh 		panic("Invalid bus width: %d", ios->bus_width);
2621a94a63f0SWarner Losh 	}
2622a94a63f0SWarner Losh 	if (ios->timing == bus_timing_hs &&
2623a94a63f0SWarner Losh 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
2624a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_HISPD;
2625a94a63f0SWarner Losh 	else
2626a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
2627a94a63f0SWarner Losh 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
2628a94a63f0SWarner Losh 	/* Some controllers like reset after bus changes. */
2629a94a63f0SWarner Losh 	if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
2630a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2631a94a63f0SWarner Losh 
2632a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2633a94a63f0SWarner Losh 	return (0);
2634a94a63f0SWarner Losh }
2635a94a63f0SWarner Losh 
2636a94a63f0SWarner Losh int
2637a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb)
2638a94a63f0SWarner Losh {
2639a94a63f0SWarner Losh 	struct ccb_mmcio *mmcio;
2640a94a63f0SWarner Losh 
2641a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
2642a94a63f0SWarner Losh 
2643a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2644a94a63f0SWarner Losh /*	if (slot->req != NULL) {
2645a94a63f0SWarner Losh 		SDHCI_UNLOCK(slot);
2646a94a63f0SWarner Losh 		return (EBUSY);
2647a94a63f0SWarner Losh 	}
2648a94a63f0SWarner Losh */
2649*aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
2650a94a63f0SWarner Losh 		slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
2651a94a63f0SWarner Losh 			    mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags,
2652a94a63f0SWarner Losh 			    mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0,
2653a94a63f0SWarner Losh 			    mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0);
2654a94a63f0SWarner Losh 	}
2655a94a63f0SWarner Losh 	if (mmcio->cmd.data != NULL) {
2656a94a63f0SWarner Losh 		if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0)
2657a94a63f0SWarner Losh 			panic("data->len = %d, data->flags = %d -- something is b0rked",
2658a94a63f0SWarner Losh 			      (int)mmcio->cmd.data->len, mmcio->cmd.data->flags);
2659a94a63f0SWarner Losh 	}
2660a94a63f0SWarner Losh 	slot->ccb = ccb;
2661a94a63f0SWarner Losh 	slot->flags = 0;
2662a94a63f0SWarner Losh 	sdhci_start(slot);
2663a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2664a94a63f0SWarner Losh 	if (dumping) {
2665a94a63f0SWarner Losh 		while (slot->ccb != NULL) {
2666a94a63f0SWarner Losh 			sdhci_generic_intr(slot);
2667a94a63f0SWarner Losh 			DELAY(10);
2668a94a63f0SWarner Losh 		}
2669a94a63f0SWarner Losh 	}
2670a94a63f0SWarner Losh 	return (0);
2671a94a63f0SWarner Losh }
267215c440e1SWarner Losh #endif /* MMCCAM */
2673a94a63f0SWarner Losh 
2674d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1);
2675