xref: /freebsd/sys/dev/sdhci/sdhci.c (revision cc22204bbc7797140f30faa1c4c3bf47dcd25fb8)
1831f5dcfSAlexander Motin /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4831f5dcfSAlexander Motin  * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
5aca38eabSMarius Strobl  * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
6831f5dcfSAlexander Motin  * All rights reserved.
7831f5dcfSAlexander Motin  *
8831f5dcfSAlexander Motin  * Redistribution and use in source and binary forms, with or without
9831f5dcfSAlexander Motin  * modification, are permitted provided that the following conditions
10831f5dcfSAlexander Motin  * are met:
11831f5dcfSAlexander Motin  * 1. Redistributions of source code must retain the above copyright
12831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer.
13831f5dcfSAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
14831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
15831f5dcfSAlexander Motin  *    documentation and/or other materials provided with the distribution.
16831f5dcfSAlexander Motin  *
17831f5dcfSAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18831f5dcfSAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19831f5dcfSAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20831f5dcfSAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21831f5dcfSAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22831f5dcfSAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23831f5dcfSAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24831f5dcfSAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25831f5dcfSAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26831f5dcfSAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27831f5dcfSAlexander Motin  */
28831f5dcfSAlexander Motin 
29831f5dcfSAlexander Motin #include <sys/cdefs.h>
30831f5dcfSAlexander Motin __FBSDID("$FreeBSD$");
31831f5dcfSAlexander Motin 
32831f5dcfSAlexander Motin #include <sys/param.h>
33831f5dcfSAlexander Motin #include <sys/systm.h>
34831f5dcfSAlexander Motin #include <sys/bus.h>
35e64f01a9SIan Lepore #include <sys/callout.h>
36831f5dcfSAlexander Motin #include <sys/conf.h>
37831f5dcfSAlexander Motin #include <sys/kernel.h>
38aca38eabSMarius Strobl #include <sys/kobj.h>
39831f5dcfSAlexander Motin #include <sys/lock.h>
40aca38eabSMarius Strobl #include <sys/malloc.h>
41831f5dcfSAlexander Motin #include <sys/module.h>
42831f5dcfSAlexander Motin #include <sys/mutex.h>
43831f5dcfSAlexander Motin #include <sys/resource.h>
44831f5dcfSAlexander Motin #include <sys/rman.h>
455b69a497SAlexander Motin #include <sys/sysctl.h>
46831f5dcfSAlexander Motin #include <sys/taskqueue.h>
47831f5dcfSAlexander Motin 
48831f5dcfSAlexander Motin #include <machine/bus.h>
49831f5dcfSAlexander Motin #include <machine/resource.h>
50831f5dcfSAlexander Motin #include <machine/stdarg.h>
51831f5dcfSAlexander Motin 
52831f5dcfSAlexander Motin #include <dev/mmc/bridge.h>
53831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h>
54831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h>
55831f5dcfSAlexander Motin 
56aca38eabSMarius Strobl #include <dev/sdhci/sdhci.h>
57aca38eabSMarius Strobl 
58a94a63f0SWarner Losh #include <cam/cam.h>
59a94a63f0SWarner Losh #include <cam/cam_ccb.h>
60a94a63f0SWarner Losh #include <cam/cam_debug.h>
61a94a63f0SWarner Losh #include <cam/cam_sim.h>
62a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h>
63a94a63f0SWarner Losh 
64831f5dcfSAlexander Motin #include "mmcbr_if.h"
65d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h"
66831f5dcfSAlexander Motin 
67a94a63f0SWarner Losh #include "opt_mmccam.h"
68a94a63f0SWarner Losh 
69f0d2731dSMarius Strobl SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver");
70831f5dcfSAlexander Motin 
71a94a63f0SWarner Losh static int sdhci_debug = 0;
727e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0,
737e6ccea3SMarius Strobl     "Debug level");
740f34084fSMarius Strobl u_int sdhci_quirk_clear = 0;
750f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear,
760f34084fSMarius Strobl     0, "Mask of quirks to clear");
770f34084fSMarius Strobl u_int sdhci_quirk_set = 0;
780f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0,
790f34084fSMarius Strobl     "Mask of quirks to set");
805b69a497SAlexander Motin 
81d6b3aaf8SOleksandr Tymoshenko #define	RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
82d6b3aaf8SOleksandr Tymoshenko #define	RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
83d6b3aaf8SOleksandr Tymoshenko #define	RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
84d6b3aaf8SOleksandr Tymoshenko #define	RD_MULTI_4(slot, off, ptr, count)	\
85d6b3aaf8SOleksandr Tymoshenko     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
86831f5dcfSAlexander Motin 
87d6b3aaf8SOleksandr Tymoshenko #define	WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
88d6b3aaf8SOleksandr Tymoshenko #define	WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
89d6b3aaf8SOleksandr Tymoshenko #define	WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
90d6b3aaf8SOleksandr Tymoshenko #define	WR_MULTI_4(slot, off, ptr, count)	\
91d6b3aaf8SOleksandr Tymoshenko     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
92831f5dcfSAlexander Motin 
93aca38eabSMarius Strobl static void sdhci_card_poll(void *arg);
94aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending);
95aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset);
96aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req);
97aca38eabSMarius Strobl static void sdhci_retune(void *arg);
98831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
99831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot);
100831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data);
101831f5dcfSAlexander Motin 
10215c440e1SWarner Losh #ifdef MMCCAM
103a94a63f0SWarner Losh /* CAM-related */
104a94a63f0SWarner Losh int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock);
105a94a63f0SWarner Losh static int sdhci_cam_update_ios(struct sdhci_slot *slot);
106a94a63f0SWarner Losh static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb);
107a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb);
108a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim);
109a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb);
11015c440e1SWarner Losh #endif
111a94a63f0SWarner Losh 
112831f5dcfSAlexander Motin /* helper routines */
1130f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot);
1140f34084fSMarius Strobl static int slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
1150f34084fSMarius Strobl     __printflike(2, 3);
116aca38eabSMarius Strobl static uint32_t sdhci_tuning_intmask(struct sdhci_slot *slot);
1170f34084fSMarius Strobl 
118831f5dcfSAlexander Motin #define	SDHCI_LOCK(_slot)		mtx_lock(&(_slot)->mtx)
119831f5dcfSAlexander Motin #define	SDHCI_UNLOCK(_slot)		mtx_unlock(&(_slot)->mtx)
120831f5dcfSAlexander Motin #define	SDHCI_LOCK_INIT(_slot) \
121831f5dcfSAlexander Motin 	mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
122831f5dcfSAlexander Motin #define	SDHCI_LOCK_DESTROY(_slot)	mtx_destroy(&_slot->mtx);
123831f5dcfSAlexander Motin #define	SDHCI_ASSERT_LOCKED(_slot)	mtx_assert(&_slot->mtx, MA_OWNED);
124831f5dcfSAlexander Motin #define	SDHCI_ASSERT_UNLOCKED(_slot)	mtx_assert(&_slot->mtx, MA_NOTOWNED);
125831f5dcfSAlexander Motin 
12633aad34dSOleksandr Tymoshenko #define	SDHCI_DEFAULT_MAX_FREQ	50
12733aad34dSOleksandr Tymoshenko 
12857677a3aSOleksandr Tymoshenko #define	SDHCI_200_MAX_DIVIDER	256
12957677a3aSOleksandr Tymoshenko #define	SDHCI_300_MAX_DIVIDER	2046
13057677a3aSOleksandr Tymoshenko 
131639f59f0SIan Lepore #define	SDHCI_CARD_PRESENT_TICKS	(hz / 5)
132639f59f0SIan Lepore #define	SDHCI_INSERT_DELAY_TICKS	(hz / 2)
133639f59f0SIan Lepore 
13493efdc63SAdrian Chadd /*
13593efdc63SAdrian Chadd  * Broadcom BCM577xx Controller Constants
13693efdc63SAdrian Chadd  */
1371bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */
1381bacf3beSMarius Strobl #define	BCM577XX_DEFAULT_MAX_DIVIDER	256
1391bacf3beSMarius Strobl /* Alternative clock's base frequency. */
1401bacf3beSMarius Strobl #define	BCM577XX_ALT_CLOCK_BASE		63000000
14193efdc63SAdrian Chadd 
14293efdc63SAdrian Chadd #define	BCM577XX_HOST_CONTROL		0x198
14393efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_MASK	0xFFFFCFFF
14493efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_SHIFT	12
14593efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_DEFAULT	0x0
14693efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_64MHZ	0x3
14793efdc63SAdrian Chadd 
148831f5dcfSAlexander Motin static void
149831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
150831f5dcfSAlexander Motin {
1517e6ccea3SMarius Strobl 
152831f5dcfSAlexander Motin 	if (error != 0) {
153831f5dcfSAlexander Motin 		printf("getaddr: error %d\n", error);
154831f5dcfSAlexander Motin 		return;
155831f5dcfSAlexander Motin 	}
156831f5dcfSAlexander Motin 	*(bus_addr_t *)arg = segs[0].ds_addr;
157831f5dcfSAlexander Motin }
158831f5dcfSAlexander Motin 
159d6b3aaf8SOleksandr Tymoshenko static int
160d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
161d6b3aaf8SOleksandr Tymoshenko {
162d6b3aaf8SOleksandr Tymoshenko 	va_list ap;
163d6b3aaf8SOleksandr Tymoshenko 	int retval;
164d6b3aaf8SOleksandr Tymoshenko 
165d6b3aaf8SOleksandr Tymoshenko 	retval = printf("%s-slot%d: ",
166d6b3aaf8SOleksandr Tymoshenko 	    device_get_nameunit(slot->bus), slot->num);
167d6b3aaf8SOleksandr Tymoshenko 
168d6b3aaf8SOleksandr Tymoshenko 	va_start(ap, fmt);
169d6b3aaf8SOleksandr Tymoshenko 	retval += vprintf(fmt, ap);
170d6b3aaf8SOleksandr Tymoshenko 	va_end(ap);
171d6b3aaf8SOleksandr Tymoshenko 	return (retval);
172d6b3aaf8SOleksandr Tymoshenko }
173d6b3aaf8SOleksandr Tymoshenko 
174831f5dcfSAlexander Motin static void
175831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot)
176831f5dcfSAlexander Motin {
1777e6ccea3SMarius Strobl 
178831f5dcfSAlexander Motin 	slot_printf(slot,
179831f5dcfSAlexander Motin 	    "============== REGISTER DUMP ==============\n");
180831f5dcfSAlexander Motin 
181831f5dcfSAlexander Motin 	slot_printf(slot, "Sys addr: 0x%08x | Version:  0x%08x\n",
182831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
183831f5dcfSAlexander Motin 	slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
184831f5dcfSAlexander Motin 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
185831f5dcfSAlexander Motin 	slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
186831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
187831f5dcfSAlexander Motin 	slot_printf(slot, "Present:  0x%08x | Host ctl: 0x%08x\n",
188831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
189831f5dcfSAlexander Motin 	slot_printf(slot, "Power:    0x%08x | Blk gap:  0x%08x\n",
190831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
191831f5dcfSAlexander Motin 	slot_printf(slot, "Wake-up:  0x%08x | Clock:    0x%08x\n",
192831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
193831f5dcfSAlexander Motin 	slot_printf(slot, "Timeout:  0x%08x | Int stat: 0x%08x\n",
194831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
195831f5dcfSAlexander Motin 	slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
196831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
1979dbf8c46SMarius Strobl 	slot_printf(slot, "AC12 err: 0x%08x | Host ctl2:0x%08x\n",
1989dbf8c46SMarius Strobl 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2));
1999dbf8c46SMarius Strobl 	slot_printf(slot, "Caps:     0x%08x | Caps2:    0x%08x\n",
2009dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2));
2019dbf8c46SMarius Strobl 	slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n",
2029dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR));
2039dbf8c46SMarius Strobl 	slot_printf(slot, "ADMA addr:0x%08x | Slot int: 0x%08x\n",
2049dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS));
205831f5dcfSAlexander Motin 
206831f5dcfSAlexander Motin 	slot_printf(slot,
207831f5dcfSAlexander Motin 	    "===========================================\n");
208831f5dcfSAlexander Motin }
209831f5dcfSAlexander Motin 
210831f5dcfSAlexander Motin static void
211831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
212831f5dcfSAlexander Motin {
213831f5dcfSAlexander Motin 	int timeout;
214b440e965SMarius Strobl 	uint32_t clock;
215831f5dcfSAlexander Motin 
216d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
2176e37fb2bSIan Lepore 		if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
218831f5dcfSAlexander Motin 			return;
219831f5dcfSAlexander Motin 	}
220831f5dcfSAlexander Motin 
221831f5dcfSAlexander Motin 	/* Some controllers need this kick or reset won't work. */
222831f5dcfSAlexander Motin 	if ((mask & SDHCI_RESET_ALL) == 0 &&
223d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
224831f5dcfSAlexander Motin 		/* This is to force an update */
225831f5dcfSAlexander Motin 		clock = slot->clock;
226831f5dcfSAlexander Motin 		slot->clock = 0;
227831f5dcfSAlexander Motin 		sdhci_set_clock(slot, clock);
228831f5dcfSAlexander Motin 	}
229831f5dcfSAlexander Motin 
230d8208d9eSAlexander Motin 	if (mask & SDHCI_RESET_ALL) {
231831f5dcfSAlexander Motin 		slot->clock = 0;
232d8208d9eSAlexander Motin 		slot->power = 0;
233d8208d9eSAlexander Motin 	}
234831f5dcfSAlexander Motin 
23561bc42f7SIan Lepore 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
23661bc42f7SIan Lepore 
23761bc42f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
23861bc42f7SIan Lepore 		/*
23961bc42f7SIan Lepore 		 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
24061bc42f7SIan Lepore 		 * specification.  The reset bit has internal propagation delay,
24161bc42f7SIan Lepore 		 * so a fast read after write returns 0 even if reset process is
24261bc42f7SIan Lepore 		 * in progress.  The workaround is to poll for 1 before polling
24361bc42f7SIan Lepore 		 * for 0.  In the worst case, if we miss seeing it asserted the
24461bc42f7SIan Lepore 		 * time we spent waiting is enough to ensure the reset finishes.
24561bc42f7SIan Lepore 		 */
24661bc42f7SIan Lepore 		timeout = 10000;
24761bc42f7SIan Lepore 		while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
24861bc42f7SIan Lepore 			if (timeout <= 0)
24961bc42f7SIan Lepore 				break;
25061bc42f7SIan Lepore 			timeout--;
25161bc42f7SIan Lepore 			DELAY(1);
25261bc42f7SIan Lepore 		}
25361bc42f7SIan Lepore 	}
25461bc42f7SIan Lepore 
255831f5dcfSAlexander Motin 	/* Wait max 100 ms */
25661bc42f7SIan Lepore 	timeout = 10000;
257831f5dcfSAlexander Motin 	/* Controller clears the bits when it's done */
25861bc42f7SIan Lepore 	while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
25961bc42f7SIan Lepore 		if (timeout <= 0) {
26061bc42f7SIan Lepore 			slot_printf(slot, "Reset 0x%x never completed.\n",
26161bc42f7SIan Lepore 			    mask);
262831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
263831f5dcfSAlexander Motin 			return;
264831f5dcfSAlexander Motin 		}
265831f5dcfSAlexander Motin 		timeout--;
26661bc42f7SIan Lepore 		DELAY(10);
267831f5dcfSAlexander Motin 	}
268831f5dcfSAlexander Motin }
269831f5dcfSAlexander Motin 
270aca38eabSMarius Strobl static uint32_t
271aca38eabSMarius Strobl sdhci_tuning_intmask(struct sdhci_slot *slot)
272aca38eabSMarius Strobl {
273aca38eabSMarius Strobl 	uint32_t intmask;
274aca38eabSMarius Strobl 
275aca38eabSMarius Strobl 	intmask = 0;
276aca38eabSMarius Strobl 	if (slot->opt & SDHCI_TUNING_SUPPORTED) {
277aca38eabSMarius Strobl 		intmask |= SDHCI_INT_TUNEERR;
278aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_2 ||
279aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_3)
280aca38eabSMarius Strobl 			intmask |= SDHCI_INT_RETUNE;
281aca38eabSMarius Strobl 	}
282aca38eabSMarius Strobl 	return (intmask);
283aca38eabSMarius Strobl }
284aca38eabSMarius Strobl 
285831f5dcfSAlexander Motin static void
286831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot)
287831f5dcfSAlexander Motin {
288831f5dcfSAlexander Motin 
289831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
290831f5dcfSAlexander Motin 
291831f5dcfSAlexander Motin 	/* Enable interrupts. */
292831f5dcfSAlexander Motin 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
293831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
294831f5dcfSAlexander Motin 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
295831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
296831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
2973685b398SWarner Losh 	    SDHCI_INT_ACMD12ERR;
298639f59f0SIan Lepore 
299639f59f0SIan Lepore 	if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
300639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
301639f59f0SIan Lepore 		slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
302639f59f0SIan Lepore 	}
303639f59f0SIan Lepore 
304*cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
305831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
306831f5dcfSAlexander Motin }
307831f5dcfSAlexander Motin 
308831f5dcfSAlexander Motin static void
309831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
310831f5dcfSAlexander Motin {
31193efdc63SAdrian Chadd 	uint32_t clk_base;
31293efdc63SAdrian Chadd 	uint32_t clk_sel;
313831f5dcfSAlexander Motin 	uint32_t res;
314831f5dcfSAlexander Motin 	uint16_t clk;
3158f3b7d56SOleksandr Tymoshenko 	uint16_t div;
316831f5dcfSAlexander Motin 	int timeout;
317831f5dcfSAlexander Motin 
318831f5dcfSAlexander Motin 	if (clock == slot->clock)
319831f5dcfSAlexander Motin 		return;
320831f5dcfSAlexander Motin 	slot->clock = clock;
321831f5dcfSAlexander Motin 
322831f5dcfSAlexander Motin 	/* Turn off the clock. */
3234ddc0172SIan Lepore 	clk = RD2(slot, SDHCI_CLOCK_CONTROL);
3244ddc0172SIan Lepore 	WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
325b440e965SMarius Strobl 	/* If no clock requested - leave it so. */
326831f5dcfSAlexander Motin 	if (clock == 0)
327831f5dcfSAlexander Motin 		return;
328ceb9e9f7SIan Lepore 
32993efdc63SAdrian Chadd 	/* Determine the clock base frequency */
33093efdc63SAdrian Chadd 	clk_base = slot->max_clk;
33193efdc63SAdrian Chadd 	if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
3321bacf3beSMarius Strobl 		clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) &
3331bacf3beSMarius Strobl 		    BCM577XX_CTRL_CLKSEL_MASK;
33493efdc63SAdrian Chadd 
3351bacf3beSMarius Strobl 		/*
3361bacf3beSMarius Strobl 		 * Select clock source appropriate for the requested frequency.
3371bacf3beSMarius Strobl 		 */
33893efdc63SAdrian Chadd 		if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
33993efdc63SAdrian Chadd 			clk_base = BCM577XX_ALT_CLOCK_BASE;
3401bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ <<
3411bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
34293efdc63SAdrian Chadd 		} else {
3431bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT <<
3441bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
34593efdc63SAdrian Chadd 		}
34693efdc63SAdrian Chadd 
34793efdc63SAdrian Chadd 		WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
34893efdc63SAdrian Chadd 	}
34993efdc63SAdrian Chadd 
350ceb9e9f7SIan Lepore 	/* Recalculate timeout clock frequency based on the new sd clock. */
351ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
352ceb9e9f7SIan Lepore 		slot->timeout_clk = slot->clock / 1000;
353ceb9e9f7SIan Lepore 
3548f3b7d56SOleksandr Tymoshenko 	if (slot->version < SDHCI_SPEC_300) {
355831f5dcfSAlexander Motin 		/* Looking for highest freq <= clock. */
35693efdc63SAdrian Chadd 		res = clk_base;
35757677a3aSOleksandr Tymoshenko 		for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
358831f5dcfSAlexander Motin 			if (res <= clock)
359831f5dcfSAlexander Motin 				break;
360831f5dcfSAlexander Motin 			res >>= 1;
361831f5dcfSAlexander Motin 		}
362831f5dcfSAlexander Motin 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
3638f3b7d56SOleksandr Tymoshenko 		div >>= 1;
364c11bbc7dSMarius Strobl 	} else {
3658f3b7d56SOleksandr Tymoshenko 		/* Version 3.0 divisors are multiples of two up to 1023 * 2 */
36693efdc63SAdrian Chadd 		if (clock >= clk_base)
36757677a3aSOleksandr Tymoshenko 			div = 0;
3688f3b7d56SOleksandr Tymoshenko 		else {
36957677a3aSOleksandr Tymoshenko 			for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
37093efdc63SAdrian Chadd 				if ((clk_base / div) <= clock)
3718f3b7d56SOleksandr Tymoshenko 					break;
3728f3b7d56SOleksandr Tymoshenko 			}
3738f3b7d56SOleksandr Tymoshenko 		}
3748f3b7d56SOleksandr Tymoshenko 		div >>= 1;
3758f3b7d56SOleksandr Tymoshenko 	}
3768f3b7d56SOleksandr Tymoshenko 
3778f3b7d56SOleksandr Tymoshenko 	if (bootverbose || sdhci_debug)
37893efdc63SAdrian Chadd 		slot_printf(slot, "Divider %d for freq %d (base %d)\n",
37993efdc63SAdrian Chadd 			div, clock, clk_base);
3808f3b7d56SOleksandr Tymoshenko 
381831f5dcfSAlexander Motin 	/* Now we have got divider, set it. */
3828f3b7d56SOleksandr Tymoshenko 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
3838f3b7d56SOleksandr Tymoshenko 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
3848f3b7d56SOleksandr Tymoshenko 		<< SDHCI_DIVIDER_HI_SHIFT;
3858f3b7d56SOleksandr Tymoshenko 
386831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
387831f5dcfSAlexander Motin 	/* Enable clock. */
388831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_INT_EN;
389831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
390831f5dcfSAlexander Motin 	/* Wait up to 10 ms until it stabilize. */
391831f5dcfSAlexander Motin 	timeout = 10;
392831f5dcfSAlexander Motin 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
393831f5dcfSAlexander Motin 		& SDHCI_CLOCK_INT_STABLE)) {
394831f5dcfSAlexander Motin 		if (timeout == 0) {
395831f5dcfSAlexander Motin 			slot_printf(slot,
396831f5dcfSAlexander Motin 			    "Internal clock never stabilised.\n");
397831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
398831f5dcfSAlexander Motin 			return;
399831f5dcfSAlexander Motin 		}
400831f5dcfSAlexander Motin 		timeout--;
401831f5dcfSAlexander Motin 		DELAY(1000);
402831f5dcfSAlexander Motin 	}
403831f5dcfSAlexander Motin 	/* Pass clock signal to the bus. */
404831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_CARD_EN;
405831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
406831f5dcfSAlexander Motin }
407831f5dcfSAlexander Motin 
408831f5dcfSAlexander Motin static void
409831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power)
410831f5dcfSAlexander Motin {
41185083a80SMarius Strobl 	int i;
412831f5dcfSAlexander Motin 	uint8_t pwr;
413831f5dcfSAlexander Motin 
414831f5dcfSAlexander Motin 	if (slot->power == power)
415831f5dcfSAlexander Motin 		return;
416d6b3aaf8SOleksandr Tymoshenko 
417831f5dcfSAlexander Motin 	slot->power = power;
418831f5dcfSAlexander Motin 
419831f5dcfSAlexander Motin 	/* Turn off the power. */
420831f5dcfSAlexander Motin 	pwr = 0;
421831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
422b440e965SMarius Strobl 	/* If power down requested - leave it so. */
423831f5dcfSAlexander Motin 	if (power == 0)
424831f5dcfSAlexander Motin 		return;
425831f5dcfSAlexander Motin 	/* Set voltage. */
426831f5dcfSAlexander Motin 	switch (1 << power) {
427831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
428831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
429831f5dcfSAlexander Motin 		break;
430831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
431831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
432831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
433831f5dcfSAlexander Motin 		break;
434831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
435831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
436831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
437831f5dcfSAlexander Motin 		break;
438831f5dcfSAlexander Motin 	}
439831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
44085083a80SMarius Strobl 	/*
44185083a80SMarius Strobl 	 * Turn on VDD1 power.  Note that at least some Intel controllers can
44285083a80SMarius Strobl 	 * fail to enable bus power on the first try after transiting from D3
4438022c8ebSMarius Strobl 	 * to D0, so we give them up to 2 ms.
44485083a80SMarius Strobl 	 */
445831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
44685083a80SMarius Strobl 	for (i = 0; i < 20; i++) {
447831f5dcfSAlexander Motin 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
44885083a80SMarius Strobl 		if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)
44985083a80SMarius Strobl 			break;
45085083a80SMarius Strobl 		DELAY(100);
45185083a80SMarius Strobl 	}
45285083a80SMarius Strobl 	if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON))
45385083a80SMarius Strobl 		slot_printf(slot, "Bus power failed to enable");
454a2832f9fSMarius Strobl 
455a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) {
456a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10);
457a2832f9fSMarius Strobl 		DELAY(10);
458a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
459a2832f9fSMarius Strobl 		DELAY(300);
460a2832f9fSMarius Strobl 	}
461831f5dcfSAlexander Motin }
462831f5dcfSAlexander Motin 
463831f5dcfSAlexander Motin static void
464831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
465831f5dcfSAlexander Motin {
466831f5dcfSAlexander Motin 	uint32_t data;
467831f5dcfSAlexander Motin 	char *buffer;
468831f5dcfSAlexander Motin 	size_t left;
469831f5dcfSAlexander Motin 
470831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
471831f5dcfSAlexander Motin 	buffer += slot->offset;
472831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
473831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
474831f5dcfSAlexander Motin 	slot->offset += left;
475831f5dcfSAlexander Motin 
476831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
477d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
478831f5dcfSAlexander Motin 		DELAY(10);
479ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
480831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
481831f5dcfSAlexander Motin 		while (left > 3) {
482831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
483831f5dcfSAlexander Motin 			buffer[0] = data;
484831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
485831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
486831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
487831f5dcfSAlexander Motin 			buffer += 4;
488831f5dcfSAlexander Motin 			left -= 4;
489831f5dcfSAlexander Motin 		}
490831f5dcfSAlexander Motin 	} else {
491d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
492831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
493831f5dcfSAlexander Motin 		left &= 3;
494831f5dcfSAlexander Motin 	}
495831f5dcfSAlexander Motin 	/* Handle uneven size case. */
496831f5dcfSAlexander Motin 	if (left > 0) {
497831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
498831f5dcfSAlexander Motin 		while (left > 0) {
499831f5dcfSAlexander Motin 			*(buffer++) = data;
500831f5dcfSAlexander Motin 			data >>= 8;
501831f5dcfSAlexander Motin 			left--;
502831f5dcfSAlexander Motin 		}
503831f5dcfSAlexander Motin 	}
504831f5dcfSAlexander Motin }
505831f5dcfSAlexander Motin 
506831f5dcfSAlexander Motin static void
507831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
508831f5dcfSAlexander Motin {
509831f5dcfSAlexander Motin 	uint32_t data = 0;
510831f5dcfSAlexander Motin 	char *buffer;
511831f5dcfSAlexander Motin 	size_t left;
512831f5dcfSAlexander Motin 
513831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
514831f5dcfSAlexander Motin 	buffer += slot->offset;
515831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
516831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
517831f5dcfSAlexander Motin 	slot->offset += left;
518831f5dcfSAlexander Motin 
519ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
520831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
521831f5dcfSAlexander Motin 		while (left > 3) {
522831f5dcfSAlexander Motin 			data = buffer[0] +
523831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
524831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
525831f5dcfSAlexander Motin 			    (buffer[3] << 24);
526831f5dcfSAlexander Motin 			left -= 4;
527831f5dcfSAlexander Motin 			buffer += 4;
528831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
529831f5dcfSAlexander Motin 		}
530831f5dcfSAlexander Motin 	} else {
531d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
532831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
533831f5dcfSAlexander Motin 		left &= 3;
534831f5dcfSAlexander Motin 	}
535831f5dcfSAlexander Motin 	/* Handle uneven size case. */
536831f5dcfSAlexander Motin 	if (left > 0) {
537831f5dcfSAlexander Motin 		while (left > 0) {
538831f5dcfSAlexander Motin 			data <<= 8;
539831f5dcfSAlexander Motin 			data += *(buffer++);
540831f5dcfSAlexander Motin 			left--;
541831f5dcfSAlexander Motin 		}
542831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
543831f5dcfSAlexander Motin 	}
544831f5dcfSAlexander Motin }
545831f5dcfSAlexander Motin 
546831f5dcfSAlexander Motin static void
547831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
548831f5dcfSAlexander Motin {
549831f5dcfSAlexander Motin 
550831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
551831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
552831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
553831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
554831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
555831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
556831f5dcfSAlexander Motin 				break;
557831f5dcfSAlexander Motin 		}
558831f5dcfSAlexander Motin 	} else {
559831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
560831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
561831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
562831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
563831f5dcfSAlexander Motin 				break;
564831f5dcfSAlexander Motin 		}
565831f5dcfSAlexander Motin 	}
566831f5dcfSAlexander Motin }
567831f5dcfSAlexander Motin 
568831f5dcfSAlexander Motin static void
5697e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused)
570831f5dcfSAlexander Motin {
571831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
5727e6ccea3SMarius Strobl 	device_t d;
573831f5dcfSAlexander Motin 
574831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
5756e37fb2bSIan Lepore 	if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
576a94a63f0SWarner Losh #ifdef MMCCAM
577a94a63f0SWarner Losh 		if (slot->card_present == 0) {
578a94a63f0SWarner Losh #else
579831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
580a94a63f0SWarner Losh #endif
581831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
582639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
583639f59f0SIan Lepore 				slot_printf(slot, "Card inserted\n");
584a94a63f0SWarner Losh #ifdef MMCCAM
585a94a63f0SWarner Losh 			slot->card_present = 1;
586a94a63f0SWarner Losh 			union ccb *ccb;
587a94a63f0SWarner Losh 			uint32_t pathid;
588a94a63f0SWarner Losh 			pathid = cam_sim_path(slot->sim);
589a94a63f0SWarner Losh 			ccb = xpt_alloc_ccb_nowait();
590a94a63f0SWarner Losh 			if (ccb == NULL) {
591a94a63f0SWarner Losh 				slot_printf(slot, "Unable to alloc CCB for rescan\n");
592a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
593a94a63f0SWarner Losh 				return;
594a94a63f0SWarner Losh 			}
595a94a63f0SWarner Losh 
596a94a63f0SWarner Losh 			/*
597a94a63f0SWarner Losh 			 * We create a rescan request for BUS:0:0, since the card
598a94a63f0SWarner Losh 			 * will be at lun 0.
599a94a63f0SWarner Losh 			 */
600a94a63f0SWarner Losh 			if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
601a94a63f0SWarner Losh 					    /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
602a94a63f0SWarner Losh 				slot_printf(slot, "Unable to create path for rescan\n");
603a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
604a94a63f0SWarner Losh 				xpt_free_ccb(ccb);
605a94a63f0SWarner Losh 				return;
606a94a63f0SWarner Losh 			}
607a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
608a94a63f0SWarner Losh 			xpt_rescan(ccb);
609a94a63f0SWarner Losh #else
610aca38eabSMarius Strobl 			d = slot->dev = device_add_child(slot->bus, "mmc", -1);
611831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
612aca38eabSMarius Strobl 			if (d) {
613aca38eabSMarius Strobl 				device_set_ivars(d, slot);
614aca38eabSMarius Strobl 				(void)device_probe_and_attach(d);
615aca38eabSMarius Strobl 			}
616a94a63f0SWarner Losh #endif
617831f5dcfSAlexander Motin 		} else
618831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
619831f5dcfSAlexander Motin 	} else {
620a94a63f0SWarner Losh #ifdef MMCCAM
621a94a63f0SWarner Losh 		if (slot->card_present == 1) {
622a94a63f0SWarner Losh #else
623831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
624a94a63f0SWarner Losh #endif
625831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
626639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
627639f59f0SIan Lepore 				slot_printf(slot, "Card removed\n");
6287e6ccea3SMarius Strobl 			d = slot->dev;
629831f5dcfSAlexander Motin 			slot->dev = NULL;
630a94a63f0SWarner Losh #ifdef MMCCAM
631a94a63f0SWarner Losh 			slot->card_present = 0;
632a94a63f0SWarner Losh 			union ccb *ccb;
633a94a63f0SWarner Losh 			uint32_t pathid;
634a94a63f0SWarner Losh 			pathid = cam_sim_path(slot->sim);
635a94a63f0SWarner Losh 			ccb = xpt_alloc_ccb_nowait();
636a94a63f0SWarner Losh 			if (ccb == NULL) {
637a94a63f0SWarner Losh 				slot_printf(slot, "Unable to alloc CCB for rescan\n");
638a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
639a94a63f0SWarner Losh 				return;
640a94a63f0SWarner Losh 			}
641a94a63f0SWarner Losh 
642a94a63f0SWarner Losh 			/*
643a94a63f0SWarner Losh 			 * We create a rescan request for BUS:0:0, since the card
644a94a63f0SWarner Losh 			 * will be at lun 0.
645a94a63f0SWarner Losh 			 */
646a94a63f0SWarner Losh 			if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
647a94a63f0SWarner Losh 					    /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
648a94a63f0SWarner Losh 				slot_printf(slot, "Unable to create path for rescan\n");
649a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
650a94a63f0SWarner Losh 				xpt_free_ccb(ccb);
651a94a63f0SWarner Losh 				return;
652a94a63f0SWarner Losh 			}
653a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
654a94a63f0SWarner Losh 			xpt_rescan(ccb);
655a94a63f0SWarner Losh #else
656aca38eabSMarius Strobl 			slot->intmask &= ~sdhci_tuning_intmask(slot);
657*cc22204bSMarius Strobl 			WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
658aca38eabSMarius Strobl 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
659aca38eabSMarius Strobl 			slot->opt &= ~SDHCI_TUNING_ENABLED;
660831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
661aca38eabSMarius Strobl 			callout_drain(&slot->retune_callout);
662d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
663a94a63f0SWarner Losh #endif
664831f5dcfSAlexander Motin 		} else
665831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
666831f5dcfSAlexander Motin 	}
667831f5dcfSAlexander Motin }
668831f5dcfSAlexander Motin 
669b8bf08b1SIan Lepore static void
670b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
671639f59f0SIan Lepore {
672639f59f0SIan Lepore 	bool was_present;
673639f59f0SIan Lepore 
674639f59f0SIan Lepore 	/*
675639f59f0SIan Lepore 	 * If there was no card and now there is one, schedule the task to
676639f59f0SIan Lepore 	 * create the child device after a short delay.  The delay is to
677639f59f0SIan Lepore 	 * debounce the card insert (sometimes the card detect pin stabilizes
678639f59f0SIan Lepore 	 * before the other pins have made good contact).
679639f59f0SIan Lepore 	 *
680639f59f0SIan Lepore 	 * If there was a card present and now it's gone, immediately schedule
681639f59f0SIan Lepore 	 * the task to delete the child device.  No debouncing -- gone is gone,
682639f59f0SIan Lepore 	 * because once power is removed, a full card re-init is needed, and
683639f59f0SIan Lepore 	 * that happens by deleting and recreating the child device.
684639f59f0SIan Lepore 	 */
685a94a63f0SWarner Losh #ifdef MMCCAM
686a94a63f0SWarner Losh 	was_present = slot->card_present;
687a94a63f0SWarner Losh #else
688639f59f0SIan Lepore 	was_present = slot->dev != NULL;
689a94a63f0SWarner Losh #endif
690639f59f0SIan Lepore 	if (!was_present && is_present) {
691639f59f0SIan Lepore 		taskqueue_enqueue_timeout(taskqueue_swi_giant,
692639f59f0SIan Lepore 		    &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
693639f59f0SIan Lepore 	} else if (was_present && !is_present) {
694639f59f0SIan Lepore 		taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
695639f59f0SIan Lepore 	}
696b8bf08b1SIan Lepore }
697b8bf08b1SIan Lepore 
698b8bf08b1SIan Lepore void
699b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
700b8bf08b1SIan Lepore {
701b8bf08b1SIan Lepore 
702b8bf08b1SIan Lepore 	SDHCI_LOCK(slot);
703b8bf08b1SIan Lepore 	sdhci_handle_card_present_locked(slot, is_present);
704639f59f0SIan Lepore 	SDHCI_UNLOCK(slot);
705639f59f0SIan Lepore }
706639f59f0SIan Lepore 
707639f59f0SIan Lepore static void
708639f59f0SIan Lepore sdhci_card_poll(void *arg)
709639f59f0SIan Lepore {
710639f59f0SIan Lepore 	struct sdhci_slot *slot = arg;
711639f59f0SIan Lepore 
712639f59f0SIan Lepore 	sdhci_handle_card_present(slot,
713639f59f0SIan Lepore 	    SDHCI_GET_CARD_PRESENT(slot->bus, slot));
714639f59f0SIan Lepore 	callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
715639f59f0SIan Lepore 	    sdhci_card_poll, slot);
716639f59f0SIan Lepore }
717639f59f0SIan Lepore 
718d6b3aaf8SOleksandr Tymoshenko int
719d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
720831f5dcfSAlexander Motin {
721aca38eabSMarius Strobl 	kobjop_desc_t kobj_desc;
722aca38eabSMarius Strobl 	kobj_method_t *kobj_method;
7230f34084fSMarius Strobl 	uint32_t caps, caps2, freq, host_caps;
724d6b3aaf8SOleksandr Tymoshenko 	int err;
725831f5dcfSAlexander Motin 
726831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
727a94a63f0SWarner Losh 
728d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
729d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
730d6b3aaf8SOleksandr Tymoshenko 
731831f5dcfSAlexander Motin 	/* Allocate DMA tag. */
732831f5dcfSAlexander Motin 	err = bus_dma_tag_create(bus_get_dma_tag(dev),
733831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
734831f5dcfSAlexander Motin 	    BUS_SPACE_MAXADDR, NULL, NULL,
735831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE,
736831f5dcfSAlexander Motin 	    BUS_DMA_ALLOCNOW, NULL, NULL,
737831f5dcfSAlexander Motin 	    &slot->dmatag);
738831f5dcfSAlexander Motin 	if (err != 0) {
739831f5dcfSAlexander Motin 		device_printf(dev, "Can't create DMA tag\n");
740831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
741d6b3aaf8SOleksandr Tymoshenko 		return (err);
742831f5dcfSAlexander Motin 	}
743831f5dcfSAlexander Motin 	/* Allocate DMA memory. */
744831f5dcfSAlexander Motin 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
745831f5dcfSAlexander Motin 	    BUS_DMA_NOWAIT, &slot->dmamap);
746831f5dcfSAlexander Motin 	if (err != 0) {
747831f5dcfSAlexander Motin 		device_printf(dev, "Can't alloc DMA memory\n");
7487fcf4780SMarius Strobl 		bus_dma_tag_destroy(slot->dmatag);
749831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
750d6b3aaf8SOleksandr Tymoshenko 		return (err);
751831f5dcfSAlexander Motin 	}
752831f5dcfSAlexander Motin 	/* Map the memory. */
753831f5dcfSAlexander Motin 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
754831f5dcfSAlexander Motin 	    (void *)slot->dmamem, DMA_BLOCK_SIZE,
755831f5dcfSAlexander Motin 	    sdhci_getaddr, &slot->paddr, 0);
756831f5dcfSAlexander Motin 	if (err != 0 || slot->paddr == 0) {
757831f5dcfSAlexander Motin 		device_printf(dev, "Can't load DMA memory\n");
7587fcf4780SMarius Strobl 		bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
7597fcf4780SMarius Strobl 		bus_dma_tag_destroy(slot->dmatag);
760831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
761d6b3aaf8SOleksandr Tymoshenko 		if (err)
762d6b3aaf8SOleksandr Tymoshenko 			return (err);
763d6b3aaf8SOleksandr Tymoshenko 		else
764d6b3aaf8SOleksandr Tymoshenko 			return (EFAULT);
765831f5dcfSAlexander Motin 	}
766d6b3aaf8SOleksandr Tymoshenko 
767d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
768d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
7690f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) {
7708f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
7710f34084fSMarius Strobl 		caps2 = slot->caps2;
7720f34084fSMarius Strobl 	} else {
773831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
7740f34084fSMarius Strobl 		if (slot->version >= SDHCI_SPEC_300)
7750f34084fSMarius Strobl 			caps2 = RD4(slot, SDHCI_CAPABILITIES2);
7760f34084fSMarius Strobl 		else
7770f34084fSMarius Strobl 			caps2 = 0;
7780f34084fSMarius Strobl 	}
7797fcf4780SMarius Strobl 	if (slot->version >= SDHCI_SPEC_300) {
7807fcf4780SMarius Strobl 		if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE &&
7817fcf4780SMarius Strobl 		    (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) {
7827fcf4780SMarius Strobl 			device_printf(dev,
7837fcf4780SMarius Strobl 			    "Driver doesn't support shared bus slots\n");
7847fcf4780SMarius Strobl 			bus_dmamap_unload(slot->dmatag, slot->dmamap);
7857fcf4780SMarius Strobl 			bus_dmamem_free(slot->dmatag, slot->dmamem,
7867fcf4780SMarius Strobl 			    slot->dmamap);
7877fcf4780SMarius Strobl 			bus_dma_tag_destroy(slot->dmatag);
7887fcf4780SMarius Strobl 			SDHCI_LOCK_DESTROY(slot);
7897fcf4780SMarius Strobl 			return (ENXIO);
7907fcf4780SMarius Strobl 		} else if ((caps & SDHCI_SLOTTYPE_MASK) ==
7917fcf4780SMarius Strobl 		    SDHCI_SLOTTYPE_EMBEDDED) {
7927fcf4780SMarius Strobl 			slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE;
7937fcf4780SMarius Strobl 		}
7947fcf4780SMarius Strobl 	}
795831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
79633aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
79787a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
79887a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
79933aad34dSOleksandr Tymoshenko 	else
80087a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
80187a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
80287a6a871SIan Lepore 	if (freq != 0)
80387a6a871SIan Lepore 		slot->max_clk = freq * 1000000;
80487a6a871SIan Lepore 	/*
80587a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
80687a6a871SIan Lepore 	 * hasn't already set max_clk we're probably not going to work right
80787a6a871SIan Lepore 	 * with an assumption, so complain about it.
80887a6a871SIan Lepore 	 */
809831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
81087a6a871SIan Lepore 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
811831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify base clock "
8121bacf3beSMarius Strobl 		    "frequency, using %dMHz as default.\n",
8131bacf3beSMarius Strobl 		    SDHCI_DEFAULT_MAX_FREQ);
814831f5dcfSAlexander Motin 	}
815a2832f9fSMarius Strobl 	/* Calculate/set timeout clock frequency. */
8168f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
8178f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
818a2832f9fSMarius Strobl 	} else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) {
819a2832f9fSMarius Strobl 		slot->timeout_clk = 1000;
8208f3b7d56SOleksandr Tymoshenko 	} else {
8211bacf3beSMarius Strobl 		slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >>
8221bacf3beSMarius Strobl 		    SDHCI_TIMEOUT_CLK_SHIFT;
8238f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
8248f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
8258f3b7d56SOleksandr Tymoshenko 	}
82687a6a871SIan Lepore 	/*
82787a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
82887a6a871SIan Lepore 	 * hasn't already set timeout_clk we'll probably work okay using the
82987a6a871SIan Lepore 	 * max timeout, but still mention it.
83087a6a871SIan Lepore 	 */
831831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
832831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify timeout clock "
833ceb9e9f7SIan Lepore 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
834ceb9e9f7SIan Lepore 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
835831f5dcfSAlexander Motin 	}
836831f5dcfSAlexander Motin 
83757677a3aSOleksandr Tymoshenko 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
838831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
839831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
840831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
841831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
842831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
843831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
8447fcf4780SMarius Strobl 	/* 1.8V VDD is not supposed to be used for removable cards. */
8457fcf4780SMarius Strobl 	if ((caps & SDHCI_CAN_VDD_180) && (slot->opt & SDHCI_SLOT_EMBEDDED))
846831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
847831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
848831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't report any "
849831f5dcfSAlexander Motin 		    "support voltages.\n");
850831f5dcfSAlexander Motin 	}
851aca38eabSMarius Strobl 
8520f34084fSMarius Strobl 	host_caps = MMC_CAP_4_BIT_DATA;
8532d1731b8SIan Lepore 	if (caps & SDHCI_CAN_DO_8BITBUS)
8540f34084fSMarius Strobl 		host_caps |= MMC_CAP_8_BIT_DATA;
855831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
8560f34084fSMarius Strobl 		host_caps |= MMC_CAP_HSPEED;
85772dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC)
8580f34084fSMarius Strobl 		host_caps |= MMC_CAP_BOOT_NOACC;
85972dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY)
8600f34084fSMarius Strobl 		host_caps |= MMC_CAP_WAIT_WHILE_BUSY;
861aca38eabSMarius Strobl 
862aca38eabSMarius Strobl 	/* Determine supported UHS-I and eMMC modes. */
8630f34084fSMarius Strobl 	if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50))
8640f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
8650f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_SDR104) {
8660f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
8670f34084fSMarius Strobl 		if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200))
8680f34084fSMarius Strobl 			host_caps |= MMC_CAP_MMC_HS200;
8690f34084fSMarius Strobl 	} else if (caps2 & SDHCI_CAN_SDR50)
8700f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR50;
8710f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_DDR50 &&
8720f34084fSMarius Strobl 	    !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50))
8730f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_DDR50;
8740f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_DDR52)
8750f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_DDR52;
8760f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 &&
8770f34084fSMarius Strobl 	    caps2 & SDHCI_CAN_MMC_HS400)
8780f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
879aca38eabSMarius Strobl 
880aca38eabSMarius Strobl 	/*
881aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes if the set_uhs_timing method is the
882aca38eabSMarius Strobl 	 * default NULL implementation.
883aca38eabSMarius Strobl 	 */
884aca38eabSMarius Strobl 	kobj_desc = &sdhci_set_uhs_timing_desc;
885aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
886aca38eabSMarius Strobl 	    kobj_desc);
887aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
888aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
889aca38eabSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
890aca38eabSMarius Strobl 		    MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400);
891aca38eabSMarius Strobl 
892aca38eabSMarius Strobl #define	SDHCI_CAP_MODES_TUNING(caps2)					\
893aca38eabSMarius Strobl     (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) |		\
894aca38eabSMarius Strobl     MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 |	\
895aca38eabSMarius Strobl     MMC_CAP_MMC_HS400)
896aca38eabSMarius Strobl 
897aca38eabSMarius Strobl 	/*
898aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes that require (re-)tuning if either
899aca38eabSMarius Strobl 	 * the tune or re-tune method is the default NULL implementation.
900aca38eabSMarius Strobl 	 */
901aca38eabSMarius Strobl 	kobj_desc = &mmcbr_tune_desc;
902aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
903aca38eabSMarius Strobl 	    kobj_desc);
904aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
905aca38eabSMarius Strobl 		goto no_tuning;
906aca38eabSMarius Strobl 	kobj_desc = &mmcbr_retune_desc;
907aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
908aca38eabSMarius Strobl 	    kobj_desc);
909aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt) {
910aca38eabSMarius Strobl no_tuning:
911aca38eabSMarius Strobl 		host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2));
912aca38eabSMarius Strobl 	}
913aca38eabSMarius Strobl 
914aca38eabSMarius Strobl 	/* Allocate tuning structures and determine tuning parameters. */
915aca38eabSMarius Strobl 	if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) {
916aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_SUPPORTED;
917aca38eabSMarius Strobl 		slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF,
918aca38eabSMarius Strobl 		    M_WAITOK);
919aca38eabSMarius Strobl 		slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF,
920aca38eabSMarius Strobl 		    M_WAITOK);
921aca38eabSMarius Strobl 		slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF,
922aca38eabSMarius Strobl 		    M_WAITOK);
923aca38eabSMarius Strobl 		if (caps2 & SDHCI_TUNE_SDR50)
924aca38eabSMarius Strobl 			slot->opt |= SDHCI_SDR50_NEEDS_TUNING;
925aca38eabSMarius Strobl 		slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >>
926aca38eabSMarius Strobl 		    SDHCI_RETUNE_MODES_SHIFT;
927aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_1) {
928aca38eabSMarius Strobl 			slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >>
929aca38eabSMarius Strobl 			    SDHCI_RETUNE_CNT_SHIFT;
930aca38eabSMarius Strobl 			if (slot->retune_count > 0xb) {
931aca38eabSMarius Strobl 				device_printf(dev, "Unknown re-tuning count "
932aca38eabSMarius Strobl 				    "%x, using 1 sec\n", slot->retune_count);
933aca38eabSMarius Strobl 				slot->retune_count = 1;
934aca38eabSMarius Strobl 			} else if (slot->retune_count != 0)
935aca38eabSMarius Strobl 				slot->retune_count =
936aca38eabSMarius Strobl 				    1 << (slot->retune_count - 1);
937aca38eabSMarius Strobl 		}
938aca38eabSMarius Strobl 	}
939aca38eabSMarius Strobl 
940aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING
941aca38eabSMarius Strobl 
942aca38eabSMarius Strobl 	/* Determine supported VCCQ signaling levels. */
9430f34084fSMarius Strobl 	host_caps |= MMC_CAP_SIGNALING_330;
9440f34084fSMarius Strobl 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
945aca38eabSMarius Strobl 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
9460f34084fSMarius Strobl 	    MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 |
9470f34084fSMarius Strobl 	    MMC_CAP_MMC_HS400_180))
948aca38eabSMarius Strobl 		host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180;
949aca38eabSMarius Strobl 
950aca38eabSMarius Strobl 	/*
951aca38eabSMarius Strobl 	 * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the
952aca38eabSMarius Strobl 	 * default NULL implementation.  Disable 1.2 V support if it's the
953aca38eabSMarius Strobl 	 * generic SDHCI implementation.
954aca38eabSMarius Strobl 	 */
955aca38eabSMarius Strobl 	kobj_desc = &mmcbr_switch_vccq_desc;
956aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
957aca38eabSMarius Strobl 	    kobj_desc);
958aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
959aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180);
960aca38eabSMarius Strobl 	else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq)
961aca38eabSMarius Strobl 		host_caps &= ~MMC_CAP_SIGNALING_120;
962aca38eabSMarius Strobl 
963aca38eabSMarius Strobl 	/* Determine supported driver types (type B is always mandatory). */
964f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_A)
9650f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_A;
966f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_C)
9670f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_C;
968f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_D)
9690f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_D;
9700f34084fSMarius Strobl 	slot->host.caps = host_caps;
9710f34084fSMarius Strobl 
972831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
973831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
974831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
975d6b3aaf8SOleksandr Tymoshenko 
976d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
977831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
978d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
979831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
980a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE)
981a2832f9fSMarius Strobl 		slot->opt |= SDHCI_NON_REMOVABLE;
982831f5dcfSAlexander Motin 
983c3a0f75aSOleksandr Tymoshenko 	/*
984c3a0f75aSOleksandr Tymoshenko 	 * Use platform-provided transfer backend
985c3a0f75aSOleksandr Tymoshenko 	 * with PIO as a fallback mechanism
986c3a0f75aSOleksandr Tymoshenko 	 */
987c3a0f75aSOleksandr Tymoshenko 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
988c3a0f75aSOleksandr Tymoshenko 		slot->opt &= ~SDHCI_HAVE_DMA;
989c3a0f75aSOleksandr Tymoshenko 
9905b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
9910f34084fSMarius Strobl 		slot_printf(slot,
9927fcf4780SMarius Strobl 		    "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n",
993831f5dcfSAlexander Motin 		    slot->max_clk / 1000000,
994831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
9950f34084fSMarius Strobl 		    (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
9960f34084fSMarius Strobl 			((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
997831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
998831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
9997fcf4780SMarius Strobl 		    ((caps & SDHCI_CAN_VDD_180) &&
10007fcf4780SMarius Strobl 		    (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "",
10010f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "",
10020f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "",
1003aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "",
1004aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "",
1005aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "",
10067fcf4780SMarius Strobl 		    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO",
10077fcf4780SMarius Strobl 		    (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" :
10087fcf4780SMarius Strobl 		    (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" :
10097fcf4780SMarius Strobl 		    "removable");
10100f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 |
10110f34084fSMarius Strobl 		    MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE))
10120f34084fSMarius Strobl 			slot_printf(slot, "eMMC:%s%s%s%s\n",
10130f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "",
10140f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "",
10150f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "",
10160f34084fSMarius Strobl 			    ((host_caps &
10170f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ==
10180f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ?
10190f34084fSMarius Strobl 			    " HS400ES" : "");
10200f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
10210f34084fSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104))
10220f34084fSMarius Strobl 			slot_printf(slot, "UHS-I:%s%s%s%s%s\n",
10230f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "",
10240f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "",
10250f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "",
10260f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "",
10270f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : "");
1028aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_SUPPORTED)
1029aca38eabSMarius Strobl 			slot_printf(slot, "Re-tuning count %d secs, mode %d\n",
1030aca38eabSMarius Strobl 			    slot->retune_count, slot->retune_mode + 1);
1031831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1032831f5dcfSAlexander Motin 	}
1033831f5dcfSAlexander Motin 
1034ba6fc1c7SLuiz Otavio O Souza 	slot->timeout = 10;
1035ba6fc1c7SLuiz Otavio O Souza 	SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
1036ba6fc1c7SLuiz Otavio O Souza 	    SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
1037ba6fc1c7SLuiz Otavio O Souza 	    "timeout", CTLFLAG_RW, &slot->timeout, 0,
1038ba6fc1c7SLuiz Otavio O Souza 	    "Maximum timeout for SDHCI transfers (in secs)");
1039831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
1040639f59f0SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
1041639f59f0SIan Lepore 		sdhci_card_task, slot);
1042639f59f0SIan Lepore 	callout_init(&slot->card_poll_callout, 1);
1043e64f01a9SIan Lepore 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
1044aca38eabSMarius Strobl 	callout_init_mtx(&slot->retune_callout, &slot->mtx, 0);
1045ba6fc1c7SLuiz Otavio O Souza 
1046639f59f0SIan Lepore 	if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
1047639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
1048639f59f0SIan Lepore 		callout_reset(&slot->card_poll_callout,
1049639f59f0SIan Lepore 		    SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
1050639f59f0SIan Lepore 	}
1051639f59f0SIan Lepore 
1052aca38eabSMarius Strobl 	sdhci_init(slot);
1053aca38eabSMarius Strobl 
1054831f5dcfSAlexander Motin 	return (0);
1055831f5dcfSAlexander Motin }
1056831f5dcfSAlexander Motin 
1057d91f1a10SIlya Bakulin #ifndef MMCCAM
1058d6b3aaf8SOleksandr Tymoshenko void
1059d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
1060831f5dcfSAlexander Motin {
10617e6ccea3SMarius Strobl 
1062d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
1063d6b3aaf8SOleksandr Tymoshenko }
1064d91f1a10SIlya Bakulin #endif
1065831f5dcfSAlexander Motin 
1066d6b3aaf8SOleksandr Tymoshenko int
1067d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
1068d6b3aaf8SOleksandr Tymoshenko {
1069831f5dcfSAlexander Motin 	device_t d;
1070831f5dcfSAlexander Motin 
1071e64f01a9SIan Lepore 	callout_drain(&slot->timeout_callout);
1072639f59f0SIan Lepore 	callout_drain(&slot->card_poll_callout);
1073aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1074831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
1075639f59f0SIan Lepore 	taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
1076831f5dcfSAlexander Motin 
1077831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1078831f5dcfSAlexander Motin 	d = slot->dev;
1079831f5dcfSAlexander Motin 	slot->dev = NULL;
1080831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1081831f5dcfSAlexander Motin 	if (d != NULL)
1082d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
1083831f5dcfSAlexander Motin 
1084831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1085831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
1086831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1087831f5dcfSAlexander Motin 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
1088831f5dcfSAlexander Motin 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
1089831f5dcfSAlexander Motin 	bus_dma_tag_destroy(slot->dmatag);
1090aca38eabSMarius Strobl 	if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1091aca38eabSMarius Strobl 		free(slot->tune_req, M_DEVBUF);
1092aca38eabSMarius Strobl 		free(slot->tune_cmd, M_DEVBUF);
1093aca38eabSMarius Strobl 		free(slot->tune_data, M_DEVBUF);
1094aca38eabSMarius Strobl 	}
1095d6b3aaf8SOleksandr Tymoshenko 
1096831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
1097d6b3aaf8SOleksandr Tymoshenko 
1098831f5dcfSAlexander Motin 	return (0);
1099831f5dcfSAlexander Motin }
1100831f5dcfSAlexander Motin 
1101d6b3aaf8SOleksandr Tymoshenko int
1102d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
110392bf0e27SAlexander Motin {
11047e6ccea3SMarius Strobl 
1105aca38eabSMarius Strobl 	/*
1106aca38eabSMarius Strobl 	 * We expect the MMC layer to issue initial tuning after resume.
1107aca38eabSMarius Strobl 	 * Otherwise, we'd need to indicate re-tuning including circuit reset
1108aca38eabSMarius Strobl 	 * being required at least for re-tuning modes 1 and 2 ourselves.
1109aca38eabSMarius Strobl 	 */
1110aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1111aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1112aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1113d6b3aaf8SOleksandr Tymoshenko 	sdhci_reset(slot, SDHCI_RESET_ALL);
1114aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
111592bf0e27SAlexander Motin 
111692bf0e27SAlexander Motin 	return (0);
111792bf0e27SAlexander Motin }
111892bf0e27SAlexander Motin 
1119d6b3aaf8SOleksandr Tymoshenko int
1120d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
112192bf0e27SAlexander Motin {
11227e6ccea3SMarius Strobl 
1123aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1124d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
1125aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
112692bf0e27SAlexander Motin 
1127d6b3aaf8SOleksandr Tymoshenko 	return (0);
112892bf0e27SAlexander Motin }
112992bf0e27SAlexander Motin 
113057677a3aSOleksandr Tymoshenko uint32_t
1131b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot)
113257677a3aSOleksandr Tymoshenko {
11337e6ccea3SMarius Strobl 
113457677a3aSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
113557677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
113657677a3aSOleksandr Tymoshenko 	else
113757677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
113857677a3aSOleksandr Tymoshenko }
113957677a3aSOleksandr Tymoshenko 
11406e37fb2bSIan Lepore bool
1141b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot)
11426e37fb2bSIan Lepore {
11436e37fb2bSIan Lepore 
1144639f59f0SIan Lepore 	if (slot->opt & SDHCI_NON_REMOVABLE)
1145639f59f0SIan Lepore 		return true;
1146639f59f0SIan Lepore 
11476e37fb2bSIan Lepore 	return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
11486e37fb2bSIan Lepore }
11496e37fb2bSIan Lepore 
11500f34084fSMarius Strobl void
11510f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot)
11520f34084fSMarius Strobl {
11530f34084fSMarius Strobl 	struct mmc_ios *ios;
11540f34084fSMarius Strobl 	uint16_t hostctrl2;
11550f34084fSMarius Strobl 
11560f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
11570f34084fSMarius Strobl 		return;
11580f34084fSMarius Strobl 
1159aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
11600f34084fSMarius Strobl 	ios = &slot->host.ios;
11610f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
11620f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
11630f34084fSMarius Strobl 	hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK;
1164aca38eabSMarius Strobl 	if (ios->clock > SD_SDR50_MAX) {
11650f34084fSMarius Strobl 		if (ios->timing == bus_timing_mmc_hs400 ||
11660f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_hs400es)
11670f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_MMC_HS400;
1168aca38eabSMarius Strobl 		else
11690f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR104;
1170aca38eabSMarius Strobl 	}
11710f34084fSMarius Strobl 	else if (ios->clock > SD_SDR25_MAX)
11720f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR50;
11730f34084fSMarius Strobl 	else if (ios->clock > SD_SDR12_MAX) {
11740f34084fSMarius Strobl 		if (ios->timing == bus_timing_uhs_ddr50 ||
11750f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_ddr52)
11760f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_DDR50;
11770f34084fSMarius Strobl 		else
11780f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR25;
11790f34084fSMarius Strobl 	} else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY)
11800f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR12;
11810f34084fSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
11820f34084fSMarius Strobl 	sdhci_set_clock(slot, ios->clock);
11830f34084fSMarius Strobl }
11840f34084fSMarius Strobl 
1185d6b3aaf8SOleksandr Tymoshenko int
1186d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
1187831f5dcfSAlexander Motin {
1188831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1189831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
1190831f5dcfSAlexander Motin 
1191831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1192831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
1193831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
1194831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
1195831f5dcfSAlexander Motin 		sdhci_init(slot);
1196831f5dcfSAlexander Motin 	}
1197831f5dcfSAlexander Motin 	/* Configure the bus. */
1198831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
1199831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
12002d1731b8SIan Lepore 	if (ios->bus_width == bus_width_8) {
12012d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
1202831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
12032d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_4) {
12042d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
12052d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
12062d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_1) {
12072d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
12082d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
12092d1731b8SIan Lepore 	} else {
12102d1731b8SIan Lepore 		panic("Invalid bus width: %d", ios->bus_width);
12112d1731b8SIan Lepore 	}
12120f34084fSMarius Strobl 	if (ios->clock > SD_SDR12_MAX &&
1213bba987dcSIan Lepore 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
1214831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
1215831f5dcfSAlexander Motin 	else
1216831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
1217831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
12180f34084fSMarius Strobl 	SDHCI_SET_UHS_TIMING(brdev, slot);
1219831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
1220d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
1221831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1222831f5dcfSAlexander Motin 
1223831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1224831f5dcfSAlexander Motin 	return (0);
1225831f5dcfSAlexander Motin }
1226831f5dcfSAlexander Motin 
12270f34084fSMarius Strobl int
12280f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev)
12290f34084fSMarius Strobl {
12300f34084fSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
12310f34084fSMarius Strobl 	enum mmc_vccq vccq;
12320f34084fSMarius Strobl 	int err;
12330f34084fSMarius Strobl 	uint16_t hostctrl2;
12340f34084fSMarius Strobl 
12350f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
12360f34084fSMarius Strobl 		return (0);
12370f34084fSMarius Strobl 
12380f34084fSMarius Strobl 	err = 0;
12390f34084fSMarius Strobl 	vccq = slot->host.ios.vccq;
12400f34084fSMarius Strobl 	SDHCI_LOCK(slot);
12410f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
12420f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
12430f34084fSMarius Strobl 	switch (vccq) {
12440f34084fSMarius Strobl 	case vccq_330:
12450f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
12460f34084fSMarius Strobl 			goto done;
12470f34084fSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE;
12480f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
12490f34084fSMarius Strobl 		DELAY(5000);
12500f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
12510f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
12520f34084fSMarius Strobl 			goto done;
12530f34084fSMarius Strobl 		err = EAGAIN;
12540f34084fSMarius Strobl 		break;
12550f34084fSMarius Strobl 	case vccq_180:
12560f34084fSMarius Strobl 		if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) {
12570f34084fSMarius Strobl 			err = EINVAL;
12580f34084fSMarius Strobl 			goto done;
12590f34084fSMarius Strobl 		}
12600f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
12610f34084fSMarius Strobl 			goto done;
12620f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_S18_ENABLE;
12630f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
12640f34084fSMarius Strobl 		DELAY(5000);
12650f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
12660f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
12670f34084fSMarius Strobl 			goto done;
12680f34084fSMarius Strobl 		err = EAGAIN;
12690f34084fSMarius Strobl 		break;
12700f34084fSMarius Strobl 	default:
12710f34084fSMarius Strobl 		slot_printf(slot,
12720f34084fSMarius Strobl 		    "Attempt to set unsupported signaling voltage\n");
12730f34084fSMarius Strobl 		err = EINVAL;
12740f34084fSMarius Strobl 		break;
12750f34084fSMarius Strobl 	}
12760f34084fSMarius Strobl done:
12770f34084fSMarius Strobl 	sdhci_set_clock(slot, slot->host.ios.clock);
12780f34084fSMarius Strobl 	SDHCI_UNLOCK(slot);
12790f34084fSMarius Strobl 	return (err);
12800f34084fSMarius Strobl }
12810f34084fSMarius Strobl 
1282aca38eabSMarius Strobl int
1283aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400)
1284aca38eabSMarius Strobl {
1285aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1286aca38eabSMarius Strobl 	struct mmc_ios *ios = &slot->host.ios;
1287aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1288aca38eabSMarius Strobl 	struct mmc_data *tune_data;
1289aca38eabSMarius Strobl 	uint32_t opcode;
1290aca38eabSMarius Strobl 	int err;
1291aca38eabSMarius Strobl 
1292aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_SUPPORTED))
1293aca38eabSMarius Strobl 		return (0);
1294aca38eabSMarius Strobl 
1295aca38eabSMarius Strobl 	slot->retune_ticks = slot->retune_count * hz;
1296aca38eabSMarius Strobl 	opcode = MMC_SEND_TUNING_BLOCK;
1297aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1298aca38eabSMarius Strobl 	switch (ios->timing) {
1299aca38eabSMarius Strobl 	case bus_timing_mmc_hs400:
1300aca38eabSMarius Strobl 		slot_printf(slot, "HS400 must be tuned in HS200 mode\n");
1301aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1302aca38eabSMarius Strobl 		return (EINVAL);
1303aca38eabSMarius Strobl 	case bus_timing_mmc_hs200:
1304aca38eabSMarius Strobl 		/*
1305aca38eabSMarius Strobl 		 * In HS400 mode, controllers use the data strobe line to
1306aca38eabSMarius Strobl 		 * latch data from the devices so periodic re-tuning isn't
1307aca38eabSMarius Strobl 		 * expected to be required.
1308aca38eabSMarius Strobl 		 */
1309aca38eabSMarius Strobl 		if (hs400)
1310aca38eabSMarius Strobl 			slot->retune_ticks = 0;
1311aca38eabSMarius Strobl 		opcode = MMC_SEND_TUNING_BLOCK_HS200;
1312aca38eabSMarius Strobl 		break;
1313aca38eabSMarius Strobl 	case bus_timing_uhs_ddr50:
1314aca38eabSMarius Strobl 	case bus_timing_uhs_sdr104:
1315aca38eabSMarius Strobl 		break;
1316aca38eabSMarius Strobl 	case bus_timing_uhs_sdr50:
1317aca38eabSMarius Strobl 		if (slot->opt & SDHCI_SDR50_NEEDS_TUNING)
1318aca38eabSMarius Strobl 			break;
1319aca38eabSMarius Strobl 		/* FALLTHROUGH */
1320aca38eabSMarius Strobl 	default:
1321aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1322aca38eabSMarius Strobl 		return (0);
1323aca38eabSMarius Strobl 	}
1324aca38eabSMarius Strobl 
1325aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1326aca38eabSMarius Strobl 	memset(tune_cmd, 0, sizeof(*tune_cmd));
1327aca38eabSMarius Strobl 	tune_cmd->opcode = opcode;
1328aca38eabSMarius Strobl 	tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1329aca38eabSMarius Strobl 	tune_data = tune_cmd->data = slot->tune_data;
1330aca38eabSMarius Strobl 	memset(tune_data, 0, sizeof(*tune_data));
1331aca38eabSMarius Strobl 	tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
1332aca38eabSMarius Strobl 	    ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 :
1333aca38eabSMarius Strobl 	    MMC_TUNING_LEN;
1334aca38eabSMarius Strobl 	tune_data->flags = MMC_DATA_READ;
1335aca38eabSMarius Strobl 	tune_data->mrq = tune_cmd->mrq = slot->tune_req;
1336aca38eabSMarius Strobl 
1337aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1338aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, true);
1339aca38eabSMarius Strobl 	if (err == 0) {
1340aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_ENABLED;
1341aca38eabSMarius Strobl 		slot->intmask |= sdhci_tuning_intmask(slot);
1342*cc22204bSMarius Strobl 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
1343aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1344aca38eabSMarius Strobl 		if (slot->retune_ticks) {
1345aca38eabSMarius Strobl 			callout_reset(&slot->retune_callout, slot->retune_ticks,
1346aca38eabSMarius Strobl 			    sdhci_retune, slot);
1347aca38eabSMarius Strobl 		}
1348aca38eabSMarius Strobl 	}
1349aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1350aca38eabSMarius Strobl 	return (err);
1351aca38eabSMarius Strobl }
1352aca38eabSMarius Strobl 
1353aca38eabSMarius Strobl int
1354aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset)
1355aca38eabSMarius Strobl {
1356aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1357aca38eabSMarius Strobl 	int err;
1358aca38eabSMarius Strobl 
1359aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_ENABLED))
1360aca38eabSMarius Strobl 		return (0);
1361aca38eabSMarius Strobl 
1362aca38eabSMarius Strobl 	/* HS400 must be tuned in HS200 mode. */
1363aca38eabSMarius Strobl 	if (slot->host.ios.timing == bus_timing_mmc_hs400)
1364aca38eabSMarius Strobl 		return (EINVAL);
1365aca38eabSMarius Strobl 
1366aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1367aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, reset);
1368aca38eabSMarius Strobl 	/*
1369aca38eabSMarius Strobl 	 * There are two ways sdhci_exec_tuning() can fail:
1370aca38eabSMarius Strobl 	 * EBUSY should not actually happen when requests are only issued
1371aca38eabSMarius Strobl 	 *	 with the host properly acquired, and
1372aca38eabSMarius Strobl 	 * EIO   re-tuning failed (but it did work initially).
1373aca38eabSMarius Strobl 	 *
1374aca38eabSMarius Strobl 	 * In both cases, we should retry at later point if periodic re-tuning
1375aca38eabSMarius Strobl 	 * is enabled.  Note that due to slot->retune_req not being cleared in
1376aca38eabSMarius Strobl 	 * these failure cases, the MMC layer should trigger another attempt at
1377aca38eabSMarius Strobl 	 * re-tuning with the next request anyway, though.
1378aca38eabSMarius Strobl 	 */
1379aca38eabSMarius Strobl 	if (slot->retune_ticks) {
1380aca38eabSMarius Strobl 		callout_reset(&slot->retune_callout, slot->retune_ticks,
1381aca38eabSMarius Strobl 		    sdhci_retune, slot);
1382aca38eabSMarius Strobl 	}
1383aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1384aca38eabSMarius Strobl 	return (err);
1385aca38eabSMarius Strobl }
1386aca38eabSMarius Strobl 
1387aca38eabSMarius Strobl static int
1388aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset)
1389aca38eabSMarius Strobl {
1390aca38eabSMarius Strobl 	struct mmc_request *tune_req;
1391aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1392aca38eabSMarius Strobl 	int i;
1393aca38eabSMarius Strobl 	uint32_t intmask;
1394aca38eabSMarius Strobl 	uint16_t hostctrl2;
1395aca38eabSMarius Strobl 	u_char opt;
1396aca38eabSMarius Strobl 
1397aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
1398aca38eabSMarius Strobl 	if (slot->req != NULL)
1399aca38eabSMarius Strobl 		return (EBUSY);
1400aca38eabSMarius Strobl 
1401aca38eabSMarius Strobl 	/* Tuning doesn't work with DMA enabled. */
1402aca38eabSMarius Strobl 	opt = slot->opt;
1403aca38eabSMarius Strobl 	slot->opt = opt & ~SDHCI_HAVE_DMA;
1404aca38eabSMarius Strobl 
1405aca38eabSMarius Strobl 	/*
1406aca38eabSMarius Strobl 	 * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only
1407aca38eabSMarius Strobl 	 * kind of interrupt we receive in response to a tuning request.
1408aca38eabSMarius Strobl 	 */
1409aca38eabSMarius Strobl 	intmask = slot->intmask;
1410aca38eabSMarius Strobl 	slot->intmask = SDHCI_INT_DATA_AVAIL;
1411*cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL);
1412aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL);
1413aca38eabSMarius Strobl 
1414aca38eabSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1415aca38eabSMarius Strobl 	if (reset)
1416aca38eabSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK;
1417aca38eabSMarius Strobl 	else
1418aca38eabSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK;
1419aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING);
1420aca38eabSMarius Strobl 
1421aca38eabSMarius Strobl 	tune_req = slot->tune_req;
1422aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1423aca38eabSMarius Strobl 	for (i = 0; i < MMC_TUNING_MAX; i++) {
1424aca38eabSMarius Strobl 		memset(tune_req, 0, sizeof(*tune_req));
1425aca38eabSMarius Strobl 		tune_req->cmd = tune_cmd;
1426aca38eabSMarius Strobl 		tune_req->done = sdhci_req_wakeup;
1427aca38eabSMarius Strobl 		tune_req->done_data = slot;
1428aca38eabSMarius Strobl 		slot->req = tune_req;
1429aca38eabSMarius Strobl 		slot->flags = 0;
1430aca38eabSMarius Strobl 		sdhci_start(slot);
1431aca38eabSMarius Strobl 		while (!(tune_req->flags & MMC_REQ_DONE))
1432aca38eabSMarius Strobl 			msleep(tune_req, &slot->mtx, 0, "sdhciet", 0);
1433aca38eabSMarius Strobl 		if (!(tune_req->flags & MMC_TUNE_DONE))
1434aca38eabSMarius Strobl 			break;
1435aca38eabSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1436aca38eabSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING))
1437aca38eabSMarius Strobl 			break;
1438aca38eabSMarius Strobl 		if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK)
1439aca38eabSMarius Strobl 			DELAY(1000);
1440aca38eabSMarius Strobl 	}
1441aca38eabSMarius Strobl 
1442aca38eabSMarius Strobl 	slot->opt = opt;
1443aca38eabSMarius Strobl 	slot->intmask = intmask;
1444*cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, intmask);
1445aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, intmask);
1446aca38eabSMarius Strobl 
1447aca38eabSMarius Strobl 	if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING |
1448aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) {
1449aca38eabSMarius Strobl 		slot->retune_req = 0;
1450aca38eabSMarius Strobl 		return (0);
1451aca38eabSMarius Strobl 	}
1452aca38eabSMarius Strobl 
1453aca38eabSMarius Strobl 	slot_printf(slot, "Tuning failed, using fixed sampling clock\n");
1454aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING |
1455aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK));
1456aca38eabSMarius Strobl 	sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1457aca38eabSMarius Strobl 	return (EIO);
1458aca38eabSMarius Strobl }
1459aca38eabSMarius Strobl 
1460aca38eabSMarius Strobl static void
1461aca38eabSMarius Strobl sdhci_retune(void *arg)
1462aca38eabSMarius Strobl {
1463aca38eabSMarius Strobl 	struct sdhci_slot *slot = arg;
1464aca38eabSMarius Strobl 
1465aca38eabSMarius Strobl 	slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
1466aca38eabSMarius Strobl }
1467aca38eabSMarius Strobl 
1468a94a63f0SWarner Losh #ifdef MMCCAM
1469a94a63f0SWarner Losh static void
1470a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot)
1471a94a63f0SWarner Losh {
1472a94a63f0SWarner Losh         union ccb *ccb;
147315c440e1SWarner Losh 
1474aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
147515c440e1SWarner Losh 		slot_printf(slot, "%s\n", __func__);
1476a94a63f0SWarner Losh 	if (slot->ccb != NULL && slot->curcmd != NULL) {
1477a94a63f0SWarner Losh 		callout_stop(&slot->timeout_callout);
1478a94a63f0SWarner Losh                 ccb = slot->ccb;
1479a94a63f0SWarner Losh                 slot->ccb = NULL;
1480a94a63f0SWarner Losh 		slot->curcmd = NULL;
1481a94a63f0SWarner Losh 
1482a94a63f0SWarner Losh                 /* Tell CAM the request is finished */
1483a94a63f0SWarner Losh                 struct ccb_mmcio *mmcio;
1484a94a63f0SWarner Losh                 mmcio = &ccb->mmcio;
1485a94a63f0SWarner Losh 
1486a94a63f0SWarner Losh                 ccb->ccb_h.status =
1487a94a63f0SWarner Losh                         (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR);
1488a94a63f0SWarner Losh                 xpt_done(ccb);
1489a94a63f0SWarner Losh 	}
1490a94a63f0SWarner Losh }
1491a94a63f0SWarner Losh #else
1492831f5dcfSAlexander Motin static void
1493e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot)
1494e64f01a9SIan Lepore {
1495e64f01a9SIan Lepore 	struct mmc_request *req;
1496e64f01a9SIan Lepore 
1497e64f01a9SIan Lepore 	if (slot->req != NULL && slot->curcmd != NULL) {
1498e64f01a9SIan Lepore 		callout_stop(&slot->timeout_callout);
1499e64f01a9SIan Lepore 		req = slot->req;
1500e64f01a9SIan Lepore 		slot->req = NULL;
1501e64f01a9SIan Lepore 		slot->curcmd = NULL;
1502e64f01a9SIan Lepore 		req->done(req);
1503e64f01a9SIan Lepore 	}
1504e64f01a9SIan Lepore }
1505a94a63f0SWarner Losh #endif
1506e64f01a9SIan Lepore 
1507e64f01a9SIan Lepore static void
1508aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req)
1509aca38eabSMarius Strobl {
1510aca38eabSMarius Strobl 	struct sdhci_slot *slot;
1511aca38eabSMarius Strobl 
1512aca38eabSMarius Strobl 	slot = req->done_data;
1513aca38eabSMarius Strobl 	req->flags |= MMC_REQ_DONE;
1514aca38eabSMarius Strobl 	wakeup(req);
1515aca38eabSMarius Strobl }
1516aca38eabSMarius Strobl 
1517aca38eabSMarius Strobl static void
1518e64f01a9SIan Lepore sdhci_timeout(void *arg)
1519e64f01a9SIan Lepore {
1520e64f01a9SIan Lepore 	struct sdhci_slot *slot = arg;
1521e64f01a9SIan Lepore 
1522e64f01a9SIan Lepore 	if (slot->curcmd != NULL) {
15237e586643SIan Lepore 		slot_printf(slot, "Controller timeout\n");
15247e586643SIan Lepore 		sdhci_dumpregs(slot);
1525a6873fd1SIan Lepore 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1526e64f01a9SIan Lepore 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1527e64f01a9SIan Lepore 		sdhci_req_done(slot);
15287e586643SIan Lepore 	} else {
15297e586643SIan Lepore 		slot_printf(slot, "Spurious timeout - no active command\n");
1530e64f01a9SIan Lepore 	}
1531e64f01a9SIan Lepore }
1532e64f01a9SIan Lepore 
1533e64f01a9SIan Lepore static void
1534b440e965SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, struct mmc_data *data)
1535831f5dcfSAlexander Motin {
1536831f5dcfSAlexander Motin 	uint16_t mode;
1537831f5dcfSAlexander Motin 
1538831f5dcfSAlexander Motin 	if (data == NULL)
1539831f5dcfSAlexander Motin 		return;
1540831f5dcfSAlexander Motin 
1541831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
1542831f5dcfSAlexander Motin 	if (data->len > 512)
1543831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
1544831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
1545831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
1546a94a63f0SWarner Losh #ifdef MMCCAM
1547a94a63f0SWarner Losh 	struct ccb_mmcio *mmcio;
1548a94a63f0SWarner Losh 	mmcio = &slot->ccb->mmcio;
1549a94a63f0SWarner Losh 	if (mmcio->stop.opcode == MMC_STOP_TRANSMISSION
1550a94a63f0SWarner Losh 	    && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))
1551a94a63f0SWarner Losh 		mode |= SDHCI_TRNS_ACMD12;
1552a94a63f0SWarner Losh #else
1553915780d7SLuiz Otavio O Souza 	if (slot->req->stop && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))
1554831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_ACMD12;
1555a94a63f0SWarner Losh #endif
1556831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
1557831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
1558831f5dcfSAlexander Motin 
1559831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
1560831f5dcfSAlexander Motin }
1561831f5dcfSAlexander Motin 
1562831f5dcfSAlexander Motin static void
1563831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
1564831f5dcfSAlexander Motin {
1565831f5dcfSAlexander Motin 	int flags, timeout;
156690993663SIan Lepore 	uint32_t mask;
1567831f5dcfSAlexander Motin 
1568831f5dcfSAlexander Motin 	slot->curcmd = cmd;
1569831f5dcfSAlexander Motin 	slot->cmd_done = 0;
1570831f5dcfSAlexander Motin 
1571831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
1572831f5dcfSAlexander Motin 
1573831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
1574831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1575831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
1576831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1577e64f01a9SIan Lepore 		sdhci_req_done(slot);
1578831f5dcfSAlexander Motin 		return;
1579831f5dcfSAlexander Motin 	}
1580831f5dcfSAlexander Motin 
1581b440e965SMarius Strobl 	/*
1582b440e965SMarius Strobl 	 * Do not issue command if there is no card, clock or power.
1583b440e965SMarius Strobl 	 * Controller will not detect timeout without clock active.
1584b440e965SMarius Strobl 	 */
15856e37fb2bSIan Lepore 	if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
1586d8208d9eSAlexander Motin 	    slot->power == 0 ||
1587d8208d9eSAlexander Motin 	    slot->clock == 0) {
1588a94a63f0SWarner Losh 		slot_printf(slot,
1589a94a63f0SWarner Losh 			    "Cannot issue a command (power=%d clock=%d)",
1590a94a63f0SWarner Losh 			    slot->power, slot->clock);
1591831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1592e64f01a9SIan Lepore 		sdhci_req_done(slot);
1593831f5dcfSAlexander Motin 		return;
1594831f5dcfSAlexander Motin 	}
1595831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
1596831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
1597831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
1598a94a63f0SWarner Losh 	if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY))
1599831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
1600aca38eabSMarius Strobl 	/*
1601aca38eabSMarius Strobl 	 * We shouldn't wait for DAT for stop commands or CMD19/CMD21.  Note
1602aca38eabSMarius Strobl 	 * that these latter are also special in that SDHCI_CMD_DATA should
1603aca38eabSMarius Strobl 	 * be set below but no actual data is ever read from the controller.
1604aca38eabSMarius Strobl 	*/
1605a94a63f0SWarner Losh #ifdef MMCCAM
1606aca38eabSMarius Strobl 	if (cmd == &slot->ccb->mmcio.stop ||
1607a94a63f0SWarner Losh #else
1608aca38eabSMarius Strobl 	if (cmd == slot->req->stop ||
1609a94a63f0SWarner Losh #endif
1610aca38eabSMarius Strobl 	    __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1611aca38eabSMarius Strobl 	    cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))
1612aca38eabSMarius Strobl 		mask &= ~SDHCI_DAT_INHIBIT;
16138775ab45SIan Lepore 	/*
16148775ab45SIan Lepore 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
16158775ab45SIan Lepore 	 *  here at all, but when writing a crash dump we may be bypassing the
16168775ab45SIan Lepore 	 *  host platform's interrupt handler, and in some cases that handler
16178775ab45SIan Lepore 	 *  may be working around hardware quirks such as not respecting r1b
16188775ab45SIan Lepore 	 *  busy indications.  In those cases, this wait-loop serves the purpose
16198775ab45SIan Lepore 	 *  of waiting for the prior command and data transfers to be done, and
16208775ab45SIan Lepore 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
16218775ab45SIan Lepore 	 *  (It's usually more like 20-30ms in the real world.)
16228775ab45SIan Lepore 	 */
16238775ab45SIan Lepore 	timeout = 250;
162490993663SIan Lepore 	while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
1625831f5dcfSAlexander Motin 		if (timeout == 0) {
1626831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
1627831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
1628831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
1629831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
1630e64f01a9SIan Lepore 			sdhci_req_done(slot);
1631831f5dcfSAlexander Motin 			return;
1632831f5dcfSAlexander Motin 		}
1633831f5dcfSAlexander Motin 		timeout--;
1634831f5dcfSAlexander Motin 		DELAY(1000);
1635831f5dcfSAlexander Motin 	}
1636831f5dcfSAlexander Motin 
1637831f5dcfSAlexander Motin 	/* Prepare command flags. */
1638831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
1639831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
1640831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
1641831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
1642831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
1643831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
1644831f5dcfSAlexander Motin 	else
1645831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
1646831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
1647831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
1648831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
1649831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
1650a94a63f0SWarner Losh 	if (cmd->data != NULL)
1651831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
1652831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
1653831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
1654831f5dcfSAlexander Motin 	/* Prepare data. */
1655831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
1656831f5dcfSAlexander Motin 	/*
1657831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
1658831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
1659831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
1660831f5dcfSAlexander Motin 	 */
1661831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
1662831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1663831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
1664831f5dcfSAlexander Motin 	}
1665831f5dcfSAlexander Motin 	/* Set command argument. */
1666831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
1667831f5dcfSAlexander Motin 	/* Set data transfer mode. */
1668831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
1669aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1670a94a63f0SWarner Losh 		slot_printf(slot, "Starting command!\n");
1671831f5dcfSAlexander Motin 	/* Start command. */
1672d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
1673a6873fd1SIan Lepore 	/* Start timeout callout. */
1674ba6fc1c7SLuiz Otavio O Souza 	callout_reset(&slot->timeout_callout, slot->timeout * hz,
1675ba6fc1c7SLuiz Otavio O Souza 	    sdhci_timeout, slot);
1676831f5dcfSAlexander Motin }
1677831f5dcfSAlexander Motin 
1678831f5dcfSAlexander Motin static void
1679831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
1680831f5dcfSAlexander Motin {
1681831f5dcfSAlexander Motin 	int i;
16821bacf3beSMarius Strobl 	uint32_t val;
16831bacf3beSMarius Strobl 	uint8_t extra;
1684831f5dcfSAlexander Motin 
1685aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1686a94a63f0SWarner Losh 		slot_printf(slot, "%s: called, err %d flags %d\n",
1687a94a63f0SWarner Losh 		    __func__, slot->curcmd->error, slot->curcmd->flags);
1688831f5dcfSAlexander Motin 	slot->cmd_done = 1;
168972dec079SMarius Strobl 	/*
169072dec079SMarius Strobl 	 * Interrupt aggregation: Restore command interrupt.
1691831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
169272dec079SMarius Strobl 	 * happened first.
169372dec079SMarius Strobl 	 */
1694aca38eabSMarius Strobl 	if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK &&
1695aca38eabSMarius Strobl 	    slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200))
1696aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |=
1697aca38eabSMarius Strobl 		    SDHCI_INT_RESPONSE);
1698831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
1699831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1700aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
1701aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1702831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1703831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1704831f5dcfSAlexander Motin 		sdhci_start(slot);
1705831f5dcfSAlexander Motin 		return;
1706831f5dcfSAlexander Motin 	}
1707831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
1708831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1709831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
1710831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
17111bacf3beSMarius Strobl 			extra = 0;
1712831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
17131bacf3beSMarius Strobl 				val = RD4(slot, SDHCI_RESPONSE + i * 4);
17141bacf3beSMarius Strobl 				if (slot->quirks &
17151bacf3beSMarius Strobl 				    SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1716677ee494SIan Lepore 					slot->curcmd->resp[3 - i] = val;
1717677ee494SIan Lepore 				else {
1718677ee494SIan Lepore 					slot->curcmd->resp[3 - i] =
1719677ee494SIan Lepore 					    (val << 8) | extra;
1720831f5dcfSAlexander Motin 					extra = val >> 24;
1721831f5dcfSAlexander Motin 				}
1722677ee494SIan Lepore 			}
1723831f5dcfSAlexander Motin 		} else
1724831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1725831f5dcfSAlexander Motin 	}
1726aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1727a94a63f0SWarner Losh 		printf("Resp: %02x %02x %02x %02x\n",
1728a94a63f0SWarner Losh 		    slot->curcmd->resp[0], slot->curcmd->resp[1],
1729a94a63f0SWarner Losh 		    slot->curcmd->resp[2], slot->curcmd->resp[3]);
1730a94a63f0SWarner Losh 
1731831f5dcfSAlexander Motin 	/* If data ready - finish. */
1732831f5dcfSAlexander Motin 	if (slot->data_done)
1733831f5dcfSAlexander Motin 		sdhci_start(slot);
1734831f5dcfSAlexander Motin }
1735831f5dcfSAlexander Motin 
1736831f5dcfSAlexander Motin static void
1737831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data)
1738831f5dcfSAlexander Motin {
1739831f5dcfSAlexander Motin 	uint32_t target_timeout, current_timeout;
1740831f5dcfSAlexander Motin 	uint8_t div;
1741831f5dcfSAlexander Motin 
1742831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1743831f5dcfSAlexander Motin 		slot->data_done = 1;
1744831f5dcfSAlexander Motin 		return;
1745831f5dcfSAlexander Motin 	}
1746831f5dcfSAlexander Motin 
1747831f5dcfSAlexander Motin 	slot->data_done = 0;
1748831f5dcfSAlexander Motin 
1749831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
1750831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
1751ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1752ceb9e9f7SIan Lepore 		div = 0xE;
1753ceb9e9f7SIan Lepore 	} else {
1754831f5dcfSAlexander Motin 		target_timeout = 1000000;
1755831f5dcfSAlexander Motin 		div = 0;
1756831f5dcfSAlexander Motin 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1757ceb9e9f7SIan Lepore 		while (current_timeout < target_timeout && div < 0xE) {
1758ceb9e9f7SIan Lepore 			++div;
1759831f5dcfSAlexander Motin 			current_timeout <<= 1;
1760831f5dcfSAlexander Motin 		}
1761831f5dcfSAlexander Motin 		/* Compensate for an off-by-one error in the CaFe chip.*/
1762ceb9e9f7SIan Lepore 		if (div < 0xE &&
1763ceb9e9f7SIan Lepore 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1764ceb9e9f7SIan Lepore 			++div;
1765831f5dcfSAlexander Motin 		}
1766ceb9e9f7SIan Lepore 	}
1767831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1768831f5dcfSAlexander Motin 
1769831f5dcfSAlexander Motin 	if (data == NULL)
1770831f5dcfSAlexander Motin 		return;
1771831f5dcfSAlexander Motin 
1772831f5dcfSAlexander Motin 	/* Use DMA if possible. */
1773831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
1774831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
1775831f5dcfSAlexander Motin 	/* If data is small, broken DMA may return zeroes instead of data, */
1776d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1777831f5dcfSAlexander Motin 	    (data->len <= 512))
1778831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1779831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
1780d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1781831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
1782831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1783831f5dcfSAlexander Motin 	/* Load DMA buffer. */
1784831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
1785831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
1786ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1787ecc2d997SRui Paulo 			    BUS_DMASYNC_PREREAD);
1788831f5dcfSAlexander Motin 		else {
1789831f5dcfSAlexander Motin 			memcpy(slot->dmamem, data->data,
1790ecc2d997SRui Paulo 			    (data->len < DMA_BLOCK_SIZE) ?
1791ecc2d997SRui Paulo 			    data->len : DMA_BLOCK_SIZE);
1792ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1793ecc2d997SRui Paulo 			    BUS_DMASYNC_PREWRITE);
1794831f5dcfSAlexander Motin 		}
1795831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1796831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1797831f5dcfSAlexander Motin 		 * for the last page and unmask else. */
1798831f5dcfSAlexander Motin 		if (data->len == DMA_BLOCK_SIZE)
1799831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1800831f5dcfSAlexander Motin 		else
1801831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
1802831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1803831f5dcfSAlexander Motin 	}
1804831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
1805831f5dcfSAlexander Motin 	slot->offset = 0;
1806831f5dcfSAlexander Motin 	/* Set block size and request IRQ on 4K border. */
18071bacf3beSMarius Strobl 	WR2(slot, SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(DMA_BOUNDARY,
18081bacf3beSMarius Strobl 	    (data->len < 512) ? data->len : 512));
1809831f5dcfSAlexander Motin 	/* Set block count. */
1810831f5dcfSAlexander Motin 	WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
1811a94a63f0SWarner Losh 
1812aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
181315c440e1SWarner Losh 		slot_printf(slot, "Block size: %02x, count %lu\n",
181415c440e1SWarner Losh 		    (unsigned int)SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512) ? data->len : 512),
1815a94a63f0SWarner Losh 		    (unsigned long)(data->len + 511) / 512);
1816831f5dcfSAlexander Motin }
1817831f5dcfSAlexander Motin 
1818c3a0f75aSOleksandr Tymoshenko void
1819831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
1820831f5dcfSAlexander Motin {
1821831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
18227e6ccea3SMarius Strobl 	size_t left;
1823831f5dcfSAlexander Motin 
1824831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
1825ecc2d997SRui Paulo 	 * Auxiliary restore point for the case when data interrupt
1826831f5dcfSAlexander Motin 	 * happened first. */
1827831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
1828831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1829831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
1830831f5dcfSAlexander Motin 	}
1831831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
1832915780d7SLuiz Otavio O Souza 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) &&
1833915780d7SLuiz Otavio O Souza 	    slot->curcmd->data != NULL) {
1834831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
18357e6ccea3SMarius Strobl 			left = data->len - slot->offset;
1836ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1837ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTREAD);
1838831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1839831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
1840831f5dcfSAlexander Motin 		} else
1841ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1842ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTWRITE);
1843831f5dcfSAlexander Motin 	}
1844a98788edSIan Lepore 	slot->data_done = 1;
1845831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
1846831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1847aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
1848aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1849831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1850831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1851831f5dcfSAlexander Motin 		sdhci_start(slot);
1852831f5dcfSAlexander Motin 		return;
1853831f5dcfSAlexander Motin 	}
1854831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
1855831f5dcfSAlexander Motin 	if (slot->cmd_done)
1856831f5dcfSAlexander Motin 		sdhci_start(slot);
1857831f5dcfSAlexander Motin }
1858831f5dcfSAlexander Motin 
1859a94a63f0SWarner Losh #ifdef MMCCAM
1860a94a63f0SWarner Losh static void
1861a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot)
1862a94a63f0SWarner Losh {
1863a94a63f0SWarner Losh         union ccb *ccb;
1864a94a63f0SWarner Losh 
1865a94a63f0SWarner Losh 	ccb = slot->ccb;
1866a94a63f0SWarner Losh 	if (ccb == NULL)
1867a94a63f0SWarner Losh 		return;
1868a94a63f0SWarner Losh 
1869a94a63f0SWarner Losh         struct ccb_mmcio *mmcio;
1870a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
1871a94a63f0SWarner Losh 
1872a94a63f0SWarner Losh 	if (!(slot->flags & CMD_STARTED)) {
1873a94a63f0SWarner Losh 		slot->flags |= CMD_STARTED;
1874a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->cmd);
1875a94a63f0SWarner Losh 		return;
1876a94a63f0SWarner Losh 	}
1877a94a63f0SWarner Losh 
1878a94a63f0SWarner Losh 	/*
1879a94a63f0SWarner Losh 	 * Old stack doesn't use this!
1880a94a63f0SWarner Losh 	 * Enabling this code causes significant performance degradation
1881a94a63f0SWarner Losh 	 * and IRQ storms on BBB, Wandboard behaves fine.
1882a94a63f0SWarner Losh 	 * Not using this code does no harm...
1883a94a63f0SWarner Losh 	if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) {
1884a94a63f0SWarner Losh 		slot->flags |= STOP_STARTED;
1885a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->stop);
1886a94a63f0SWarner Losh 		return;
1887a94a63f0SWarner Losh 	}
1888a94a63f0SWarner Losh 	*/
1889aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1890a94a63f0SWarner Losh 		slot_printf(slot, "result: %d\n", mmcio->cmd.error);
1891a94a63f0SWarner Losh 	if (mmcio->cmd.error == 0 &&
1892a94a63f0SWarner Losh 	    (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
1893a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD);
1894a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_DATA);
1895a94a63f0SWarner Losh 	}
1896a94a63f0SWarner Losh 
1897a94a63f0SWarner Losh 	sdhci_req_done(slot);
1898a94a63f0SWarner Losh }
1899a94a63f0SWarner Losh #else
1900831f5dcfSAlexander Motin static void
1901831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
1902831f5dcfSAlexander Motin {
1903831f5dcfSAlexander Motin 	struct mmc_request *req;
1904831f5dcfSAlexander Motin 
1905831f5dcfSAlexander Motin 	req = slot->req;
1906831f5dcfSAlexander Motin 	if (req == NULL)
1907831f5dcfSAlexander Motin 		return;
1908831f5dcfSAlexander Motin 
1909831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
1910831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
1911831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
1912831f5dcfSAlexander Motin 		return;
1913831f5dcfSAlexander Motin 	}
1914915780d7SLuiz Otavio O Souza 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) &&
1915915780d7SLuiz Otavio O Souza 	    !(slot->flags & STOP_STARTED) && req->stop) {
1916831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
1917831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
1918831f5dcfSAlexander Motin 		return;
1919831f5dcfSAlexander Motin 	}
1920aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
19215b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
19225b69a497SAlexander Motin 	if (!req->cmd->error &&
1923915780d7SLuiz Otavio O Souza 	    ((slot->curcmd == req->stop &&
1924915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) ||
1925915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
1926831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1927831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1928831f5dcfSAlexander Motin 	}
1929831f5dcfSAlexander Motin 
1930e64f01a9SIan Lepore 	sdhci_req_done(slot);
1931831f5dcfSAlexander Motin }
1932a94a63f0SWarner Losh #endif
1933831f5dcfSAlexander Motin 
1934d6b3aaf8SOleksandr Tymoshenko int
1935b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev,
1936b440e965SMarius Strobl     struct mmc_request *req)
1937831f5dcfSAlexander Motin {
1938831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1939831f5dcfSAlexander Motin 
1940831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1941831f5dcfSAlexander Motin 	if (slot->req != NULL) {
1942831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1943831f5dcfSAlexander Motin 		return (EBUSY);
1944831f5dcfSAlexander Motin 	}
1945aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
19461bacf3beSMarius Strobl 		slot_printf(slot,
19471bacf3beSMarius Strobl 		    "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1948831f5dcfSAlexander Motin 		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
19495b69a497SAlexander Motin 		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
19505b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
19515b69a497SAlexander Motin 	}
1952831f5dcfSAlexander Motin 	slot->req = req;
1953831f5dcfSAlexander Motin 	slot->flags = 0;
1954831f5dcfSAlexander Motin 	sdhci_start(slot);
1955831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1956bea2dca2SAlexander Motin 	if (dumping) {
1957bea2dca2SAlexander Motin 		while (slot->req != NULL) {
1958d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
1959bea2dca2SAlexander Motin 			DELAY(10);
1960bea2dca2SAlexander Motin 		}
1961bea2dca2SAlexander Motin 	}
1962831f5dcfSAlexander Motin 	return (0);
1963831f5dcfSAlexander Motin }
1964831f5dcfSAlexander Motin 
1965d6b3aaf8SOleksandr Tymoshenko int
1966b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev)
1967831f5dcfSAlexander Motin {
1968831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1969831f5dcfSAlexander Motin 	uint32_t val;
1970831f5dcfSAlexander Motin 
1971831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1972831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
1973831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1974831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
1975831f5dcfSAlexander Motin }
1976831f5dcfSAlexander Motin 
1977d6b3aaf8SOleksandr Tymoshenko int
1978b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev)
1979831f5dcfSAlexander Motin {
1980831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1981831f5dcfSAlexander Motin 	int err = 0;
1982831f5dcfSAlexander Motin 
1983831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1984831f5dcfSAlexander Motin 	while (slot->bus_busy)
1985d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
1986831f5dcfSAlexander Motin 	slot->bus_busy++;
1987831f5dcfSAlexander Motin 	/* Activate led. */
1988831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
1989831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1990831f5dcfSAlexander Motin 	return (err);
1991831f5dcfSAlexander Motin }
1992831f5dcfSAlexander Motin 
1993d6b3aaf8SOleksandr Tymoshenko int
1994b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev)
1995831f5dcfSAlexander Motin {
1996831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1997831f5dcfSAlexander Motin 
1998831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1999831f5dcfSAlexander Motin 	/* Deactivate led. */
2000831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
2001831f5dcfSAlexander Motin 	slot->bus_busy--;
2002831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2003d493985aSAlexander Motin 	wakeup(slot);
2004831f5dcfSAlexander Motin 	return (0);
2005831f5dcfSAlexander Motin }
2006831f5dcfSAlexander Motin 
2007831f5dcfSAlexander Motin static void
2008831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
2009831f5dcfSAlexander Motin {
2010831f5dcfSAlexander Motin 
2011831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2012831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
2013831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2014831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2015831f5dcfSAlexander Motin 		return;
2016831f5dcfSAlexander Motin 	}
2017831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
2018831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2019831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
2020831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2021831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
2022831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
2023831f5dcfSAlexander Motin 
2024831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
2025831f5dcfSAlexander Motin }
2026831f5dcfSAlexander Motin 
2027831f5dcfSAlexander Motin static void
2028831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
2029831f5dcfSAlexander Motin {
20301bacf3beSMarius Strobl 	struct mmc_data *data;
203115c440e1SWarner Losh 	size_t left;
2032831f5dcfSAlexander Motin 
2033831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2034831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2035831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2036831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2037831f5dcfSAlexander Motin 		return;
2038831f5dcfSAlexander Motin 	}
2039831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2040831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
2041831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2042831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
2043831f5dcfSAlexander Motin 		    intmask);
2044831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2045831f5dcfSAlexander Motin 		return;
2046831f5dcfSAlexander Motin 	}
2047831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
2048831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2049acbaa69fSOleksandr Tymoshenko 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
2050831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2051831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2052831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
2053831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
2054831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2055831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
2056831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2057831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
2058831f5dcfSAlexander Motin 	}
2059831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
2060831f5dcfSAlexander Motin 		/* No need to continue after any error. */
2061a98788edSIan Lepore 		goto done;
2062831f5dcfSAlexander Motin 	}
2063831f5dcfSAlexander Motin 
2064aca38eabSMarius Strobl 	/* Handle tuning completion interrupt. */
2065aca38eabSMarius Strobl 	if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) &&
2066aca38eabSMarius Strobl 	    (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK ||
2067aca38eabSMarius Strobl 	    slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) {
2068aca38eabSMarius Strobl 		slot->req->flags |= MMC_TUNE_DONE;
2069aca38eabSMarius Strobl 		sdhci_finish_command(slot);
2070aca38eabSMarius Strobl 		sdhci_finish_data(slot);
2071aca38eabSMarius Strobl 		return;
2072aca38eabSMarius Strobl 	}
2073831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
2074c3a0f75aSOleksandr Tymoshenko 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
2075c3a0f75aSOleksandr Tymoshenko 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
2076c3a0f75aSOleksandr Tymoshenko 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
20771bacf3beSMarius Strobl 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot,
20781bacf3beSMarius Strobl 			    &intmask);
2079c3a0f75aSOleksandr Tymoshenko 			slot->flags |= PLATFORM_DATA_STARTED;
2080c3a0f75aSOleksandr Tymoshenko 		} else
2081831f5dcfSAlexander Motin 			sdhci_transfer_pio(slot);
2082c3a0f75aSOleksandr Tymoshenko 	}
2083831f5dcfSAlexander Motin 	/* Handle DMA border. */
2084831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
20851bacf3beSMarius Strobl 		data = slot->curcmd->data;
2086831f5dcfSAlexander Motin 
2087831f5dcfSAlexander Motin 		/* Unload DMA buffer ... */
2088831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2089831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2090831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2091831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
2092831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
2093831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
2094831f5dcfSAlexander Motin 		} else {
2095831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2096831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
2097831f5dcfSAlexander Motin 		}
2098831f5dcfSAlexander Motin 		/* ... and reload it again. */
2099831f5dcfSAlexander Motin 		slot->offset += DMA_BLOCK_SIZE;
2100831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2101831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2102831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2103831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREREAD);
2104831f5dcfSAlexander Motin 		} else {
2105831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
2106831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)? left : DMA_BLOCK_SIZE);
2107831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2108831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
2109831f5dcfSAlexander Motin 		}
2110831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
2111831f5dcfSAlexander Motin 		 * for the last page. */
2112831f5dcfSAlexander Motin 		if (left == DMA_BLOCK_SIZE) {
2113831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
2114831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2115831f5dcfSAlexander Motin 		}
2116831f5dcfSAlexander Motin 		/* Restart DMA. */
2117831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
2118831f5dcfSAlexander Motin 	}
2119831f5dcfSAlexander Motin 	/* We have got all data. */
2120c3a0f75aSOleksandr Tymoshenko 	if (intmask & SDHCI_INT_DATA_END) {
2121c3a0f75aSOleksandr Tymoshenko 		if (slot->flags & PLATFORM_DATA_STARTED) {
2122c3a0f75aSOleksandr Tymoshenko 			slot->flags &= ~PLATFORM_DATA_STARTED;
2123c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2124c3a0f75aSOleksandr Tymoshenko 		} else
2125831f5dcfSAlexander Motin 			sdhci_finish_data(slot);
2126831f5dcfSAlexander Motin 	}
2127a98788edSIan Lepore done:
2128a98788edSIan Lepore 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
2129a98788edSIan Lepore 		if (slot->flags & PLATFORM_DATA_STARTED) {
2130a98788edSIan Lepore 			slot->flags &= ~PLATFORM_DATA_STARTED;
2131a98788edSIan Lepore 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2132a98788edSIan Lepore 		} else
2133a98788edSIan Lepore 			sdhci_finish_data(slot);
2134a98788edSIan Lepore 	}
2135c3a0f75aSOleksandr Tymoshenko }
2136831f5dcfSAlexander Motin 
2137831f5dcfSAlexander Motin static void
2138831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot)
2139831f5dcfSAlexander Motin {
2140831f5dcfSAlexander Motin 	uint16_t err;
2141831f5dcfSAlexander Motin 
2142831f5dcfSAlexander Motin 	err = RD4(slot, SDHCI_ACMD12_ERR);
2143831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2144831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
2145831f5dcfSAlexander Motin 		    "there is no active command.\n", err);
2146831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2147831f5dcfSAlexander Motin 		return;
2148831f5dcfSAlexander Motin 	}
2149831f5dcfSAlexander Motin 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err);
2150831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_CMD);
2151831f5dcfSAlexander Motin }
2152831f5dcfSAlexander Motin 
2153d6b3aaf8SOleksandr Tymoshenko void
2154d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
2155831f5dcfSAlexander Motin {
21562b96b955SJustin Hibbits 	uint32_t intmask, present;
2157831f5dcfSAlexander Motin 
2158831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2159831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
2160831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
2161831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
2162831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
2163d6b3aaf8SOleksandr Tymoshenko 		return;
2164831f5dcfSAlexander Motin 	}
2165aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 2))
21665b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
21675b69a497SAlexander Motin 
2168aca38eabSMarius Strobl 	/* Handle tuning error interrupt. */
2169aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_TUNEERR)) {
2170aca38eabSMarius Strobl 		slot_printf(slot, "Tuning error indicated\n");
2171aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
2172aca38eabSMarius Strobl 		if (slot->curcmd) {
2173aca38eabSMarius Strobl 			slot->curcmd->error = MMC_ERR_BADCRC;
2174aca38eabSMarius Strobl 			sdhci_finish_command(slot);
2175aca38eabSMarius Strobl 		}
2176aca38eabSMarius Strobl 	}
2177aca38eabSMarius Strobl 	/* Handle re-tuning interrupt. */
2178aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_RETUNE))
2179aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
2180831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
2181831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2182639f59f0SIan Lepore 		present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
21832b96b955SJustin Hibbits 		slot->intmask &=
21842b96b955SJustin Hibbits 		    ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
21852b96b955SJustin Hibbits 		slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
21862b96b955SJustin Hibbits 		    SDHCI_INT_CARD_INSERT;
21872b96b955SJustin Hibbits 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
21882b96b955SJustin Hibbits 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2189831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
2190831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
2191b8bf08b1SIan Lepore 		sdhci_handle_card_present_locked(slot, present);
2192831f5dcfSAlexander Motin 	}
2193831f5dcfSAlexander Motin 	/* Handle command interrupts. */
2194831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
2195831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
2196831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
2197831f5dcfSAlexander Motin 	}
2198831f5dcfSAlexander Motin 	/* Handle data interrupts. */
2199831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
2200831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
22017e6ccea3SMarius Strobl 		/* Don't call data_irq in case of errored command. */
22027e586643SIan Lepore 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
2203831f5dcfSAlexander Motin 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
2204831f5dcfSAlexander Motin 	}
2205831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
2206831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
2207831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
2208831f5dcfSAlexander Motin 		sdhci_acmd_irq(slot);
2209831f5dcfSAlexander Motin 	}
2210831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
2211831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
2212831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
2213aca38eabSMarius Strobl 		slot_printf(slot, "Card is consuming too much power!\n");
2214831f5dcfSAlexander Motin 	}
2215aca38eabSMarius Strobl 	intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE |
2216aca38eabSMarius Strobl 	    SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK |
2217aca38eabSMarius Strobl 	    SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER);
2218831f5dcfSAlexander Motin 	/* The rest is unknown. */
2219831f5dcfSAlexander Motin 	if (intmask) {
2220831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
2221831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
2222831f5dcfSAlexander Motin 		    intmask);
2223831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2224831f5dcfSAlexander Motin 	}
2225831f5dcfSAlexander Motin 
2226831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2227831f5dcfSAlexander Motin }
2228831f5dcfSAlexander Motin 
2229d6b3aaf8SOleksandr Tymoshenko int
22301bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which,
22311bacf3beSMarius Strobl     uintptr_t *result)
2232831f5dcfSAlexander Motin {
2233831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
2234831f5dcfSAlexander Motin 
2235831f5dcfSAlexander Motin 	switch (which) {
2236831f5dcfSAlexander Motin 	default:
2237831f5dcfSAlexander Motin 		return (EINVAL);
2238831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2239bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
2240831f5dcfSAlexander Motin 		break;
2241831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2242bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
2243831f5dcfSAlexander Motin 		break;
2244831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2245bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
2246831f5dcfSAlexander Motin 		break;
2247831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2248bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
2249831f5dcfSAlexander Motin 		break;
2250831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2251bcd91d25SJayachandran C. 		*result = slot->host.f_min;
2252831f5dcfSAlexander Motin 		break;
2253831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
2254bcd91d25SJayachandran C. 		*result = slot->host.f_max;
2255831f5dcfSAlexander Motin 		break;
2256831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2257bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
2258831f5dcfSAlexander Motin 		break;
2259831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2260bcd91d25SJayachandran C. 		*result = slot->host.mode;
2261831f5dcfSAlexander Motin 		break;
2262831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2263bcd91d25SJayachandran C. 		*result = slot->host.ocr;
2264831f5dcfSAlexander Motin 		break;
2265831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2266bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
2267831f5dcfSAlexander Motin 		break;
2268831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2269bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
2270831f5dcfSAlexander Motin 		break;
2271aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2272aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED) {
2273aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) {
2274aca38eabSMarius Strobl 				*result = retune_req_reset;
2275aca38eabSMarius Strobl 				break;
2276aca38eabSMarius Strobl 			}
2277aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) {
2278aca38eabSMarius Strobl 				*result = retune_req_normal;
2279aca38eabSMarius Strobl 				break;
2280aca38eabSMarius Strobl 			}
2281aca38eabSMarius Strobl 		}
2282aca38eabSMarius Strobl 		*result = retune_req_none;
2283aca38eabSMarius Strobl 		break;
22840f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
22850f34084fSMarius Strobl 		*result = slot->host.ios.vccq;
22860f34084fSMarius Strobl 		break;
2287831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2288bcd91d25SJayachandran C. 		*result = slot->host.caps;
2289831f5dcfSAlexander Motin 		break;
2290831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2291bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
2292831f5dcfSAlexander Motin 		break;
22933a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2294aca38eabSMarius Strobl 		/*
2295aca38eabSMarius Strobl 		 * Re-tuning modes 1 and 2 restrict the maximum data length
2296aca38eabSMarius Strobl 		 * per read/write command to 4 MiB.
2297aca38eabSMarius Strobl 		 */
2298aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED &&
2299aca38eabSMarius Strobl 		    (slot->retune_mode == SDHCI_RETUNE_MODE_1 ||
2300aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_2)) {
2301aca38eabSMarius Strobl 			*result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE;
2302aca38eabSMarius Strobl 			break;
2303aca38eabSMarius Strobl 		}
2304bcd91d25SJayachandran C. 		*result = 65535;
23053a4a2557SAlexander Motin 		break;
230672dec079SMarius Strobl 	case MMCBR_IVAR_MAX_BUSY_TIMEOUT:
230772dec079SMarius Strobl 		/*
230872dec079SMarius Strobl 		 * Currently, sdhci_start_data() hardcodes 1 s for all CMDs.
230972dec079SMarius Strobl 		 */
231072dec079SMarius Strobl 		*result = 1000000;
231172dec079SMarius Strobl 		break;
2312831f5dcfSAlexander Motin 	}
2313831f5dcfSAlexander Motin 	return (0);
2314831f5dcfSAlexander Motin }
2315831f5dcfSAlexander Motin 
2316d6b3aaf8SOleksandr Tymoshenko int
23171bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which,
23181bacf3beSMarius Strobl     uintptr_t value)
2319831f5dcfSAlexander Motin {
2320831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
2321b440e965SMarius Strobl 	uint32_t clock, max_clock;
2322b440e965SMarius Strobl 	int i;
2323831f5dcfSAlexander Motin 
232415c440e1SWarner Losh 	if (sdhci_debug > 1)
232515c440e1SWarner Losh 		slot_printf(slot, "%s: var=%d\n", __func__, which);
2326831f5dcfSAlexander Motin 	switch (which) {
2327831f5dcfSAlexander Motin 	default:
2328831f5dcfSAlexander Motin 		return (EINVAL);
2329831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2330831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
2331831f5dcfSAlexander Motin 		break;
2332831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2333831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
2334831f5dcfSAlexander Motin 		break;
2335831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2336831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
2337831f5dcfSAlexander Motin 		break;
2338831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2339831f5dcfSAlexander Motin 		if (value > 0) {
234057677a3aSOleksandr Tymoshenko 			max_clock = slot->max_clk;
234157677a3aSOleksandr Tymoshenko 			clock = max_clock;
234257677a3aSOleksandr Tymoshenko 
234357677a3aSOleksandr Tymoshenko 			if (slot->version < SDHCI_SPEC_300) {
234457677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
234557677a3aSOleksandr Tymoshenko 				    i <<= 1) {
2346831f5dcfSAlexander Motin 					if (clock <= value)
2347831f5dcfSAlexander Motin 						break;
2348831f5dcfSAlexander Motin 					clock >>= 1;
2349831f5dcfSAlexander Motin 				}
2350b440e965SMarius Strobl 			} else {
235157677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
235257677a3aSOleksandr Tymoshenko 				    i += 2) {
235357677a3aSOleksandr Tymoshenko 					if (clock <= value)
235457677a3aSOleksandr Tymoshenko 						break;
235557677a3aSOleksandr Tymoshenko 					clock = max_clock / (i + 2);
235657677a3aSOleksandr Tymoshenko 				}
235757677a3aSOleksandr Tymoshenko 			}
235857677a3aSOleksandr Tymoshenko 
2359831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
2360831f5dcfSAlexander Motin 		} else
2361831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
2362831f5dcfSAlexander Motin 		break;
2363831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2364831f5dcfSAlexander Motin 		slot->host.mode = value;
2365831f5dcfSAlexander Motin 		break;
2366831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2367831f5dcfSAlexander Motin 		slot->host.ocr = value;
2368831f5dcfSAlexander Motin 		break;
2369831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2370831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
2371831f5dcfSAlexander Motin 		break;
2372831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2373831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
2374831f5dcfSAlexander Motin 		break;
23750f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
23760f34084fSMarius Strobl 		slot->host.ios.vccq = value;
23770f34084fSMarius Strobl 		break;
2378831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2379831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
2380831f5dcfSAlexander Motin 		break;
2381831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2382831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2383831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2384831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
23853a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2386aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2387831f5dcfSAlexander Motin 		return (EINVAL);
2388831f5dcfSAlexander Motin 	}
2389831f5dcfSAlexander Motin 	return (0);
2390831f5dcfSAlexander Motin }
2391831f5dcfSAlexander Motin 
239215c440e1SWarner Losh #ifdef MMCCAM
2393a94a63f0SWarner Losh void
2394d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot)
2395a94a63f0SWarner Losh {
2396a94a63f0SWarner Losh         if ((slot->devq = cam_simq_alloc(1)) == NULL) {
2397a94a63f0SWarner Losh                 goto fail;
2398a94a63f0SWarner Losh         }
2399a94a63f0SWarner Losh 
2400a94a63f0SWarner Losh         mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF);
2401a94a63f0SWarner Losh         slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll,
2402a94a63f0SWarner Losh                                   "sdhci_slot", slot, device_get_unit(slot->bus),
2403a94a63f0SWarner Losh                                   &slot->sim_mtx, 1, 1, slot->devq);
2404a94a63f0SWarner Losh 
2405a94a63f0SWarner Losh         if (slot->sim == NULL) {
2406a94a63f0SWarner Losh                 cam_simq_free(slot->devq);
2407a94a63f0SWarner Losh                 slot_printf(slot, "cannot allocate CAM SIM\n");
2408a94a63f0SWarner Losh                 goto fail;
2409a94a63f0SWarner Losh         }
2410a94a63f0SWarner Losh 
2411a94a63f0SWarner Losh         mtx_lock(&slot->sim_mtx);
2412a94a63f0SWarner Losh         if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) {
2413a94a63f0SWarner Losh                 slot_printf(slot,
2414a94a63f0SWarner Losh                               "cannot register SCSI pass-through bus\n");
2415a94a63f0SWarner Losh                 cam_sim_free(slot->sim, FALSE);
2416a94a63f0SWarner Losh                 cam_simq_free(slot->devq);
2417a94a63f0SWarner Losh                 mtx_unlock(&slot->sim_mtx);
2418a94a63f0SWarner Losh                 goto fail;
2419a94a63f0SWarner Losh         }
2420a94a63f0SWarner Losh 
2421a94a63f0SWarner Losh         mtx_unlock(&slot->sim_mtx);
2422a94a63f0SWarner Losh         /* End CAM-specific init */
2423a94a63f0SWarner Losh 	slot->card_present = 0;
2424a94a63f0SWarner Losh 	sdhci_card_task(slot, 0);
2425a94a63f0SWarner Losh         return;
2426a94a63f0SWarner Losh 
2427a94a63f0SWarner Losh fail:
2428a94a63f0SWarner Losh         if (slot->sim != NULL) {
2429a94a63f0SWarner Losh                 mtx_lock(&slot->sim_mtx);
2430a94a63f0SWarner Losh                 xpt_bus_deregister(cam_sim_path(slot->sim));
2431a94a63f0SWarner Losh                 cam_sim_free(slot->sim, FALSE);
2432a94a63f0SWarner Losh                 mtx_unlock(&slot->sim_mtx);
2433a94a63f0SWarner Losh         }
2434a94a63f0SWarner Losh 
2435a94a63f0SWarner Losh         if (slot->devq != NULL)
2436a94a63f0SWarner Losh                 cam_simq_free(slot->devq);
2437a94a63f0SWarner Losh }
2438a94a63f0SWarner Losh 
2439a94a63f0SWarner Losh static void
2440a94a63f0SWarner Losh sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb)
2441a94a63f0SWarner Losh {
2442a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2443a94a63f0SWarner Losh 
2444a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2445a94a63f0SWarner Losh 
2446a94a63f0SWarner Losh 	sdhci_cam_request(slot, ccb);
2447a94a63f0SWarner Losh }
2448a94a63f0SWarner Losh 
2449a94a63f0SWarner Losh void
2450a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb)
2451a94a63f0SWarner Losh {
2452a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2453a94a63f0SWarner Losh 
2454a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2455a94a63f0SWarner Losh 	if (slot == NULL) {
2456a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2457a94a63f0SWarner Losh 		xpt_done(ccb);
2458a94a63f0SWarner Losh 		return;
2459a94a63f0SWarner Losh 	}
2460a94a63f0SWarner Losh 
2461a94a63f0SWarner Losh 	mtx_assert(&slot->sim_mtx, MA_OWNED);
2462a94a63f0SWarner Losh 
2463a94a63f0SWarner Losh 	switch (ccb->ccb_h.func_code) {
2464a94a63f0SWarner Losh 	case XPT_PATH_INQ:
2465a94a63f0SWarner Losh 	{
2466a94a63f0SWarner Losh 		struct ccb_pathinq *cpi;
2467a94a63f0SWarner Losh 
2468a94a63f0SWarner Losh 		cpi = &ccb->cpi;
2469a94a63f0SWarner Losh 		cpi->version_num = 1;
2470a94a63f0SWarner Losh 		cpi->hba_inquiry = 0;
2471a94a63f0SWarner Losh 		cpi->target_sprt = 0;
2472a94a63f0SWarner Losh 		cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
2473a94a63f0SWarner Losh 		cpi->hba_eng_cnt = 0;
2474a94a63f0SWarner Losh 		cpi->max_target = 0;
2475a94a63f0SWarner Losh 		cpi->max_lun = 0;
2476a94a63f0SWarner Losh 		cpi->initiator_id = 1;
2477a94a63f0SWarner Losh 		cpi->maxio = MAXPHYS;
2478a94a63f0SWarner Losh 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2479a94a63f0SWarner Losh 		strncpy(cpi->hba_vid, "Deglitch Networks", HBA_IDLEN);
2480a94a63f0SWarner Losh 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2481a94a63f0SWarner Losh 		cpi->unit_number = cam_sim_unit(sim);
2482a94a63f0SWarner Losh 		cpi->bus_id = cam_sim_bus(sim);
2483a94a63f0SWarner Losh 		cpi->base_transfer_speed = 100; /* XXX WTF? */
2484a94a63f0SWarner Losh 		cpi->protocol = PROTO_MMCSD;
2485a94a63f0SWarner Losh 		cpi->protocol_version = SCSI_REV_0;
2486a94a63f0SWarner Losh 		cpi->transport = XPORT_MMCSD;
2487a94a63f0SWarner Losh 		cpi->transport_version = 0;
2488a94a63f0SWarner Losh 
2489a94a63f0SWarner Losh 		cpi->ccb_h.status = CAM_REQ_CMP;
2490a94a63f0SWarner Losh 		break;
2491a94a63f0SWarner Losh 	}
2492a94a63f0SWarner Losh 	case XPT_GET_TRAN_SETTINGS:
2493a94a63f0SWarner Losh 	{
2494a94a63f0SWarner Losh 		struct ccb_trans_settings *cts = &ccb->cts;
2495a94a63f0SWarner Losh 
2496a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2497a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n");
2498a94a63f0SWarner Losh 
2499a94a63f0SWarner Losh 		cts->protocol = PROTO_MMCSD;
2500a94a63f0SWarner Losh 		cts->protocol_version = 1;
2501a94a63f0SWarner Losh 		cts->transport = XPORT_MMCSD;
2502a94a63f0SWarner Losh 		cts->transport_version = 1;
2503a94a63f0SWarner Losh 		cts->xport_specific.valid = 0;
2504a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_ocr = slot->host.host_ocr;
2505a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_min = slot->host.f_min;
2506a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_max = slot->host.f_max;
2507a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_caps = slot->host.caps;
2508a94a63f0SWarner Losh 		memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios));
2509a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2510a94a63f0SWarner Losh 		break;
2511a94a63f0SWarner Losh 	}
2512a94a63f0SWarner Losh 	case XPT_SET_TRAN_SETTINGS:
2513a94a63f0SWarner Losh 	{
2514a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2515a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n");
2516a94a63f0SWarner Losh 		sdhci_cam_settran_settings(slot, ccb);
2517a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2518a94a63f0SWarner Losh 		break;
2519a94a63f0SWarner Losh 	}
2520a94a63f0SWarner Losh 	case XPT_RESET_BUS:
2521a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2522a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n");
2523a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2524a94a63f0SWarner Losh 		break;
2525a94a63f0SWarner Losh 	case XPT_MMC_IO:
2526a94a63f0SWarner Losh 		/*
2527a94a63f0SWarner Losh 		 * Here is the HW-dependent part of
2528a94a63f0SWarner Losh 		 * sending the command to the underlying h/w
2529a94a63f0SWarner Losh 		 * At some point in the future an interrupt comes.
2530a94a63f0SWarner Losh 		 * Then the request will be marked as completed.
2531a94a63f0SWarner Losh 		 */
2532aca38eabSMarius Strobl 		if (__predict_false(sdhci_debug > 1))
2533a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_MMC_IO\n");
2534a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INPROG;
2535a94a63f0SWarner Losh 
2536a94a63f0SWarner Losh 		sdhci_cam_handle_mmcio(sim, ccb);
2537a94a63f0SWarner Losh 		return;
2538a94a63f0SWarner Losh 		/* NOTREACHED */
2539a94a63f0SWarner Losh 		break;
2540a94a63f0SWarner Losh 	default:
2541a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INVALID;
2542a94a63f0SWarner Losh 		break;
2543a94a63f0SWarner Losh 	}
2544a94a63f0SWarner Losh 	xpt_done(ccb);
2545a94a63f0SWarner Losh 	return;
2546a94a63f0SWarner Losh }
2547a94a63f0SWarner Losh 
2548a94a63f0SWarner Losh void
2549a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim)
2550a94a63f0SWarner Losh {
2551a94a63f0SWarner Losh 	return;
2552a94a63f0SWarner Losh }
2553a94a63f0SWarner Losh 
2554a94a63f0SWarner Losh int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock) {
2555a94a63f0SWarner Losh 	int max_clock, clock, i;
2556a94a63f0SWarner Losh 
2557a94a63f0SWarner Losh 	if (proposed_clock == 0)
2558a94a63f0SWarner Losh 		return 0;
2559a94a63f0SWarner Losh 	max_clock = slot->max_clk;
2560a94a63f0SWarner Losh 	clock = max_clock;
2561a94a63f0SWarner Losh 
2562a94a63f0SWarner Losh 	if (slot->version < SDHCI_SPEC_300) {
2563a94a63f0SWarner Losh 		for (i = 0; i < SDHCI_200_MAX_DIVIDER;
2564a94a63f0SWarner Losh 		     i <<= 1) {
2565a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2566a94a63f0SWarner Losh 				break;
2567a94a63f0SWarner Losh 			clock >>= 1;
2568a94a63f0SWarner Losh 		}
2569a94a63f0SWarner Losh 	} else {
2570a94a63f0SWarner Losh 		for (i = 0; i < SDHCI_300_MAX_DIVIDER;
2571a94a63f0SWarner Losh 		     i += 2) {
2572a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2573a94a63f0SWarner Losh 				break;
2574a94a63f0SWarner Losh 			clock = max_clock / (i + 2);
2575a94a63f0SWarner Losh 		}
2576a94a63f0SWarner Losh 	}
2577a94a63f0SWarner Losh 	return clock;
2578a94a63f0SWarner Losh }
2579a94a63f0SWarner Losh 
2580a94a63f0SWarner Losh int
2581a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb)
2582a94a63f0SWarner Losh {
2583a94a63f0SWarner Losh 	struct mmc_ios *ios;
2584a94a63f0SWarner Losh 	struct mmc_ios *new_ios;
2585a94a63f0SWarner Losh 	struct ccb_trans_settings_mmc *cts;
2586a94a63f0SWarner Losh 
2587a94a63f0SWarner Losh 	ios = &slot->host.ios;
2588a94a63f0SWarner Losh 
2589a94a63f0SWarner Losh 	cts = &ccb->cts.proto_specific.mmc;
2590a94a63f0SWarner Losh 	new_ios = &cts->ios;
2591a94a63f0SWarner Losh 
2592a94a63f0SWarner Losh 	/* Update only requested fields */
2593a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CLK) {
2594a94a63f0SWarner Losh 		ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock);
2595a94a63f0SWarner Losh 		slot_printf(slot, "Clock => %d\n", ios->clock);
2596a94a63f0SWarner Losh 	}
2597a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_VDD) {
2598a94a63f0SWarner Losh 		ios->vdd = new_ios->vdd;
2599a94a63f0SWarner Losh 		slot_printf(slot, "VDD => %d\n", ios->vdd);
2600a94a63f0SWarner Losh 	}
2601a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CS) {
2602a94a63f0SWarner Losh 		ios->chip_select = new_ios->chip_select;
2603a94a63f0SWarner Losh 		slot_printf(slot, "CS => %d\n", ios->chip_select);
2604a94a63f0SWarner Losh 	}
2605a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BW) {
2606a94a63f0SWarner Losh 		ios->bus_width = new_ios->bus_width;
2607a94a63f0SWarner Losh 		slot_printf(slot, "Bus width => %d\n", ios->bus_width);
2608a94a63f0SWarner Losh 	}
2609a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_PM) {
2610a94a63f0SWarner Losh 		ios->power_mode = new_ios->power_mode;
2611a94a63f0SWarner Losh 		slot_printf(slot, "Power mode => %d\n", ios->power_mode);
2612a94a63f0SWarner Losh 	}
2613a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BT) {
2614a94a63f0SWarner Losh 		ios->timing = new_ios->timing;
2615a94a63f0SWarner Losh 		slot_printf(slot, "Timing => %d\n", ios->timing);
2616a94a63f0SWarner Losh 	}
2617a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BM) {
2618a94a63f0SWarner Losh 		ios->bus_mode = new_ios->bus_mode;
2619a94a63f0SWarner Losh 		slot_printf(slot, "Bus mode => %d\n", ios->bus_mode);
2620a94a63f0SWarner Losh 	}
2621a94a63f0SWarner Losh 
2622a94a63f0SWarner Losh         /* XXX Provide a way to call a chip-specific IOS update, required for TI */
2623a94a63f0SWarner Losh 	return (sdhci_cam_update_ios(slot));
2624a94a63f0SWarner Losh }
2625a94a63f0SWarner Losh 
2626a94a63f0SWarner Losh int
2627a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot)
2628a94a63f0SWarner Losh {
2629a94a63f0SWarner Losh 	struct mmc_ios *ios = &slot->host.ios;
2630a94a63f0SWarner Losh 
2631a94a63f0SWarner Losh 	slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n",
2632a94a63f0SWarner Losh 		    __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing);
2633a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2634a94a63f0SWarner Losh 	/* Do full reset on bus power down to clear from any state. */
2635a94a63f0SWarner Losh 	if (ios->power_mode == power_off) {
2636a94a63f0SWarner Losh 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
2637a94a63f0SWarner Losh 		sdhci_init(slot);
2638a94a63f0SWarner Losh 	}
2639a94a63f0SWarner Losh 	/* Configure the bus. */
2640a94a63f0SWarner Losh 	sdhci_set_clock(slot, ios->clock);
2641a94a63f0SWarner Losh 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
2642a94a63f0SWarner Losh 	if (ios->bus_width == bus_width_8) {
2643a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
2644a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2645a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_4) {
2646a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2647a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
2648a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_1) {
2649a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2650a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2651a94a63f0SWarner Losh 	} else {
2652a94a63f0SWarner Losh 		panic("Invalid bus width: %d", ios->bus_width);
2653a94a63f0SWarner Losh 	}
2654a94a63f0SWarner Losh 	if (ios->timing == bus_timing_hs &&
2655a94a63f0SWarner Losh 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
2656a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_HISPD;
2657a94a63f0SWarner Losh 	else
2658a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
2659a94a63f0SWarner Losh 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
2660a94a63f0SWarner Losh 	/* Some controllers like reset after bus changes. */
2661a94a63f0SWarner Losh 	if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
2662a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2663a94a63f0SWarner Losh 
2664a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2665a94a63f0SWarner Losh 	return (0);
2666a94a63f0SWarner Losh }
2667a94a63f0SWarner Losh 
2668a94a63f0SWarner Losh int
2669a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb)
2670a94a63f0SWarner Losh {
2671a94a63f0SWarner Losh 	struct ccb_mmcio *mmcio;
2672a94a63f0SWarner Losh 
2673a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
2674a94a63f0SWarner Losh 
2675a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2676a94a63f0SWarner Losh /*	if (slot->req != NULL) {
2677a94a63f0SWarner Losh 		SDHCI_UNLOCK(slot);
2678a94a63f0SWarner Losh 		return (EBUSY);
2679a94a63f0SWarner Losh 	}
2680a94a63f0SWarner Losh */
2681aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
2682a94a63f0SWarner Losh 		slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
2683a94a63f0SWarner Losh 			    mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags,
2684a94a63f0SWarner Losh 			    mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0,
2685a94a63f0SWarner Losh 			    mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0);
2686a94a63f0SWarner Losh 	}
2687a94a63f0SWarner Losh 	if (mmcio->cmd.data != NULL) {
2688a94a63f0SWarner Losh 		if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0)
2689a94a63f0SWarner Losh 			panic("data->len = %d, data->flags = %d -- something is b0rked",
2690a94a63f0SWarner Losh 			      (int)mmcio->cmd.data->len, mmcio->cmd.data->flags);
2691a94a63f0SWarner Losh 	}
2692a94a63f0SWarner Losh 	slot->ccb = ccb;
2693a94a63f0SWarner Losh 	slot->flags = 0;
2694a94a63f0SWarner Losh 	sdhci_start(slot);
2695a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2696a94a63f0SWarner Losh 	if (dumping) {
2697a94a63f0SWarner Losh 		while (slot->ccb != NULL) {
2698a94a63f0SWarner Losh 			sdhci_generic_intr(slot);
2699a94a63f0SWarner Losh 			DELAY(10);
2700a94a63f0SWarner Losh 		}
2701a94a63f0SWarner Losh 	}
2702a94a63f0SWarner Losh 	return (0);
2703a94a63f0SWarner Losh }
270415c440e1SWarner Losh #endif /* MMCCAM */
2705a94a63f0SWarner Losh 
2706d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1);
2707