xref: /freebsd/sys/dev/sdhci/sdhci.c (revision 505f6a0cea3c872a66c09789727bcf183563961b)
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>
39ab00a509SMarius Strobl #include <sys/libkern.h>
40831f5dcfSAlexander Motin #include <sys/lock.h>
41aca38eabSMarius Strobl #include <sys/malloc.h>
42831f5dcfSAlexander Motin #include <sys/module.h>
43831f5dcfSAlexander Motin #include <sys/mutex.h>
44831f5dcfSAlexander Motin #include <sys/resource.h>
45831f5dcfSAlexander Motin #include <sys/rman.h>
465b69a497SAlexander Motin #include <sys/sysctl.h>
47831f5dcfSAlexander Motin #include <sys/taskqueue.h>
48831f5dcfSAlexander Motin 
49831f5dcfSAlexander Motin #include <machine/bus.h>
50831f5dcfSAlexander Motin #include <machine/resource.h>
51831f5dcfSAlexander Motin #include <machine/stdarg.h>
52831f5dcfSAlexander Motin 
53831f5dcfSAlexander Motin #include <dev/mmc/bridge.h>
54831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h>
55831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h>
56831f5dcfSAlexander Motin 
57aca38eabSMarius Strobl #include <dev/sdhci/sdhci.h>
58aca38eabSMarius Strobl 
59a94a63f0SWarner Losh #include <cam/cam.h>
60a94a63f0SWarner Losh #include <cam/cam_ccb.h>
61a94a63f0SWarner Losh #include <cam/cam_debug.h>
62a94a63f0SWarner Losh #include <cam/cam_sim.h>
63a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h>
64a94a63f0SWarner Losh 
65831f5dcfSAlexander Motin #include "mmcbr_if.h"
66d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h"
67831f5dcfSAlexander Motin 
68a94a63f0SWarner Losh #include "opt_mmccam.h"
69a94a63f0SWarner Losh 
70f0d2731dSMarius Strobl SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver");
71831f5dcfSAlexander Motin 
72a94a63f0SWarner Losh static int sdhci_debug = 0;
737e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0,
747e6ccea3SMarius Strobl     "Debug level");
750f34084fSMarius Strobl u_int sdhci_quirk_clear = 0;
760f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear,
770f34084fSMarius Strobl     0, "Mask of quirks to clear");
780f34084fSMarius Strobl u_int sdhci_quirk_set = 0;
790f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0,
800f34084fSMarius Strobl     "Mask of quirks to set");
815b69a497SAlexander Motin 
82d6b3aaf8SOleksandr Tymoshenko #define	RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
83d6b3aaf8SOleksandr Tymoshenko #define	RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
84d6b3aaf8SOleksandr Tymoshenko #define	RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
85d6b3aaf8SOleksandr Tymoshenko #define	RD_MULTI_4(slot, off, ptr, count)	\
86d6b3aaf8SOleksandr Tymoshenko     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
87831f5dcfSAlexander Motin 
88d6b3aaf8SOleksandr Tymoshenko #define	WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
89d6b3aaf8SOleksandr Tymoshenko #define	WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
90d6b3aaf8SOleksandr Tymoshenko #define	WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
91d6b3aaf8SOleksandr Tymoshenko #define	WR_MULTI_4(slot, off, ptr, count)	\
92d6b3aaf8SOleksandr Tymoshenko     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
93831f5dcfSAlexander Motin 
946dea80e6SMarius Strobl static void sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err);
95aca38eabSMarius Strobl static void sdhci_card_poll(void *arg);
96aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending);
976dea80e6SMarius Strobl static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask);
986dea80e6SMarius Strobl static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask);
99aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset);
1006dea80e6SMarius Strobl static void sdhci_handle_card_present_locked(struct sdhci_slot *slot,
1016dea80e6SMarius Strobl     bool is_present);
1026dea80e6SMarius Strobl static void sdhci_finish_command(struct sdhci_slot *slot);
1036dea80e6SMarius Strobl static void sdhci_init(struct sdhci_slot *slot);
1046dea80e6SMarius Strobl static void sdhci_read_block_pio(struct sdhci_slot *slot);
1056dea80e6SMarius Strobl static void sdhci_req_done(struct sdhci_slot *slot);
106aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req);
1076dea80e6SMarius Strobl static void sdhci_reset(struct sdhci_slot *slot, uint8_t mask);
108aca38eabSMarius Strobl static void sdhci_retune(void *arg);
109831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
1106dea80e6SMarius Strobl static void sdhci_set_power(struct sdhci_slot *slot, u_char power);
1116dea80e6SMarius Strobl static void sdhci_set_transfer_mode(struct sdhci_slot *slot,
112ab00a509SMarius Strobl    const struct mmc_data *data);
113831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot);
1146dea80e6SMarius Strobl static void sdhci_timeout(void *arg);
1156dea80e6SMarius Strobl static void sdhci_start_command(struct sdhci_slot *slot,
1166dea80e6SMarius Strobl    struct mmc_command *cmd);
117ab00a509SMarius Strobl static void sdhci_start_data(struct sdhci_slot *slot,
118ab00a509SMarius Strobl    const struct mmc_data *data);
1196dea80e6SMarius Strobl static void sdhci_write_block_pio(struct sdhci_slot *slot);
1206dea80e6SMarius Strobl static void sdhci_transfer_pio(struct sdhci_slot *slot);
121831f5dcfSAlexander Motin 
12215c440e1SWarner Losh #ifdef MMCCAM
123a94a63f0SWarner Losh /* CAM-related */
124a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb);
125ab00a509SMarius Strobl static int sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot,
1266dea80e6SMarius Strobl     int proposed_clock);
1276dea80e6SMarius Strobl static void sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb);
128a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim);
1296dea80e6SMarius Strobl static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb);
130a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb);
1316dea80e6SMarius Strobl static int sdhci_cam_update_ios(struct sdhci_slot *slot);
13215c440e1SWarner Losh #endif
133a94a63f0SWarner Losh 
134831f5dcfSAlexander Motin /* helper routines */
135ab00a509SMarius Strobl static int sdhci_dma_alloc(struct sdhci_slot *slot);
136ab00a509SMarius Strobl static void sdhci_dma_free(struct sdhci_slot *slot);
1370f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot);
1386dea80e6SMarius Strobl static void sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs,
1396dea80e6SMarius Strobl     int error);
140ab00a509SMarius Strobl static int slot_printf(const struct sdhci_slot *slot, const char * fmt, ...)
1410f34084fSMarius Strobl     __printflike(2, 3);
142ab00a509SMarius Strobl static uint32_t sdhci_tuning_intmask(const struct sdhci_slot *slot);
1430f34084fSMarius Strobl 
144831f5dcfSAlexander Motin #define	SDHCI_LOCK(_slot)		mtx_lock(&(_slot)->mtx)
145831f5dcfSAlexander Motin #define	SDHCI_UNLOCK(_slot)		mtx_unlock(&(_slot)->mtx)
146831f5dcfSAlexander Motin #define	SDHCI_LOCK_INIT(_slot) \
147831f5dcfSAlexander Motin 	mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
148831f5dcfSAlexander Motin #define	SDHCI_LOCK_DESTROY(_slot)	mtx_destroy(&_slot->mtx);
149831f5dcfSAlexander Motin #define	SDHCI_ASSERT_LOCKED(_slot)	mtx_assert(&_slot->mtx, MA_OWNED);
150831f5dcfSAlexander Motin #define	SDHCI_ASSERT_UNLOCKED(_slot)	mtx_assert(&_slot->mtx, MA_NOTOWNED);
151831f5dcfSAlexander Motin 
15233aad34dSOleksandr Tymoshenko #define	SDHCI_DEFAULT_MAX_FREQ	50
15333aad34dSOleksandr Tymoshenko 
15457677a3aSOleksandr Tymoshenko #define	SDHCI_200_MAX_DIVIDER	256
15557677a3aSOleksandr Tymoshenko #define	SDHCI_300_MAX_DIVIDER	2046
15657677a3aSOleksandr Tymoshenko 
157639f59f0SIan Lepore #define	SDHCI_CARD_PRESENT_TICKS	(hz / 5)
158639f59f0SIan Lepore #define	SDHCI_INSERT_DELAY_TICKS	(hz / 2)
159639f59f0SIan Lepore 
16093efdc63SAdrian Chadd /*
16193efdc63SAdrian Chadd  * Broadcom BCM577xx Controller Constants
16293efdc63SAdrian Chadd  */
1631bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */
1641bacf3beSMarius Strobl #define	BCM577XX_DEFAULT_MAX_DIVIDER	256
1651bacf3beSMarius Strobl /* Alternative clock's base frequency. */
1661bacf3beSMarius Strobl #define	BCM577XX_ALT_CLOCK_BASE		63000000
16793efdc63SAdrian Chadd 
16893efdc63SAdrian Chadd #define	BCM577XX_HOST_CONTROL		0x198
16993efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_MASK	0xFFFFCFFF
17093efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_SHIFT	12
17193efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_DEFAULT	0x0
17293efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_64MHZ	0x3
17393efdc63SAdrian Chadd 
174831f5dcfSAlexander Motin static void
175831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
176831f5dcfSAlexander Motin {
1777e6ccea3SMarius Strobl 
178831f5dcfSAlexander Motin 	if (error != 0) {
179831f5dcfSAlexander Motin 		printf("getaddr: error %d\n", error);
180831f5dcfSAlexander Motin 		return;
181831f5dcfSAlexander Motin 	}
182831f5dcfSAlexander Motin 	*(bus_addr_t *)arg = segs[0].ds_addr;
183831f5dcfSAlexander Motin }
184831f5dcfSAlexander Motin 
185d6b3aaf8SOleksandr Tymoshenko static int
186ab00a509SMarius Strobl slot_printf(const struct sdhci_slot *slot, const char * fmt, ...)
187d6b3aaf8SOleksandr Tymoshenko {
188d6b3aaf8SOleksandr Tymoshenko 	va_list ap;
189d6b3aaf8SOleksandr Tymoshenko 	int retval;
190d6b3aaf8SOleksandr Tymoshenko 
191d6b3aaf8SOleksandr Tymoshenko 	retval = printf("%s-slot%d: ",
192d6b3aaf8SOleksandr Tymoshenko 	    device_get_nameunit(slot->bus), slot->num);
193d6b3aaf8SOleksandr Tymoshenko 
194d6b3aaf8SOleksandr Tymoshenko 	va_start(ap, fmt);
195d6b3aaf8SOleksandr Tymoshenko 	retval += vprintf(fmt, ap);
196d6b3aaf8SOleksandr Tymoshenko 	va_end(ap);
197d6b3aaf8SOleksandr Tymoshenko 	return (retval);
198d6b3aaf8SOleksandr Tymoshenko }
199d6b3aaf8SOleksandr Tymoshenko 
200831f5dcfSAlexander Motin static void
201831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot)
202831f5dcfSAlexander Motin {
2037e6ccea3SMarius Strobl 
204831f5dcfSAlexander Motin 	slot_printf(slot,
205831f5dcfSAlexander Motin 	    "============== REGISTER DUMP ==============\n");
206831f5dcfSAlexander Motin 
207831f5dcfSAlexander Motin 	slot_printf(slot, "Sys addr: 0x%08x | Version:  0x%08x\n",
208831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
209831f5dcfSAlexander Motin 	slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
210831f5dcfSAlexander Motin 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
211831f5dcfSAlexander Motin 	slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
212831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
213831f5dcfSAlexander Motin 	slot_printf(slot, "Present:  0x%08x | Host ctl: 0x%08x\n",
214831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
215831f5dcfSAlexander Motin 	slot_printf(slot, "Power:    0x%08x | Blk gap:  0x%08x\n",
216831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
217831f5dcfSAlexander Motin 	slot_printf(slot, "Wake-up:  0x%08x | Clock:    0x%08x\n",
218831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
219831f5dcfSAlexander Motin 	slot_printf(slot, "Timeout:  0x%08x | Int stat: 0x%08x\n",
220831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
221831f5dcfSAlexander Motin 	slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
222831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
2239dbf8c46SMarius Strobl 	slot_printf(slot, "AC12 err: 0x%08x | Host ctl2:0x%08x\n",
2249dbf8c46SMarius Strobl 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2));
2259dbf8c46SMarius Strobl 	slot_printf(slot, "Caps:     0x%08x | Caps2:    0x%08x\n",
2269dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2));
2279dbf8c46SMarius Strobl 	slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n",
2289dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR));
2299dbf8c46SMarius Strobl 	slot_printf(slot, "ADMA addr:0x%08x | Slot int: 0x%08x\n",
2309dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS));
231831f5dcfSAlexander Motin 
232831f5dcfSAlexander Motin 	slot_printf(slot,
233831f5dcfSAlexander Motin 	    "===========================================\n");
234831f5dcfSAlexander Motin }
235831f5dcfSAlexander Motin 
236831f5dcfSAlexander Motin static void
237831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
238831f5dcfSAlexander Motin {
239831f5dcfSAlexander Motin 	int timeout;
240b440e965SMarius Strobl 	uint32_t clock;
241831f5dcfSAlexander Motin 
242d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
2436e37fb2bSIan Lepore 		if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
244831f5dcfSAlexander Motin 			return;
245831f5dcfSAlexander Motin 	}
246831f5dcfSAlexander Motin 
247831f5dcfSAlexander Motin 	/* Some controllers need this kick or reset won't work. */
248831f5dcfSAlexander Motin 	if ((mask & SDHCI_RESET_ALL) == 0 &&
249d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
250831f5dcfSAlexander Motin 		/* This is to force an update */
251831f5dcfSAlexander Motin 		clock = slot->clock;
252831f5dcfSAlexander Motin 		slot->clock = 0;
253831f5dcfSAlexander Motin 		sdhci_set_clock(slot, clock);
254831f5dcfSAlexander Motin 	}
255831f5dcfSAlexander Motin 
256d8208d9eSAlexander Motin 	if (mask & SDHCI_RESET_ALL) {
257831f5dcfSAlexander Motin 		slot->clock = 0;
258d8208d9eSAlexander Motin 		slot->power = 0;
259d8208d9eSAlexander Motin 	}
260831f5dcfSAlexander Motin 
26161bc42f7SIan Lepore 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
26261bc42f7SIan Lepore 
26361bc42f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
26461bc42f7SIan Lepore 		/*
26561bc42f7SIan Lepore 		 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
26661bc42f7SIan Lepore 		 * specification.  The reset bit has internal propagation delay,
26761bc42f7SIan Lepore 		 * so a fast read after write returns 0 even if reset process is
26861bc42f7SIan Lepore 		 * in progress.  The workaround is to poll for 1 before polling
26961bc42f7SIan Lepore 		 * for 0.  In the worst case, if we miss seeing it asserted the
27061bc42f7SIan Lepore 		 * time we spent waiting is enough to ensure the reset finishes.
27161bc42f7SIan Lepore 		 */
27261bc42f7SIan Lepore 		timeout = 10000;
27361bc42f7SIan Lepore 		while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
27461bc42f7SIan Lepore 			if (timeout <= 0)
27561bc42f7SIan Lepore 				break;
27661bc42f7SIan Lepore 			timeout--;
27761bc42f7SIan Lepore 			DELAY(1);
27861bc42f7SIan Lepore 		}
27961bc42f7SIan Lepore 	}
28061bc42f7SIan Lepore 
281831f5dcfSAlexander Motin 	/* Wait max 100 ms */
28261bc42f7SIan Lepore 	timeout = 10000;
283831f5dcfSAlexander Motin 	/* Controller clears the bits when it's done */
28461bc42f7SIan Lepore 	while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
28561bc42f7SIan Lepore 		if (timeout <= 0) {
28661bc42f7SIan Lepore 			slot_printf(slot, "Reset 0x%x never completed.\n",
28761bc42f7SIan Lepore 			    mask);
288831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
289831f5dcfSAlexander Motin 			return;
290831f5dcfSAlexander Motin 		}
291831f5dcfSAlexander Motin 		timeout--;
29261bc42f7SIan Lepore 		DELAY(10);
293831f5dcfSAlexander Motin 	}
294831f5dcfSAlexander Motin }
295831f5dcfSAlexander Motin 
296aca38eabSMarius Strobl static uint32_t
297ab00a509SMarius Strobl sdhci_tuning_intmask(const struct sdhci_slot *slot)
298aca38eabSMarius Strobl {
299aca38eabSMarius Strobl 	uint32_t intmask;
300aca38eabSMarius Strobl 
301aca38eabSMarius Strobl 	intmask = 0;
30278f8baa8SMarius Strobl 	if (slot->opt & SDHCI_TUNING_ENABLED) {
303aca38eabSMarius Strobl 		intmask |= SDHCI_INT_TUNEERR;
304aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_2 ||
305aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_3)
306aca38eabSMarius Strobl 			intmask |= SDHCI_INT_RETUNE;
307aca38eabSMarius Strobl 	}
308aca38eabSMarius Strobl 	return (intmask);
309aca38eabSMarius Strobl }
310aca38eabSMarius Strobl 
311831f5dcfSAlexander Motin static void
312831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot)
313831f5dcfSAlexander Motin {
314831f5dcfSAlexander Motin 
315831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
316831f5dcfSAlexander Motin 
317831f5dcfSAlexander Motin 	/* Enable interrupts. */
318831f5dcfSAlexander Motin 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
319831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
320831f5dcfSAlexander Motin 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
321831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
322831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
3233685b398SWarner Losh 	    SDHCI_INT_ACMD12ERR;
324639f59f0SIan Lepore 
325639f59f0SIan Lepore 	if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
326639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
327639f59f0SIan Lepore 		slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
328639f59f0SIan Lepore 	}
329639f59f0SIan Lepore 
330cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
331831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
332831f5dcfSAlexander Motin }
333831f5dcfSAlexander Motin 
334831f5dcfSAlexander Motin static void
335831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
336831f5dcfSAlexander Motin {
33793efdc63SAdrian Chadd 	uint32_t clk_base;
33893efdc63SAdrian Chadd 	uint32_t clk_sel;
339831f5dcfSAlexander Motin 	uint32_t res;
340831f5dcfSAlexander Motin 	uint16_t clk;
3418f3b7d56SOleksandr Tymoshenko 	uint16_t div;
342831f5dcfSAlexander Motin 	int timeout;
343831f5dcfSAlexander Motin 
344831f5dcfSAlexander Motin 	if (clock == slot->clock)
345831f5dcfSAlexander Motin 		return;
346831f5dcfSAlexander Motin 	slot->clock = clock;
347831f5dcfSAlexander Motin 
348831f5dcfSAlexander Motin 	/* Turn off the clock. */
3494ddc0172SIan Lepore 	clk = RD2(slot, SDHCI_CLOCK_CONTROL);
3504ddc0172SIan Lepore 	WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
351b440e965SMarius Strobl 	/* If no clock requested - leave it so. */
352831f5dcfSAlexander Motin 	if (clock == 0)
353831f5dcfSAlexander Motin 		return;
354ceb9e9f7SIan Lepore 
35593efdc63SAdrian Chadd 	/* Determine the clock base frequency */
35693efdc63SAdrian Chadd 	clk_base = slot->max_clk;
35793efdc63SAdrian Chadd 	if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
3581bacf3beSMarius Strobl 		clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) &
3591bacf3beSMarius Strobl 		    BCM577XX_CTRL_CLKSEL_MASK;
36093efdc63SAdrian Chadd 
3611bacf3beSMarius Strobl 		/*
3621bacf3beSMarius Strobl 		 * Select clock source appropriate for the requested frequency.
3631bacf3beSMarius Strobl 		 */
36493efdc63SAdrian Chadd 		if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
36593efdc63SAdrian Chadd 			clk_base = BCM577XX_ALT_CLOCK_BASE;
3661bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ <<
3671bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
36893efdc63SAdrian Chadd 		} else {
3691bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT <<
3701bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
37193efdc63SAdrian Chadd 		}
37293efdc63SAdrian Chadd 
37393efdc63SAdrian Chadd 		WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
37493efdc63SAdrian Chadd 	}
37593efdc63SAdrian Chadd 
376ceb9e9f7SIan Lepore 	/* Recalculate timeout clock frequency based on the new sd clock. */
377ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
378ceb9e9f7SIan Lepore 		slot->timeout_clk = slot->clock / 1000;
379ceb9e9f7SIan Lepore 
3808f3b7d56SOleksandr Tymoshenko 	if (slot->version < SDHCI_SPEC_300) {
381831f5dcfSAlexander Motin 		/* Looking for highest freq <= clock. */
38293efdc63SAdrian Chadd 		res = clk_base;
38357677a3aSOleksandr Tymoshenko 		for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
384831f5dcfSAlexander Motin 			if (res <= clock)
385831f5dcfSAlexander Motin 				break;
386831f5dcfSAlexander Motin 			res >>= 1;
387831f5dcfSAlexander Motin 		}
388831f5dcfSAlexander Motin 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
3898f3b7d56SOleksandr Tymoshenko 		div >>= 1;
390c11bbc7dSMarius Strobl 	} else {
3918f3b7d56SOleksandr Tymoshenko 		/* Version 3.0 divisors are multiples of two up to 1023 * 2 */
39293efdc63SAdrian Chadd 		if (clock >= clk_base)
39357677a3aSOleksandr Tymoshenko 			div = 0;
3948f3b7d56SOleksandr Tymoshenko 		else {
39557677a3aSOleksandr Tymoshenko 			for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
39693efdc63SAdrian Chadd 				if ((clk_base / div) <= clock)
3978f3b7d56SOleksandr Tymoshenko 					break;
3988f3b7d56SOleksandr Tymoshenko 			}
3998f3b7d56SOleksandr Tymoshenko 		}
4008f3b7d56SOleksandr Tymoshenko 		div >>= 1;
4018f3b7d56SOleksandr Tymoshenko 	}
4028f3b7d56SOleksandr Tymoshenko 
4038f3b7d56SOleksandr Tymoshenko 	if (bootverbose || sdhci_debug)
40493efdc63SAdrian Chadd 		slot_printf(slot, "Divider %d for freq %d (base %d)\n",
40593efdc63SAdrian Chadd 			div, clock, clk_base);
4068f3b7d56SOleksandr Tymoshenko 
407831f5dcfSAlexander Motin 	/* Now we have got divider, set it. */
4088f3b7d56SOleksandr Tymoshenko 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
4098f3b7d56SOleksandr Tymoshenko 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
4108f3b7d56SOleksandr Tymoshenko 		<< SDHCI_DIVIDER_HI_SHIFT;
4118f3b7d56SOleksandr Tymoshenko 
412831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
413831f5dcfSAlexander Motin 	/* Enable clock. */
414831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_INT_EN;
415831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
416831f5dcfSAlexander Motin 	/* Wait up to 10 ms until it stabilize. */
417831f5dcfSAlexander Motin 	timeout = 10;
418831f5dcfSAlexander Motin 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
419831f5dcfSAlexander Motin 		& SDHCI_CLOCK_INT_STABLE)) {
420831f5dcfSAlexander Motin 		if (timeout == 0) {
421831f5dcfSAlexander Motin 			slot_printf(slot,
422831f5dcfSAlexander Motin 			    "Internal clock never stabilised.\n");
423831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
424831f5dcfSAlexander Motin 			return;
425831f5dcfSAlexander Motin 		}
426831f5dcfSAlexander Motin 		timeout--;
427831f5dcfSAlexander Motin 		DELAY(1000);
428831f5dcfSAlexander Motin 	}
429831f5dcfSAlexander Motin 	/* Pass clock signal to the bus. */
430831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_CARD_EN;
431831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
432831f5dcfSAlexander Motin }
433831f5dcfSAlexander Motin 
434831f5dcfSAlexander Motin static void
435831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power)
436831f5dcfSAlexander Motin {
43785083a80SMarius Strobl 	int i;
438831f5dcfSAlexander Motin 	uint8_t pwr;
439831f5dcfSAlexander Motin 
440831f5dcfSAlexander Motin 	if (slot->power == power)
441831f5dcfSAlexander Motin 		return;
442d6b3aaf8SOleksandr Tymoshenko 
443831f5dcfSAlexander Motin 	slot->power = power;
444831f5dcfSAlexander Motin 
445831f5dcfSAlexander Motin 	/* Turn off the power. */
446831f5dcfSAlexander Motin 	pwr = 0;
447831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
448b440e965SMarius Strobl 	/* If power down requested - leave it so. */
449831f5dcfSAlexander Motin 	if (power == 0)
450831f5dcfSAlexander Motin 		return;
451831f5dcfSAlexander Motin 	/* Set voltage. */
452831f5dcfSAlexander Motin 	switch (1 << power) {
453831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
454831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
455831f5dcfSAlexander Motin 		break;
456831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
457831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
458831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
459831f5dcfSAlexander Motin 		break;
460831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
461831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
462831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
463831f5dcfSAlexander Motin 		break;
464831f5dcfSAlexander Motin 	}
465831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
46685083a80SMarius Strobl 	/*
46785083a80SMarius Strobl 	 * Turn on VDD1 power.  Note that at least some Intel controllers can
46885083a80SMarius Strobl 	 * fail to enable bus power on the first try after transiting from D3
4698022c8ebSMarius Strobl 	 * to D0, so we give them up to 2 ms.
47085083a80SMarius Strobl 	 */
471831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
47285083a80SMarius Strobl 	for (i = 0; i < 20; i++) {
473831f5dcfSAlexander Motin 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
47485083a80SMarius Strobl 		if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)
47585083a80SMarius Strobl 			break;
47685083a80SMarius Strobl 		DELAY(100);
47785083a80SMarius Strobl 	}
47885083a80SMarius Strobl 	if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON))
47985083a80SMarius Strobl 		slot_printf(slot, "Bus power failed to enable");
480a2832f9fSMarius Strobl 
481a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) {
482a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10);
483a2832f9fSMarius Strobl 		DELAY(10);
484a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
485a2832f9fSMarius Strobl 		DELAY(300);
486a2832f9fSMarius Strobl 	}
487831f5dcfSAlexander Motin }
488831f5dcfSAlexander Motin 
489831f5dcfSAlexander Motin static void
490831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
491831f5dcfSAlexander Motin {
492831f5dcfSAlexander Motin 	uint32_t data;
493831f5dcfSAlexander Motin 	char *buffer;
494831f5dcfSAlexander Motin 	size_t left;
495831f5dcfSAlexander Motin 
496831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
497831f5dcfSAlexander Motin 	buffer += slot->offset;
498831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
499831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
500831f5dcfSAlexander Motin 	slot->offset += left;
501831f5dcfSAlexander Motin 
502831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
503d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
504831f5dcfSAlexander Motin 		DELAY(10);
505ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
506831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
507831f5dcfSAlexander Motin 		while (left > 3) {
508831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
509831f5dcfSAlexander Motin 			buffer[0] = data;
510831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
511831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
512831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
513831f5dcfSAlexander Motin 			buffer += 4;
514831f5dcfSAlexander Motin 			left -= 4;
515831f5dcfSAlexander Motin 		}
516831f5dcfSAlexander Motin 	} else {
517d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
518831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
519831f5dcfSAlexander Motin 		left &= 3;
520831f5dcfSAlexander Motin 	}
521831f5dcfSAlexander Motin 	/* Handle uneven size case. */
522831f5dcfSAlexander Motin 	if (left > 0) {
523831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
524831f5dcfSAlexander Motin 		while (left > 0) {
525831f5dcfSAlexander Motin 			*(buffer++) = data;
526831f5dcfSAlexander Motin 			data >>= 8;
527831f5dcfSAlexander Motin 			left--;
528831f5dcfSAlexander Motin 		}
529831f5dcfSAlexander Motin 	}
530831f5dcfSAlexander Motin }
531831f5dcfSAlexander Motin 
532831f5dcfSAlexander Motin static void
533831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
534831f5dcfSAlexander Motin {
535831f5dcfSAlexander Motin 	uint32_t data = 0;
536831f5dcfSAlexander Motin 	char *buffer;
537831f5dcfSAlexander Motin 	size_t left;
538831f5dcfSAlexander Motin 
539831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
540831f5dcfSAlexander Motin 	buffer += slot->offset;
541831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
542831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
543831f5dcfSAlexander Motin 	slot->offset += left;
544831f5dcfSAlexander Motin 
545ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
546831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
547831f5dcfSAlexander Motin 		while (left > 3) {
548831f5dcfSAlexander Motin 			data = buffer[0] +
549831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
550831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
551831f5dcfSAlexander Motin 			    (buffer[3] << 24);
552831f5dcfSAlexander Motin 			left -= 4;
553831f5dcfSAlexander Motin 			buffer += 4;
554831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
555831f5dcfSAlexander Motin 		}
556831f5dcfSAlexander Motin 	} else {
557d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
558831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
559831f5dcfSAlexander Motin 		left &= 3;
560831f5dcfSAlexander Motin 	}
561831f5dcfSAlexander Motin 	/* Handle uneven size case. */
562831f5dcfSAlexander Motin 	if (left > 0) {
563831f5dcfSAlexander Motin 		while (left > 0) {
564831f5dcfSAlexander Motin 			data <<= 8;
565831f5dcfSAlexander Motin 			data += *(buffer++);
566831f5dcfSAlexander Motin 			left--;
567831f5dcfSAlexander Motin 		}
568831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
569831f5dcfSAlexander Motin 	}
570831f5dcfSAlexander Motin }
571831f5dcfSAlexander Motin 
572831f5dcfSAlexander Motin static void
573831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
574831f5dcfSAlexander Motin {
575831f5dcfSAlexander Motin 
576831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
577831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
578831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
579831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
580831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
581831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
582831f5dcfSAlexander Motin 				break;
583831f5dcfSAlexander Motin 		}
584831f5dcfSAlexander Motin 	} else {
585831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
586831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
587831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
588831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
589831f5dcfSAlexander Motin 				break;
590831f5dcfSAlexander Motin 		}
591831f5dcfSAlexander Motin 	}
592831f5dcfSAlexander Motin }
593831f5dcfSAlexander Motin 
594831f5dcfSAlexander Motin static void
5957e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused)
596831f5dcfSAlexander Motin {
597831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
5987e6ccea3SMarius Strobl 	device_t d;
599831f5dcfSAlexander Motin 
600831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
6016e37fb2bSIan Lepore 	if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
602a94a63f0SWarner Losh #ifdef MMCCAM
603a94a63f0SWarner Losh 		if (slot->card_present == 0) {
604a94a63f0SWarner Losh #else
605831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
606a94a63f0SWarner Losh #endif
607831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
608639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
609639f59f0SIan Lepore 				slot_printf(slot, "Card inserted\n");
610a94a63f0SWarner Losh #ifdef MMCCAM
611a94a63f0SWarner Losh 			slot->card_present = 1;
612a94a63f0SWarner Losh 			union ccb *ccb;
613a94a63f0SWarner Losh 			uint32_t pathid;
614a94a63f0SWarner Losh 			pathid = cam_sim_path(slot->sim);
615a94a63f0SWarner Losh 			ccb = xpt_alloc_ccb_nowait();
616a94a63f0SWarner Losh 			if (ccb == NULL) {
617a94a63f0SWarner Losh 				slot_printf(slot, "Unable to alloc CCB for rescan\n");
618a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
619a94a63f0SWarner Losh 				return;
620a94a63f0SWarner Losh 			}
621a94a63f0SWarner Losh 
622a94a63f0SWarner Losh 			/*
623a94a63f0SWarner Losh 			 * We create a rescan request for BUS:0:0, since the card
624a94a63f0SWarner Losh 			 * will be at lun 0.
625a94a63f0SWarner Losh 			 */
626a94a63f0SWarner Losh 			if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
627a94a63f0SWarner Losh 					    /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
628a94a63f0SWarner Losh 				slot_printf(slot, "Unable to create path for rescan\n");
629a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
630a94a63f0SWarner Losh 				xpt_free_ccb(ccb);
631a94a63f0SWarner Losh 				return;
632a94a63f0SWarner Losh 			}
633a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
634a94a63f0SWarner Losh 			xpt_rescan(ccb);
635a94a63f0SWarner Losh #else
636aca38eabSMarius Strobl 			d = slot->dev = device_add_child(slot->bus, "mmc", -1);
637831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
638aca38eabSMarius Strobl 			if (d) {
639aca38eabSMarius Strobl 				device_set_ivars(d, slot);
640aca38eabSMarius Strobl 				(void)device_probe_and_attach(d);
641aca38eabSMarius Strobl 			}
642a94a63f0SWarner Losh #endif
643831f5dcfSAlexander Motin 		} else
644831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
645831f5dcfSAlexander Motin 	} else {
646a94a63f0SWarner Losh #ifdef MMCCAM
647a94a63f0SWarner Losh 		if (slot->card_present == 1) {
648a94a63f0SWarner Losh #else
649831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
650a94a63f0SWarner Losh #endif
651831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
652639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
653639f59f0SIan Lepore 				slot_printf(slot, "Card removed\n");
6547e6ccea3SMarius Strobl 			d = slot->dev;
655831f5dcfSAlexander Motin 			slot->dev = NULL;
656a94a63f0SWarner Losh #ifdef MMCCAM
657a94a63f0SWarner Losh 			slot->card_present = 0;
658a94a63f0SWarner Losh 			union ccb *ccb;
659a94a63f0SWarner Losh 			uint32_t pathid;
660a94a63f0SWarner Losh 			pathid = cam_sim_path(slot->sim);
661a94a63f0SWarner Losh 			ccb = xpt_alloc_ccb_nowait();
662a94a63f0SWarner Losh 			if (ccb == NULL) {
663a94a63f0SWarner Losh 				slot_printf(slot, "Unable to alloc CCB for rescan\n");
664a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
665a94a63f0SWarner Losh 				return;
666a94a63f0SWarner Losh 			}
667a94a63f0SWarner Losh 
668a94a63f0SWarner Losh 			/*
669a94a63f0SWarner Losh 			 * We create a rescan request for BUS:0:0, since the card
670a94a63f0SWarner Losh 			 * will be at lun 0.
671a94a63f0SWarner Losh 			 */
672a94a63f0SWarner Losh 			if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
673a94a63f0SWarner Losh 					    /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
674a94a63f0SWarner Losh 				slot_printf(slot, "Unable to create path for rescan\n");
675a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
676a94a63f0SWarner Losh 				xpt_free_ccb(ccb);
677a94a63f0SWarner Losh 				return;
678a94a63f0SWarner Losh 			}
679a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
680a94a63f0SWarner Losh 			xpt_rescan(ccb);
681a94a63f0SWarner Losh #else
682aca38eabSMarius Strobl 			slot->intmask &= ~sdhci_tuning_intmask(slot);
683cc22204bSMarius Strobl 			WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
684aca38eabSMarius Strobl 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
685aca38eabSMarius Strobl 			slot->opt &= ~SDHCI_TUNING_ENABLED;
686831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
687aca38eabSMarius Strobl 			callout_drain(&slot->retune_callout);
688d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
689a94a63f0SWarner Losh #endif
690831f5dcfSAlexander Motin 		} else
691831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
692831f5dcfSAlexander Motin 	}
693831f5dcfSAlexander Motin }
694831f5dcfSAlexander Motin 
695b8bf08b1SIan Lepore static void
696b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
697639f59f0SIan Lepore {
698639f59f0SIan Lepore 	bool was_present;
699639f59f0SIan Lepore 
700639f59f0SIan Lepore 	/*
701639f59f0SIan Lepore 	 * If there was no card and now there is one, schedule the task to
702639f59f0SIan Lepore 	 * create the child device after a short delay.  The delay is to
703639f59f0SIan Lepore 	 * debounce the card insert (sometimes the card detect pin stabilizes
704639f59f0SIan Lepore 	 * before the other pins have made good contact).
705639f59f0SIan Lepore 	 *
706639f59f0SIan Lepore 	 * If there was a card present and now it's gone, immediately schedule
707639f59f0SIan Lepore 	 * the task to delete the child device.  No debouncing -- gone is gone,
708639f59f0SIan Lepore 	 * because once power is removed, a full card re-init is needed, and
709639f59f0SIan Lepore 	 * that happens by deleting and recreating the child device.
710639f59f0SIan Lepore 	 */
711a94a63f0SWarner Losh #ifdef MMCCAM
712a94a63f0SWarner Losh 	was_present = slot->card_present;
713a94a63f0SWarner Losh #else
714639f59f0SIan Lepore 	was_present = slot->dev != NULL;
715a94a63f0SWarner Losh #endif
716639f59f0SIan Lepore 	if (!was_present && is_present) {
717639f59f0SIan Lepore 		taskqueue_enqueue_timeout(taskqueue_swi_giant,
718639f59f0SIan Lepore 		    &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
719639f59f0SIan Lepore 	} else if (was_present && !is_present) {
720639f59f0SIan Lepore 		taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
721639f59f0SIan Lepore 	}
722b8bf08b1SIan Lepore }
723b8bf08b1SIan Lepore 
724b8bf08b1SIan Lepore void
725b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
726b8bf08b1SIan Lepore {
727b8bf08b1SIan Lepore 
728b8bf08b1SIan Lepore 	SDHCI_LOCK(slot);
729b8bf08b1SIan Lepore 	sdhci_handle_card_present_locked(slot, is_present);
730639f59f0SIan Lepore 	SDHCI_UNLOCK(slot);
731639f59f0SIan Lepore }
732639f59f0SIan Lepore 
733639f59f0SIan Lepore static void
734639f59f0SIan Lepore sdhci_card_poll(void *arg)
735639f59f0SIan Lepore {
736639f59f0SIan Lepore 	struct sdhci_slot *slot = arg;
737639f59f0SIan Lepore 
738639f59f0SIan Lepore 	sdhci_handle_card_present(slot,
739639f59f0SIan Lepore 	    SDHCI_GET_CARD_PRESENT(slot->bus, slot));
740639f59f0SIan Lepore 	callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
741639f59f0SIan Lepore 	    sdhci_card_poll, slot);
742639f59f0SIan Lepore }
743639f59f0SIan Lepore 
744ab00a509SMarius Strobl static int
745ab00a509SMarius Strobl sdhci_dma_alloc(struct sdhci_slot *slot)
746ab00a509SMarius Strobl {
747ab00a509SMarius Strobl 	int err;
748ab00a509SMarius Strobl 
749ab00a509SMarius Strobl 	if (!(slot->quirks & SDHCI_QUIRK_BROKEN_SDMA_BOUNDARY)) {
750ab00a509SMarius Strobl 		if (MAXPHYS <= 1024 * 4)
751ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_4K;
752ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 8)
753ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_8K;
754ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 16)
755ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_16K;
756ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 32)
757ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_32K;
758ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 64)
759ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_64K;
760ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 128)
761ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_128K;
762ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 256)
763ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_256K;
764ab00a509SMarius Strobl 		else
765ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_512K;
766ab00a509SMarius Strobl 	}
767ab00a509SMarius Strobl 	slot->sdma_bbufsz = SDHCI_SDMA_BNDRY_TO_BBUFSZ(slot->sdma_boundary);
768ab00a509SMarius Strobl 
769ab00a509SMarius Strobl 	/*
770ab00a509SMarius Strobl 	 * Allocate the DMA tag for an SDMA bounce buffer.
771ab00a509SMarius Strobl 	 * Note that the SDHCI specification doesn't state any alignment
772ab00a509SMarius Strobl 	 * constraint for the SDMA system address.  However, controllers
773ab00a509SMarius Strobl 	 * typically ignore the SDMA boundary bits in SDHCI_DMA_ADDRESS when
774ab00a509SMarius Strobl 	 * forming the actual address of data, requiring the SDMA buffer to
775ab00a509SMarius Strobl 	 * be aligned to the SDMA boundary.
776ab00a509SMarius Strobl 	 */
777ab00a509SMarius Strobl 	err = bus_dma_tag_create(bus_get_dma_tag(slot->bus), slot->sdma_bbufsz,
778ab00a509SMarius Strobl 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
779ab00a509SMarius Strobl 	    slot->sdma_bbufsz, 1, slot->sdma_bbufsz, BUS_DMA_ALLOCNOW,
780ab00a509SMarius Strobl 	    NULL, NULL, &slot->dmatag);
781ab00a509SMarius Strobl 	if (err != 0) {
782ab00a509SMarius Strobl 		slot_printf(slot, "Can't create DMA tag for SDMA\n");
783ab00a509SMarius Strobl 		return (err);
784ab00a509SMarius Strobl 	}
785ab00a509SMarius Strobl 	/* Allocate DMA memory for the SDMA bounce buffer. */
786ab00a509SMarius Strobl 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
787ab00a509SMarius Strobl 	    BUS_DMA_NOWAIT, &slot->dmamap);
788ab00a509SMarius Strobl 	if (err != 0) {
789ab00a509SMarius Strobl 		slot_printf(slot, "Can't alloc DMA memory for SDMA\n");
790ab00a509SMarius Strobl 		bus_dma_tag_destroy(slot->dmatag);
791ab00a509SMarius Strobl 		return (err);
792ab00a509SMarius Strobl 	}
793ab00a509SMarius Strobl 	/* Map the memory of the SDMA bounce buffer. */
794ab00a509SMarius Strobl 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
795ab00a509SMarius Strobl 	    (void *)slot->dmamem, slot->sdma_bbufsz, sdhci_getaddr,
796ab00a509SMarius Strobl 	    &slot->paddr, 0);
797ab00a509SMarius Strobl 	if (err != 0 || slot->paddr == 0) {
798ab00a509SMarius Strobl 		slot_printf(slot, "Can't load DMA memory for SDMA\n");
799ab00a509SMarius Strobl 		bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
800ab00a509SMarius Strobl 		bus_dma_tag_destroy(slot->dmatag);
801ab00a509SMarius Strobl 		if (err)
802ab00a509SMarius Strobl 			return (err);
803ab00a509SMarius Strobl 		else
804ab00a509SMarius Strobl 			return (EFAULT);
805ab00a509SMarius Strobl 	}
806ab00a509SMarius Strobl 
807ab00a509SMarius Strobl 	return (0);
808ab00a509SMarius Strobl }
809ab00a509SMarius Strobl 
810ab00a509SMarius Strobl static void
811ab00a509SMarius Strobl sdhci_dma_free(struct sdhci_slot *slot)
812ab00a509SMarius Strobl {
813ab00a509SMarius Strobl 
814ab00a509SMarius Strobl 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
815ab00a509SMarius Strobl 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
816ab00a509SMarius Strobl 	bus_dma_tag_destroy(slot->dmatag);
817ab00a509SMarius Strobl }
818ab00a509SMarius Strobl 
819d6b3aaf8SOleksandr Tymoshenko int
820d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
821831f5dcfSAlexander Motin {
822aca38eabSMarius Strobl 	kobjop_desc_t kobj_desc;
823aca38eabSMarius Strobl 	kobj_method_t *kobj_method;
8240f34084fSMarius Strobl 	uint32_t caps, caps2, freq, host_caps;
825d6b3aaf8SOleksandr Tymoshenko 	int err;
826831f5dcfSAlexander Motin 
827831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
828a94a63f0SWarner Losh 
829d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
830d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
831d6b3aaf8SOleksandr Tymoshenko 
832d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
833d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
8340f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) {
8358f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
8360f34084fSMarius Strobl 		caps2 = slot->caps2;
8370f34084fSMarius Strobl 	} else {
838831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
8390f34084fSMarius Strobl 		if (slot->version >= SDHCI_SPEC_300)
8400f34084fSMarius Strobl 			caps2 = RD4(slot, SDHCI_CAPABILITIES2);
8410f34084fSMarius Strobl 		else
8420f34084fSMarius Strobl 			caps2 = 0;
8430f34084fSMarius Strobl 	}
8447fcf4780SMarius Strobl 	if (slot->version >= SDHCI_SPEC_300) {
8457fcf4780SMarius Strobl 		if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE &&
8467fcf4780SMarius Strobl 		    (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) {
847ab00a509SMarius Strobl 			slot_printf(slot,
8487fcf4780SMarius Strobl 			    "Driver doesn't support shared bus slots\n");
8497fcf4780SMarius Strobl 			SDHCI_LOCK_DESTROY(slot);
8507fcf4780SMarius Strobl 			return (ENXIO);
8517fcf4780SMarius Strobl 		} else if ((caps & SDHCI_SLOTTYPE_MASK) ==
8527fcf4780SMarius Strobl 		    SDHCI_SLOTTYPE_EMBEDDED) {
8537fcf4780SMarius Strobl 			slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE;
8547fcf4780SMarius Strobl 		}
8557fcf4780SMarius Strobl 	}
856831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
85733aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
85887a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
85987a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
86033aad34dSOleksandr Tymoshenko 	else
86187a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
86287a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
86387a6a871SIan Lepore 	if (freq != 0)
86487a6a871SIan Lepore 		slot->max_clk = freq * 1000000;
86587a6a871SIan Lepore 	/*
86687a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
86787a6a871SIan Lepore 	 * hasn't already set max_clk we're probably not going to work right
86887a6a871SIan Lepore 	 * with an assumption, so complain about it.
86987a6a871SIan Lepore 	 */
870831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
87187a6a871SIan Lepore 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
872ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't specify base clock "
8731bacf3beSMarius Strobl 		    "frequency, using %dMHz as default.\n",
8741bacf3beSMarius Strobl 		    SDHCI_DEFAULT_MAX_FREQ);
875831f5dcfSAlexander Motin 	}
876a2832f9fSMarius Strobl 	/* Calculate/set timeout clock frequency. */
8778f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
8788f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
879a2832f9fSMarius Strobl 	} else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) {
880a2832f9fSMarius Strobl 		slot->timeout_clk = 1000;
8818f3b7d56SOleksandr Tymoshenko 	} else {
8821bacf3beSMarius Strobl 		slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >>
8831bacf3beSMarius Strobl 		    SDHCI_TIMEOUT_CLK_SHIFT;
8848f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
8858f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
8868f3b7d56SOleksandr Tymoshenko 	}
88787a6a871SIan Lepore 	/*
88887a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
88987a6a871SIan Lepore 	 * hasn't already set timeout_clk we'll probably work okay using the
89087a6a871SIan Lepore 	 * max timeout, but still mention it.
89187a6a871SIan Lepore 	 */
892831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
893ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't specify timeout clock "
894ceb9e9f7SIan Lepore 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
895ceb9e9f7SIan Lepore 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
896831f5dcfSAlexander Motin 	}
897831f5dcfSAlexander Motin 
89857677a3aSOleksandr Tymoshenko 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
899831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
900831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
901831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
902831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
903831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
904831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
9057fcf4780SMarius Strobl 	/* 1.8V VDD is not supposed to be used for removable cards. */
9067fcf4780SMarius Strobl 	if ((caps & SDHCI_CAN_VDD_180) && (slot->opt & SDHCI_SLOT_EMBEDDED))
907831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
908831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
909ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't report any "
910831f5dcfSAlexander Motin 		    "support voltages.\n");
911831f5dcfSAlexander Motin 	}
912aca38eabSMarius Strobl 
9130f34084fSMarius Strobl 	host_caps = MMC_CAP_4_BIT_DATA;
9142d1731b8SIan Lepore 	if (caps & SDHCI_CAN_DO_8BITBUS)
9150f34084fSMarius Strobl 		host_caps |= MMC_CAP_8_BIT_DATA;
916831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
9170f34084fSMarius Strobl 		host_caps |= MMC_CAP_HSPEED;
91872dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC)
9190f34084fSMarius Strobl 		host_caps |= MMC_CAP_BOOT_NOACC;
92072dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY)
9210f34084fSMarius Strobl 		host_caps |= MMC_CAP_WAIT_WHILE_BUSY;
922aca38eabSMarius Strobl 
923aca38eabSMarius Strobl 	/* Determine supported UHS-I and eMMC modes. */
9240f34084fSMarius Strobl 	if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50))
9250f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
9260f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_SDR104) {
9270f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
9280f34084fSMarius Strobl 		if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200))
9290f34084fSMarius Strobl 			host_caps |= MMC_CAP_MMC_HS200;
9300f34084fSMarius Strobl 	} else if (caps2 & SDHCI_CAN_SDR50)
9310f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR50;
9320f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_DDR50 &&
9330f34084fSMarius Strobl 	    !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50))
9340f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_DDR50;
9350f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_DDR52)
9360f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_DDR52;
9370f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 &&
9380f34084fSMarius Strobl 	    caps2 & SDHCI_CAN_MMC_HS400)
9390f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
940835998c2SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 &&
941835998c2SMarius Strobl 	    caps2 & SDHCI_CAN_SDR104)
942835998c2SMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
943aca38eabSMarius Strobl 
944aca38eabSMarius Strobl 	/*
945aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes if the set_uhs_timing method is the
946aca38eabSMarius Strobl 	 * default NULL implementation.
947aca38eabSMarius Strobl 	 */
948aca38eabSMarius Strobl 	kobj_desc = &sdhci_set_uhs_timing_desc;
949aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
950aca38eabSMarius Strobl 	    kobj_desc);
951aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
952aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
953aca38eabSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
954aca38eabSMarius Strobl 		    MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400);
955aca38eabSMarius Strobl 
956aca38eabSMarius Strobl #define	SDHCI_CAP_MODES_TUNING(caps2)					\
957aca38eabSMarius Strobl     (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) |		\
958aca38eabSMarius Strobl     MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 |	\
959aca38eabSMarius Strobl     MMC_CAP_MMC_HS400)
960aca38eabSMarius Strobl 
961aca38eabSMarius Strobl 	/*
962aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes that require (re-)tuning if either
963aca38eabSMarius Strobl 	 * the tune or re-tune method is the default NULL implementation.
964aca38eabSMarius Strobl 	 */
965aca38eabSMarius Strobl 	kobj_desc = &mmcbr_tune_desc;
966aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
967aca38eabSMarius Strobl 	    kobj_desc);
968aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
969aca38eabSMarius Strobl 		goto no_tuning;
970aca38eabSMarius Strobl 	kobj_desc = &mmcbr_retune_desc;
971aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
972aca38eabSMarius Strobl 	    kobj_desc);
973aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt) {
974aca38eabSMarius Strobl no_tuning:
975aca38eabSMarius Strobl 		host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2));
976aca38eabSMarius Strobl 	}
977aca38eabSMarius Strobl 
978aca38eabSMarius Strobl 	/* Allocate tuning structures and determine tuning parameters. */
979aca38eabSMarius Strobl 	if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) {
980aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_SUPPORTED;
981aca38eabSMarius Strobl 		slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF,
982aca38eabSMarius Strobl 		    M_WAITOK);
983aca38eabSMarius Strobl 		slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF,
984aca38eabSMarius Strobl 		    M_WAITOK);
985aca38eabSMarius Strobl 		slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF,
986aca38eabSMarius Strobl 		    M_WAITOK);
987aca38eabSMarius Strobl 		if (caps2 & SDHCI_TUNE_SDR50)
988aca38eabSMarius Strobl 			slot->opt |= SDHCI_SDR50_NEEDS_TUNING;
989aca38eabSMarius Strobl 		slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >>
990aca38eabSMarius Strobl 		    SDHCI_RETUNE_MODES_SHIFT;
991aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_1) {
992aca38eabSMarius Strobl 			slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >>
993aca38eabSMarius Strobl 			    SDHCI_RETUNE_CNT_SHIFT;
994aca38eabSMarius Strobl 			if (slot->retune_count > 0xb) {
995ab00a509SMarius Strobl 				slot_printf(slot, "Unknown re-tuning count "
996aca38eabSMarius Strobl 				    "%x, using 1 sec\n", slot->retune_count);
997aca38eabSMarius Strobl 				slot->retune_count = 1;
998aca38eabSMarius Strobl 			} else if (slot->retune_count != 0)
999aca38eabSMarius Strobl 				slot->retune_count =
1000aca38eabSMarius Strobl 				    1 << (slot->retune_count - 1);
1001aca38eabSMarius Strobl 		}
1002aca38eabSMarius Strobl 	}
1003aca38eabSMarius Strobl 
1004aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING
1005aca38eabSMarius Strobl 
1006aca38eabSMarius Strobl 	/* Determine supported VCCQ signaling levels. */
10070f34084fSMarius Strobl 	host_caps |= MMC_CAP_SIGNALING_330;
10080f34084fSMarius Strobl 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
1009aca38eabSMarius Strobl 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
10100f34084fSMarius Strobl 	    MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 |
10110f34084fSMarius Strobl 	    MMC_CAP_MMC_HS400_180))
1012aca38eabSMarius Strobl 		host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180;
1013aca38eabSMarius Strobl 
1014aca38eabSMarius Strobl 	/*
1015aca38eabSMarius Strobl 	 * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the
1016aca38eabSMarius Strobl 	 * default NULL implementation.  Disable 1.2 V support if it's the
1017aca38eabSMarius Strobl 	 * generic SDHCI implementation.
1018aca38eabSMarius Strobl 	 */
1019aca38eabSMarius Strobl 	kobj_desc = &mmcbr_switch_vccq_desc;
1020aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
1021aca38eabSMarius Strobl 	    kobj_desc);
1022aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
1023aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180);
1024aca38eabSMarius Strobl 	else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq)
1025aca38eabSMarius Strobl 		host_caps &= ~MMC_CAP_SIGNALING_120;
1026aca38eabSMarius Strobl 
1027aca38eabSMarius Strobl 	/* Determine supported driver types (type B is always mandatory). */
1028f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_A)
10290f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_A;
1030f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_C)
10310f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_C;
1032f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_D)
10330f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_D;
10340f34084fSMarius Strobl 	slot->host.caps = host_caps;
10350f34084fSMarius Strobl 
1036831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
1037831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
1038831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
1039d6b3aaf8SOleksandr Tymoshenko 
1040d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
1041831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
1042d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
1043831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
1044a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE)
1045a2832f9fSMarius Strobl 		slot->opt |= SDHCI_NON_REMOVABLE;
1046831f5dcfSAlexander Motin 
1047c3a0f75aSOleksandr Tymoshenko 	/*
1048c3a0f75aSOleksandr Tymoshenko 	 * Use platform-provided transfer backend
1049c3a0f75aSOleksandr Tymoshenko 	 * with PIO as a fallback mechanism
1050c3a0f75aSOleksandr Tymoshenko 	 */
1051c3a0f75aSOleksandr Tymoshenko 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
1052c3a0f75aSOleksandr Tymoshenko 		slot->opt &= ~SDHCI_HAVE_DMA;
1053c3a0f75aSOleksandr Tymoshenko 
1054ab00a509SMarius Strobl 	if (slot->opt & SDHCI_HAVE_DMA) {
1055ab00a509SMarius Strobl 		err = sdhci_dma_alloc(slot);
1056ab00a509SMarius Strobl 		if (err != 0) {
1057ab00a509SMarius Strobl 			if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1058ab00a509SMarius Strobl 				free(slot->tune_req, M_DEVBUF);
1059ab00a509SMarius Strobl 				free(slot->tune_cmd, M_DEVBUF);
1060ab00a509SMarius Strobl 				free(slot->tune_data, M_DEVBUF);
1061ab00a509SMarius Strobl 			}
1062ab00a509SMarius Strobl 			SDHCI_LOCK_DESTROY(slot);
1063ab00a509SMarius Strobl 			return (err);
1064ab00a509SMarius Strobl 		}
1065ab00a509SMarius Strobl 	}
1066ab00a509SMarius Strobl 
10675b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
10680f34084fSMarius Strobl 		slot_printf(slot,
10697fcf4780SMarius Strobl 		    "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n",
1070831f5dcfSAlexander Motin 		    slot->max_clk / 1000000,
1071831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
10720f34084fSMarius Strobl 		    (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
10730f34084fSMarius Strobl 			((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
1074831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
1075831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
10767fcf4780SMarius Strobl 		    ((caps & SDHCI_CAN_VDD_180) &&
10777fcf4780SMarius Strobl 		    (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "",
10780f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "",
10790f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "",
1080aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "",
1081aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "",
1082aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "",
10837fcf4780SMarius Strobl 		    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO",
10847fcf4780SMarius Strobl 		    (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" :
10857fcf4780SMarius Strobl 		    (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" :
10867fcf4780SMarius Strobl 		    "removable");
10870f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 |
10880f34084fSMarius Strobl 		    MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE))
10890f34084fSMarius Strobl 			slot_printf(slot, "eMMC:%s%s%s%s\n",
10900f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "",
10910f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "",
10920f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "",
10930f34084fSMarius Strobl 			    ((host_caps &
10940f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ==
10950f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ?
10960f34084fSMarius Strobl 			    " HS400ES" : "");
10970f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
10980f34084fSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104))
10990f34084fSMarius Strobl 			slot_printf(slot, "UHS-I:%s%s%s%s%s\n",
11000f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "",
11010f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "",
11020f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "",
11030f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "",
11040f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : "");
1105aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_SUPPORTED)
1106aca38eabSMarius Strobl 			slot_printf(slot, "Re-tuning count %d secs, mode %d\n",
1107aca38eabSMarius Strobl 			    slot->retune_count, slot->retune_mode + 1);
1108831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1109831f5dcfSAlexander Motin 	}
1110831f5dcfSAlexander Motin 
1111ba6fc1c7SLuiz Otavio O Souza 	slot->timeout = 10;
1112ba6fc1c7SLuiz Otavio O Souza 	SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
1113ba6fc1c7SLuiz Otavio O Souza 	    SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
1114ba6fc1c7SLuiz Otavio O Souza 	    "timeout", CTLFLAG_RW, &slot->timeout, 0,
1115ba6fc1c7SLuiz Otavio O Souza 	    "Maximum timeout for SDHCI transfers (in secs)");
1116831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
1117639f59f0SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
1118639f59f0SIan Lepore 		sdhci_card_task, slot);
1119639f59f0SIan Lepore 	callout_init(&slot->card_poll_callout, 1);
1120e64f01a9SIan Lepore 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
1121aca38eabSMarius Strobl 	callout_init_mtx(&slot->retune_callout, &slot->mtx, 0);
1122ba6fc1c7SLuiz Otavio O Souza 
1123639f59f0SIan Lepore 	if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
1124639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
1125639f59f0SIan Lepore 		callout_reset(&slot->card_poll_callout,
1126639f59f0SIan Lepore 		    SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
1127639f59f0SIan Lepore 	}
1128639f59f0SIan Lepore 
1129aca38eabSMarius Strobl 	sdhci_init(slot);
1130aca38eabSMarius Strobl 
1131831f5dcfSAlexander Motin 	return (0);
1132831f5dcfSAlexander Motin }
1133831f5dcfSAlexander Motin 
1134d91f1a10SIlya Bakulin #ifndef MMCCAM
1135d6b3aaf8SOleksandr Tymoshenko void
1136d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
1137831f5dcfSAlexander Motin {
11387e6ccea3SMarius Strobl 
1139d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
1140d6b3aaf8SOleksandr Tymoshenko }
1141d91f1a10SIlya Bakulin #endif
1142831f5dcfSAlexander Motin 
1143d6b3aaf8SOleksandr Tymoshenko int
1144d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
1145d6b3aaf8SOleksandr Tymoshenko {
1146831f5dcfSAlexander Motin 	device_t d;
1147831f5dcfSAlexander Motin 
1148e64f01a9SIan Lepore 	callout_drain(&slot->timeout_callout);
1149639f59f0SIan Lepore 	callout_drain(&slot->card_poll_callout);
1150aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1151831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
1152639f59f0SIan Lepore 	taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
1153831f5dcfSAlexander Motin 
1154831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1155831f5dcfSAlexander Motin 	d = slot->dev;
1156831f5dcfSAlexander Motin 	slot->dev = NULL;
1157831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1158831f5dcfSAlexander Motin 	if (d != NULL)
1159d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
1160831f5dcfSAlexander Motin 
1161831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1162831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
1163831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1164ab00a509SMarius Strobl 	if (slot->opt & SDHCI_HAVE_DMA)
1165ab00a509SMarius Strobl 		sdhci_dma_free(slot);
1166aca38eabSMarius Strobl 	if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1167aca38eabSMarius Strobl 		free(slot->tune_req, M_DEVBUF);
1168aca38eabSMarius Strobl 		free(slot->tune_cmd, M_DEVBUF);
1169aca38eabSMarius Strobl 		free(slot->tune_data, M_DEVBUF);
1170aca38eabSMarius Strobl 	}
1171d6b3aaf8SOleksandr Tymoshenko 
1172831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
1173d6b3aaf8SOleksandr Tymoshenko 
1174831f5dcfSAlexander Motin 	return (0);
1175831f5dcfSAlexander Motin }
1176831f5dcfSAlexander Motin 
1177d6b3aaf8SOleksandr Tymoshenko int
1178d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
117992bf0e27SAlexander Motin {
11807e6ccea3SMarius Strobl 
1181aca38eabSMarius Strobl 	/*
1182aca38eabSMarius Strobl 	 * We expect the MMC layer to issue initial tuning after resume.
1183aca38eabSMarius Strobl 	 * Otherwise, we'd need to indicate re-tuning including circuit reset
1184aca38eabSMarius Strobl 	 * being required at least for re-tuning modes 1 and 2 ourselves.
1185aca38eabSMarius Strobl 	 */
1186aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1187aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1188aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1189d6b3aaf8SOleksandr Tymoshenko 	sdhci_reset(slot, SDHCI_RESET_ALL);
1190aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
119192bf0e27SAlexander Motin 
119292bf0e27SAlexander Motin 	return (0);
119392bf0e27SAlexander Motin }
119492bf0e27SAlexander Motin 
1195d6b3aaf8SOleksandr Tymoshenko int
1196d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
119792bf0e27SAlexander Motin {
11987e6ccea3SMarius Strobl 
1199aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1200d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
1201aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
120292bf0e27SAlexander Motin 
1203d6b3aaf8SOleksandr Tymoshenko 	return (0);
120492bf0e27SAlexander Motin }
120592bf0e27SAlexander Motin 
120657677a3aSOleksandr Tymoshenko uint32_t
1207b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot)
120857677a3aSOleksandr Tymoshenko {
12097e6ccea3SMarius Strobl 
121057677a3aSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
121157677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
121257677a3aSOleksandr Tymoshenko 	else
121357677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
121457677a3aSOleksandr Tymoshenko }
121557677a3aSOleksandr Tymoshenko 
12166e37fb2bSIan Lepore bool
1217b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot)
12186e37fb2bSIan Lepore {
12196e37fb2bSIan Lepore 
1220639f59f0SIan Lepore 	if (slot->opt & SDHCI_NON_REMOVABLE)
1221639f59f0SIan Lepore 		return true;
1222639f59f0SIan Lepore 
12236e37fb2bSIan Lepore 	return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
12246e37fb2bSIan Lepore }
12256e37fb2bSIan Lepore 
12260f34084fSMarius Strobl void
12270f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot)
12280f34084fSMarius Strobl {
1229ab00a509SMarius Strobl 	const struct mmc_ios *ios;
12300f34084fSMarius Strobl 	uint16_t hostctrl2;
12310f34084fSMarius Strobl 
12320f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
12330f34084fSMarius Strobl 		return;
12340f34084fSMarius Strobl 
1235aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
12360f34084fSMarius Strobl 	ios = &slot->host.ios;
12370f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
12380f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
12390f34084fSMarius Strobl 	hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK;
1240aca38eabSMarius Strobl 	if (ios->clock > SD_SDR50_MAX) {
12410f34084fSMarius Strobl 		if (ios->timing == bus_timing_mmc_hs400 ||
12420f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_hs400es)
12430f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_MMC_HS400;
1244aca38eabSMarius Strobl 		else
12450f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR104;
1246aca38eabSMarius Strobl 	}
12470f34084fSMarius Strobl 	else if (ios->clock > SD_SDR25_MAX)
12480f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR50;
12490f34084fSMarius Strobl 	else if (ios->clock > SD_SDR12_MAX) {
12500f34084fSMarius Strobl 		if (ios->timing == bus_timing_uhs_ddr50 ||
12510f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_ddr52)
12520f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_DDR50;
12530f34084fSMarius Strobl 		else
12540f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR25;
12550f34084fSMarius Strobl 	} else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY)
12560f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR12;
12570f34084fSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
12580f34084fSMarius Strobl 	sdhci_set_clock(slot, ios->clock);
12590f34084fSMarius Strobl }
12600f34084fSMarius Strobl 
1261d6b3aaf8SOleksandr Tymoshenko int
1262d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
1263831f5dcfSAlexander Motin {
1264831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1265831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
1266831f5dcfSAlexander Motin 
1267831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1268831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
1269831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
1270831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
1271831f5dcfSAlexander Motin 		sdhci_init(slot);
1272831f5dcfSAlexander Motin 	}
1273831f5dcfSAlexander Motin 	/* Configure the bus. */
1274831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
1275831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
12762d1731b8SIan Lepore 	if (ios->bus_width == bus_width_8) {
12772d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
1278831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
12792d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_4) {
12802d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
12812d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
12822d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_1) {
12832d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
12842d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
12852d1731b8SIan Lepore 	} else {
12862d1731b8SIan Lepore 		panic("Invalid bus width: %d", ios->bus_width);
12872d1731b8SIan Lepore 	}
12880f34084fSMarius Strobl 	if (ios->clock > SD_SDR12_MAX &&
1289bba987dcSIan Lepore 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
1290831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
1291831f5dcfSAlexander Motin 	else
1292831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
1293831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
12940f34084fSMarius Strobl 	SDHCI_SET_UHS_TIMING(brdev, slot);
1295831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
1296d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
1297831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1298831f5dcfSAlexander Motin 
1299831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1300831f5dcfSAlexander Motin 	return (0);
1301831f5dcfSAlexander Motin }
1302831f5dcfSAlexander Motin 
13030f34084fSMarius Strobl int
13040f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev)
13050f34084fSMarius Strobl {
13060f34084fSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
13070f34084fSMarius Strobl 	enum mmc_vccq vccq;
13080f34084fSMarius Strobl 	int err;
13090f34084fSMarius Strobl 	uint16_t hostctrl2;
13100f34084fSMarius Strobl 
13110f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
13120f34084fSMarius Strobl 		return (0);
13130f34084fSMarius Strobl 
13140f34084fSMarius Strobl 	err = 0;
13150f34084fSMarius Strobl 	vccq = slot->host.ios.vccq;
13160f34084fSMarius Strobl 	SDHCI_LOCK(slot);
13170f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
13180f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
13190f34084fSMarius Strobl 	switch (vccq) {
13200f34084fSMarius Strobl 	case vccq_330:
13210f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
13220f34084fSMarius Strobl 			goto done;
13230f34084fSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE;
13240f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
13250f34084fSMarius Strobl 		DELAY(5000);
13260f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
13270f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
13280f34084fSMarius Strobl 			goto done;
13290f34084fSMarius Strobl 		err = EAGAIN;
13300f34084fSMarius Strobl 		break;
13310f34084fSMarius Strobl 	case vccq_180:
13320f34084fSMarius Strobl 		if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) {
13330f34084fSMarius Strobl 			err = EINVAL;
13340f34084fSMarius Strobl 			goto done;
13350f34084fSMarius Strobl 		}
13360f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
13370f34084fSMarius Strobl 			goto done;
13380f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_S18_ENABLE;
13390f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
13400f34084fSMarius Strobl 		DELAY(5000);
13410f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
13420f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
13430f34084fSMarius Strobl 			goto done;
13440f34084fSMarius Strobl 		err = EAGAIN;
13450f34084fSMarius Strobl 		break;
13460f34084fSMarius Strobl 	default:
13470f34084fSMarius Strobl 		slot_printf(slot,
13480f34084fSMarius Strobl 		    "Attempt to set unsupported signaling voltage\n");
13490f34084fSMarius Strobl 		err = EINVAL;
13500f34084fSMarius Strobl 		break;
13510f34084fSMarius Strobl 	}
13520f34084fSMarius Strobl done:
13530f34084fSMarius Strobl 	sdhci_set_clock(slot, slot->host.ios.clock);
13540f34084fSMarius Strobl 	SDHCI_UNLOCK(slot);
13550f34084fSMarius Strobl 	return (err);
13560f34084fSMarius Strobl }
13570f34084fSMarius Strobl 
1358aca38eabSMarius Strobl int
1359aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400)
1360aca38eabSMarius Strobl {
1361aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1362ab00a509SMarius Strobl 	const struct mmc_ios *ios = &slot->host.ios;
1363aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1364aca38eabSMarius Strobl 	struct mmc_data *tune_data;
1365aca38eabSMarius Strobl 	uint32_t opcode;
1366aca38eabSMarius Strobl 	int err;
1367aca38eabSMarius Strobl 
1368aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_SUPPORTED))
1369aca38eabSMarius Strobl 		return (0);
1370aca38eabSMarius Strobl 
1371aca38eabSMarius Strobl 	slot->retune_ticks = slot->retune_count * hz;
1372aca38eabSMarius Strobl 	opcode = MMC_SEND_TUNING_BLOCK;
1373aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1374aca38eabSMarius Strobl 	switch (ios->timing) {
1375aca38eabSMarius Strobl 	case bus_timing_mmc_hs400:
1376aca38eabSMarius Strobl 		slot_printf(slot, "HS400 must be tuned in HS200 mode\n");
1377aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1378aca38eabSMarius Strobl 		return (EINVAL);
1379aca38eabSMarius Strobl 	case bus_timing_mmc_hs200:
1380aca38eabSMarius Strobl 		/*
1381aca38eabSMarius Strobl 		 * In HS400 mode, controllers use the data strobe line to
1382aca38eabSMarius Strobl 		 * latch data from the devices so periodic re-tuning isn't
1383aca38eabSMarius Strobl 		 * expected to be required.
1384aca38eabSMarius Strobl 		 */
1385aca38eabSMarius Strobl 		if (hs400)
1386aca38eabSMarius Strobl 			slot->retune_ticks = 0;
1387aca38eabSMarius Strobl 		opcode = MMC_SEND_TUNING_BLOCK_HS200;
1388aca38eabSMarius Strobl 		break;
1389aca38eabSMarius Strobl 	case bus_timing_uhs_ddr50:
1390aca38eabSMarius Strobl 	case bus_timing_uhs_sdr104:
1391aca38eabSMarius Strobl 		break;
1392aca38eabSMarius Strobl 	case bus_timing_uhs_sdr50:
1393aca38eabSMarius Strobl 		if (slot->opt & SDHCI_SDR50_NEEDS_TUNING)
1394aca38eabSMarius Strobl 			break;
1395aca38eabSMarius Strobl 		/* FALLTHROUGH */
1396aca38eabSMarius Strobl 	default:
1397aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1398aca38eabSMarius Strobl 		return (0);
1399aca38eabSMarius Strobl 	}
1400aca38eabSMarius Strobl 
1401aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1402aca38eabSMarius Strobl 	memset(tune_cmd, 0, sizeof(*tune_cmd));
1403aca38eabSMarius Strobl 	tune_cmd->opcode = opcode;
1404aca38eabSMarius Strobl 	tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1405aca38eabSMarius Strobl 	tune_data = tune_cmd->data = slot->tune_data;
1406aca38eabSMarius Strobl 	memset(tune_data, 0, sizeof(*tune_data));
1407aca38eabSMarius Strobl 	tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
1408aca38eabSMarius Strobl 	    ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 :
1409aca38eabSMarius Strobl 	    MMC_TUNING_LEN;
1410aca38eabSMarius Strobl 	tune_data->flags = MMC_DATA_READ;
1411aca38eabSMarius Strobl 	tune_data->mrq = tune_cmd->mrq = slot->tune_req;
1412aca38eabSMarius Strobl 
1413aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1414aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, true);
1415aca38eabSMarius Strobl 	if (err == 0) {
1416aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_ENABLED;
1417aca38eabSMarius Strobl 		slot->intmask |= sdhci_tuning_intmask(slot);
1418cc22204bSMarius Strobl 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
1419aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1420aca38eabSMarius Strobl 		if (slot->retune_ticks) {
1421aca38eabSMarius Strobl 			callout_reset(&slot->retune_callout, slot->retune_ticks,
1422aca38eabSMarius Strobl 			    sdhci_retune, slot);
1423aca38eabSMarius Strobl 		}
1424aca38eabSMarius Strobl 	}
1425aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1426aca38eabSMarius Strobl 	return (err);
1427aca38eabSMarius Strobl }
1428aca38eabSMarius Strobl 
1429aca38eabSMarius Strobl int
1430aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset)
1431aca38eabSMarius Strobl {
1432aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1433aca38eabSMarius Strobl 	int err;
1434aca38eabSMarius Strobl 
1435aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_ENABLED))
1436aca38eabSMarius Strobl 		return (0);
1437aca38eabSMarius Strobl 
1438aca38eabSMarius Strobl 	/* HS400 must be tuned in HS200 mode. */
1439aca38eabSMarius Strobl 	if (slot->host.ios.timing == bus_timing_mmc_hs400)
1440aca38eabSMarius Strobl 		return (EINVAL);
1441aca38eabSMarius Strobl 
1442aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1443aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, reset);
1444aca38eabSMarius Strobl 	/*
1445aca38eabSMarius Strobl 	 * There are two ways sdhci_exec_tuning() can fail:
1446aca38eabSMarius Strobl 	 * EBUSY should not actually happen when requests are only issued
1447aca38eabSMarius Strobl 	 *	 with the host properly acquired, and
1448aca38eabSMarius Strobl 	 * EIO   re-tuning failed (but it did work initially).
1449aca38eabSMarius Strobl 	 *
1450aca38eabSMarius Strobl 	 * In both cases, we should retry at later point if periodic re-tuning
1451aca38eabSMarius Strobl 	 * is enabled.  Note that due to slot->retune_req not being cleared in
1452aca38eabSMarius Strobl 	 * these failure cases, the MMC layer should trigger another attempt at
1453aca38eabSMarius Strobl 	 * re-tuning with the next request anyway, though.
1454aca38eabSMarius Strobl 	 */
1455aca38eabSMarius Strobl 	if (slot->retune_ticks) {
1456aca38eabSMarius Strobl 		callout_reset(&slot->retune_callout, slot->retune_ticks,
1457aca38eabSMarius Strobl 		    sdhci_retune, slot);
1458aca38eabSMarius Strobl 	}
1459aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1460aca38eabSMarius Strobl 	return (err);
1461aca38eabSMarius Strobl }
1462aca38eabSMarius Strobl 
1463aca38eabSMarius Strobl static int
1464aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset)
1465aca38eabSMarius Strobl {
1466aca38eabSMarius Strobl 	struct mmc_request *tune_req;
1467aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1468aca38eabSMarius Strobl 	int i;
1469aca38eabSMarius Strobl 	uint32_t intmask;
1470aca38eabSMarius Strobl 	uint16_t hostctrl2;
1471aca38eabSMarius Strobl 	u_char opt;
1472aca38eabSMarius Strobl 
1473aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
1474aca38eabSMarius Strobl 	if (slot->req != NULL)
1475aca38eabSMarius Strobl 		return (EBUSY);
1476aca38eabSMarius Strobl 
1477aca38eabSMarius Strobl 	/* Tuning doesn't work with DMA enabled. */
1478aca38eabSMarius Strobl 	opt = slot->opt;
1479aca38eabSMarius Strobl 	slot->opt = opt & ~SDHCI_HAVE_DMA;
1480aca38eabSMarius Strobl 
1481aca38eabSMarius Strobl 	/*
1482aca38eabSMarius Strobl 	 * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only
1483aca38eabSMarius Strobl 	 * kind of interrupt we receive in response to a tuning request.
1484aca38eabSMarius Strobl 	 */
1485aca38eabSMarius Strobl 	intmask = slot->intmask;
1486aca38eabSMarius Strobl 	slot->intmask = SDHCI_INT_DATA_AVAIL;
1487cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL);
1488aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL);
1489aca38eabSMarius Strobl 
1490aca38eabSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1491aca38eabSMarius Strobl 	if (reset)
1492aca38eabSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK;
1493aca38eabSMarius Strobl 	else
1494aca38eabSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK;
1495aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING);
1496aca38eabSMarius Strobl 
1497aca38eabSMarius Strobl 	tune_req = slot->tune_req;
1498aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1499aca38eabSMarius Strobl 	for (i = 0; i < MMC_TUNING_MAX; i++) {
1500aca38eabSMarius Strobl 		memset(tune_req, 0, sizeof(*tune_req));
1501aca38eabSMarius Strobl 		tune_req->cmd = tune_cmd;
1502aca38eabSMarius Strobl 		tune_req->done = sdhci_req_wakeup;
1503aca38eabSMarius Strobl 		tune_req->done_data = slot;
1504aca38eabSMarius Strobl 		slot->req = tune_req;
1505aca38eabSMarius Strobl 		slot->flags = 0;
1506aca38eabSMarius Strobl 		sdhci_start(slot);
1507aca38eabSMarius Strobl 		while (!(tune_req->flags & MMC_REQ_DONE))
1508aca38eabSMarius Strobl 			msleep(tune_req, &slot->mtx, 0, "sdhciet", 0);
1509aca38eabSMarius Strobl 		if (!(tune_req->flags & MMC_TUNE_DONE))
1510aca38eabSMarius Strobl 			break;
1511aca38eabSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1512aca38eabSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING))
1513aca38eabSMarius Strobl 			break;
1514aca38eabSMarius Strobl 		if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK)
1515aca38eabSMarius Strobl 			DELAY(1000);
1516aca38eabSMarius Strobl 	}
1517aca38eabSMarius Strobl 
151878f8baa8SMarius Strobl 	/*
151978f8baa8SMarius Strobl 	 * Restore DMA usage and interrupts.
152078f8baa8SMarius Strobl 	 * Note that the interrupt aggregation code might have cleared
152178f8baa8SMarius Strobl 	 * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask
152278f8baa8SMarius Strobl 	 * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE
152378f8baa8SMarius Strobl 	 * doesn't lose these.
152478f8baa8SMarius Strobl 	 */
1525aca38eabSMarius Strobl 	slot->opt = opt;
1526aca38eabSMarius Strobl 	slot->intmask = intmask;
152778f8baa8SMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END |
152878f8baa8SMarius Strobl 	    SDHCI_INT_RESPONSE);
1529aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, intmask);
1530aca38eabSMarius Strobl 
1531aca38eabSMarius Strobl 	if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING |
1532aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) {
1533aca38eabSMarius Strobl 		slot->retune_req = 0;
1534aca38eabSMarius Strobl 		return (0);
1535aca38eabSMarius Strobl 	}
1536aca38eabSMarius Strobl 
1537aca38eabSMarius Strobl 	slot_printf(slot, "Tuning failed, using fixed sampling clock\n");
1538aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING |
1539aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK));
1540aca38eabSMarius Strobl 	sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1541aca38eabSMarius Strobl 	return (EIO);
1542aca38eabSMarius Strobl }
1543aca38eabSMarius Strobl 
1544aca38eabSMarius Strobl static void
1545aca38eabSMarius Strobl sdhci_retune(void *arg)
1546aca38eabSMarius Strobl {
1547aca38eabSMarius Strobl 	struct sdhci_slot *slot = arg;
1548aca38eabSMarius Strobl 
1549aca38eabSMarius Strobl 	slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
1550aca38eabSMarius Strobl }
1551aca38eabSMarius Strobl 
1552a94a63f0SWarner Losh #ifdef MMCCAM
1553a94a63f0SWarner Losh static void
1554a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot)
1555a94a63f0SWarner Losh {
1556a94a63f0SWarner Losh 	union ccb *ccb;
155715c440e1SWarner Losh 
1558aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
155915c440e1SWarner Losh 		slot_printf(slot, "%s\n", __func__);
1560a94a63f0SWarner Losh 	if (slot->ccb != NULL && slot->curcmd != NULL) {
1561a94a63f0SWarner Losh 		callout_stop(&slot->timeout_callout);
1562a94a63f0SWarner Losh 		ccb = slot->ccb;
1563a94a63f0SWarner Losh 		slot->ccb = NULL;
1564a94a63f0SWarner Losh 		slot->curcmd = NULL;
1565a94a63f0SWarner Losh 
1566a94a63f0SWarner Losh 		/* Tell CAM the request is finished */
1567a94a63f0SWarner Losh 		struct ccb_mmcio *mmcio;
1568a94a63f0SWarner Losh 		mmcio = &ccb->mmcio;
1569a94a63f0SWarner Losh 
1570a94a63f0SWarner Losh 		ccb->ccb_h.status =
1571a94a63f0SWarner Losh 		    (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR);
1572a94a63f0SWarner Losh 		xpt_done(ccb);
1573a94a63f0SWarner Losh 	}
1574a94a63f0SWarner Losh }
1575a94a63f0SWarner Losh #else
1576831f5dcfSAlexander Motin static void
1577e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot)
1578e64f01a9SIan Lepore {
1579e64f01a9SIan Lepore 	struct mmc_request *req;
1580e64f01a9SIan Lepore 
1581e64f01a9SIan Lepore 	if (slot->req != NULL && slot->curcmd != NULL) {
1582e64f01a9SIan Lepore 		callout_stop(&slot->timeout_callout);
1583e64f01a9SIan Lepore 		req = slot->req;
1584e64f01a9SIan Lepore 		slot->req = NULL;
1585e64f01a9SIan Lepore 		slot->curcmd = NULL;
1586e64f01a9SIan Lepore 		req->done(req);
1587e64f01a9SIan Lepore 	}
1588e64f01a9SIan Lepore }
1589a94a63f0SWarner Losh #endif
1590e64f01a9SIan Lepore 
1591e64f01a9SIan Lepore static void
1592aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req)
1593aca38eabSMarius Strobl {
1594aca38eabSMarius Strobl 	struct sdhci_slot *slot;
1595aca38eabSMarius Strobl 
1596aca38eabSMarius Strobl 	slot = req->done_data;
1597aca38eabSMarius Strobl 	req->flags |= MMC_REQ_DONE;
1598aca38eabSMarius Strobl 	wakeup(req);
1599aca38eabSMarius Strobl }
1600aca38eabSMarius Strobl 
1601aca38eabSMarius Strobl static void
1602e64f01a9SIan Lepore sdhci_timeout(void *arg)
1603e64f01a9SIan Lepore {
1604e64f01a9SIan Lepore 	struct sdhci_slot *slot = arg;
1605e64f01a9SIan Lepore 
1606e64f01a9SIan Lepore 	if (slot->curcmd != NULL) {
16077e586643SIan Lepore 		slot_printf(slot, "Controller timeout\n");
16087e586643SIan Lepore 		sdhci_dumpregs(slot);
1609a6873fd1SIan Lepore 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1610e64f01a9SIan Lepore 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1611e64f01a9SIan Lepore 		sdhci_req_done(slot);
16127e586643SIan Lepore 	} else {
16137e586643SIan Lepore 		slot_printf(slot, "Spurious timeout - no active command\n");
1614e64f01a9SIan Lepore 	}
1615e64f01a9SIan Lepore }
1616e64f01a9SIan Lepore 
1617e64f01a9SIan Lepore static void
1618ab00a509SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data)
1619831f5dcfSAlexander Motin {
1620831f5dcfSAlexander Motin 	uint16_t mode;
1621831f5dcfSAlexander Motin 
1622831f5dcfSAlexander Motin 	if (data == NULL)
1623831f5dcfSAlexander Motin 		return;
1624831f5dcfSAlexander Motin 
1625831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
16266dea80e6SMarius Strobl 	if (data->len > 512) {
1627831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
16286dea80e6SMarius Strobl 		if (__predict_true(
16296dea80e6SMarius Strobl #ifdef MMCCAM
16306dea80e6SMarius Strobl 		    slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION &&
16316dea80e6SMarius Strobl #else
16320519c933SMarius Strobl 		    slot->req->stop != NULL &&
16336dea80e6SMarius Strobl #endif
16346dea80e6SMarius Strobl 		    !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)))
16356dea80e6SMarius Strobl 			mode |= SDHCI_TRNS_ACMD12;
16366dea80e6SMarius Strobl 	}
1637831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
1638831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
1639831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
1640831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
1641831f5dcfSAlexander Motin 
1642831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
1643831f5dcfSAlexander Motin }
1644831f5dcfSAlexander Motin 
1645831f5dcfSAlexander Motin static void
1646831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
1647831f5dcfSAlexander Motin {
1648831f5dcfSAlexander Motin 	int flags, timeout;
164990993663SIan Lepore 	uint32_t mask;
1650831f5dcfSAlexander Motin 
1651831f5dcfSAlexander Motin 	slot->curcmd = cmd;
1652831f5dcfSAlexander Motin 	slot->cmd_done = 0;
1653831f5dcfSAlexander Motin 
1654831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
1655831f5dcfSAlexander Motin 
1656831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
1657831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1658831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
1659831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1660e64f01a9SIan Lepore 		sdhci_req_done(slot);
1661831f5dcfSAlexander Motin 		return;
1662831f5dcfSAlexander Motin 	}
1663831f5dcfSAlexander Motin 
1664b440e965SMarius Strobl 	/*
1665b440e965SMarius Strobl 	 * Do not issue command if there is no card, clock or power.
1666b440e965SMarius Strobl 	 * Controller will not detect timeout without clock active.
1667b440e965SMarius Strobl 	 */
16686e37fb2bSIan Lepore 	if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
1669d8208d9eSAlexander Motin 	    slot->power == 0 ||
1670d8208d9eSAlexander Motin 	    slot->clock == 0) {
1671a94a63f0SWarner Losh 		slot_printf(slot,
1672a94a63f0SWarner Losh 			    "Cannot issue a command (power=%d clock=%d)",
1673a94a63f0SWarner Losh 			    slot->power, slot->clock);
1674831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1675e64f01a9SIan Lepore 		sdhci_req_done(slot);
1676831f5dcfSAlexander Motin 		return;
1677831f5dcfSAlexander Motin 	}
1678831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
1679831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
1680831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
1681a94a63f0SWarner Losh 	if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY))
1682831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
1683aca38eabSMarius Strobl 	/*
1684aca38eabSMarius Strobl 	 * We shouldn't wait for DAT for stop commands or CMD19/CMD21.  Note
1685aca38eabSMarius Strobl 	 * that these latter are also special in that SDHCI_CMD_DATA should
1686aca38eabSMarius Strobl 	 * be set below but no actual data is ever read from the controller.
1687aca38eabSMarius Strobl 	*/
1688a94a63f0SWarner Losh #ifdef MMCCAM
1689aca38eabSMarius Strobl 	if (cmd == &slot->ccb->mmcio.stop ||
1690a94a63f0SWarner Losh #else
1691aca38eabSMarius Strobl 	if (cmd == slot->req->stop ||
1692a94a63f0SWarner Losh #endif
1693aca38eabSMarius Strobl 	    __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1694aca38eabSMarius Strobl 	    cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))
1695aca38eabSMarius Strobl 		mask &= ~SDHCI_DAT_INHIBIT;
16968775ab45SIan Lepore 	/*
16978775ab45SIan Lepore 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
16988775ab45SIan Lepore 	 *  here at all, but when writing a crash dump we may be bypassing the
16998775ab45SIan Lepore 	 *  host platform's interrupt handler, and in some cases that handler
17008775ab45SIan Lepore 	 *  may be working around hardware quirks such as not respecting r1b
17018775ab45SIan Lepore 	 *  busy indications.  In those cases, this wait-loop serves the purpose
17028775ab45SIan Lepore 	 *  of waiting for the prior command and data transfers to be done, and
17038775ab45SIan Lepore 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
17048775ab45SIan Lepore 	 *  (It's usually more like 20-30ms in the real world.)
17058775ab45SIan Lepore 	 */
17068775ab45SIan Lepore 	timeout = 250;
170790993663SIan Lepore 	while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
1708831f5dcfSAlexander Motin 		if (timeout == 0) {
1709831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
1710831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
1711831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
1712831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
1713e64f01a9SIan Lepore 			sdhci_req_done(slot);
1714831f5dcfSAlexander Motin 			return;
1715831f5dcfSAlexander Motin 		}
1716831f5dcfSAlexander Motin 		timeout--;
1717831f5dcfSAlexander Motin 		DELAY(1000);
1718831f5dcfSAlexander Motin 	}
1719831f5dcfSAlexander Motin 
1720831f5dcfSAlexander Motin 	/* Prepare command flags. */
1721831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
1722831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
1723831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
1724831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
1725831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
1726831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
1727831f5dcfSAlexander Motin 	else
1728831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
1729831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
1730831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
1731831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
1732831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
1733a94a63f0SWarner Losh 	if (cmd->data != NULL)
1734831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
1735831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
1736831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
1737831f5dcfSAlexander Motin 	/* Prepare data. */
1738831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
1739831f5dcfSAlexander Motin 	/*
1740831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
1741831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
1742831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
1743831f5dcfSAlexander Motin 	 */
1744831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
1745831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1746831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
1747831f5dcfSAlexander Motin 	}
1748831f5dcfSAlexander Motin 	/* Set command argument. */
1749831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
1750831f5dcfSAlexander Motin 	/* Set data transfer mode. */
1751831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
1752aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1753a94a63f0SWarner Losh 		slot_printf(slot, "Starting command!\n");
1754831f5dcfSAlexander Motin 	/* Start command. */
1755d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
1756a6873fd1SIan Lepore 	/* Start timeout callout. */
1757ba6fc1c7SLuiz Otavio O Souza 	callout_reset(&slot->timeout_callout, slot->timeout * hz,
1758ba6fc1c7SLuiz Otavio O Souza 	    sdhci_timeout, slot);
1759831f5dcfSAlexander Motin }
1760831f5dcfSAlexander Motin 
1761831f5dcfSAlexander Motin static void
1762831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
1763831f5dcfSAlexander Motin {
1764831f5dcfSAlexander Motin 	int i;
17651bacf3beSMarius Strobl 	uint32_t val;
17661bacf3beSMarius Strobl 	uint8_t extra;
1767831f5dcfSAlexander Motin 
1768aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1769a94a63f0SWarner Losh 		slot_printf(slot, "%s: called, err %d flags %d\n",
1770a94a63f0SWarner Losh 		    __func__, slot->curcmd->error, slot->curcmd->flags);
1771831f5dcfSAlexander Motin 	slot->cmd_done = 1;
177272dec079SMarius Strobl 	/*
177372dec079SMarius Strobl 	 * Interrupt aggregation: Restore command interrupt.
1774831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
177572dec079SMarius Strobl 	 * happened first.
177672dec079SMarius Strobl 	 */
1777aca38eabSMarius Strobl 	if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK &&
1778aca38eabSMarius Strobl 	    slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200))
1779aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |=
1780aca38eabSMarius Strobl 		    SDHCI_INT_RESPONSE);
1781831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
1782831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1783aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
1784aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1785831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1786831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1787831f5dcfSAlexander Motin 		sdhci_start(slot);
1788831f5dcfSAlexander Motin 		return;
1789831f5dcfSAlexander Motin 	}
1790831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
1791831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1792831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
1793831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
17941bacf3beSMarius Strobl 			extra = 0;
1795831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
17961bacf3beSMarius Strobl 				val = RD4(slot, SDHCI_RESPONSE + i * 4);
17971bacf3beSMarius Strobl 				if (slot->quirks &
17981bacf3beSMarius Strobl 				    SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1799677ee494SIan Lepore 					slot->curcmd->resp[3 - i] = val;
1800677ee494SIan Lepore 				else {
1801677ee494SIan Lepore 					slot->curcmd->resp[3 - i] =
1802677ee494SIan Lepore 					    (val << 8) | extra;
1803831f5dcfSAlexander Motin 					extra = val >> 24;
1804831f5dcfSAlexander Motin 				}
1805677ee494SIan Lepore 			}
1806831f5dcfSAlexander Motin 		} else
1807831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1808831f5dcfSAlexander Motin 	}
1809aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1810a94a63f0SWarner Losh 		printf("Resp: %02x %02x %02x %02x\n",
1811a94a63f0SWarner Losh 		    slot->curcmd->resp[0], slot->curcmd->resp[1],
1812a94a63f0SWarner Losh 		    slot->curcmd->resp[2], slot->curcmd->resp[3]);
1813a94a63f0SWarner Losh 
1814831f5dcfSAlexander Motin 	/* If data ready - finish. */
1815831f5dcfSAlexander Motin 	if (slot->data_done)
1816831f5dcfSAlexander Motin 		sdhci_start(slot);
1817831f5dcfSAlexander Motin }
1818831f5dcfSAlexander Motin 
1819831f5dcfSAlexander Motin static void
1820ab00a509SMarius Strobl sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data)
1821831f5dcfSAlexander Motin {
1822ab00a509SMarius Strobl 	uint32_t blkcnt, blksz, current_timeout, sdma_bbufsz, target_timeout;
1823831f5dcfSAlexander Motin 	uint8_t div;
1824831f5dcfSAlexander Motin 
1825831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1826831f5dcfSAlexander Motin 		slot->data_done = 1;
1827831f5dcfSAlexander Motin 		return;
1828831f5dcfSAlexander Motin 	}
1829831f5dcfSAlexander Motin 
1830831f5dcfSAlexander Motin 	slot->data_done = 0;
1831831f5dcfSAlexander Motin 
1832831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
1833831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
1834ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1835ceb9e9f7SIan Lepore 		div = 0xE;
1836ceb9e9f7SIan Lepore 	} else {
1837831f5dcfSAlexander Motin 		target_timeout = 1000000;
1838831f5dcfSAlexander Motin 		div = 0;
1839831f5dcfSAlexander Motin 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1840ceb9e9f7SIan Lepore 		while (current_timeout < target_timeout && div < 0xE) {
1841ceb9e9f7SIan Lepore 			++div;
1842831f5dcfSAlexander Motin 			current_timeout <<= 1;
1843831f5dcfSAlexander Motin 		}
1844831f5dcfSAlexander Motin 		/* Compensate for an off-by-one error in the CaFe chip.*/
1845ceb9e9f7SIan Lepore 		if (div < 0xE &&
1846ceb9e9f7SIan Lepore 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1847ceb9e9f7SIan Lepore 			++div;
1848831f5dcfSAlexander Motin 		}
1849ceb9e9f7SIan Lepore 	}
1850831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1851831f5dcfSAlexander Motin 
1852831f5dcfSAlexander Motin 	if (data == NULL)
1853831f5dcfSAlexander Motin 		return;
1854831f5dcfSAlexander Motin 
1855831f5dcfSAlexander Motin 	/* Use DMA if possible. */
1856831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
1857831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
1858ab00a509SMarius Strobl 	/* If data is small, broken DMA may return zeroes instead of data. */
1859d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1860831f5dcfSAlexander Motin 	    (data->len <= 512))
1861831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1862831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
1863d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1864831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
1865831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1866831f5dcfSAlexander Motin 	/* Load DMA buffer. */
1867831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
1868ab00a509SMarius Strobl 		sdma_bbufsz = slot->sdma_bbufsz;
1869831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
1870ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1871ecc2d997SRui Paulo 			    BUS_DMASYNC_PREREAD);
1872831f5dcfSAlexander Motin 		else {
1873ab00a509SMarius Strobl 			memcpy(slot->dmamem, data->data, ulmin(data->len,
1874ab00a509SMarius Strobl 			    sdma_bbufsz));
1875ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1876ecc2d997SRui Paulo 			    BUS_DMASYNC_PREWRITE);
1877831f5dcfSAlexander Motin 		}
1878831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1879ab00a509SMarius Strobl 		/*
1880ab00a509SMarius Strobl 		 * Interrupt aggregation: Mask border interrupt for the last
1881ab00a509SMarius Strobl 		 * bounce buffer and unmask otherwise.
1882ab00a509SMarius Strobl 		 */
1883ab00a509SMarius Strobl 		if (data->len == sdma_bbufsz)
1884831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1885831f5dcfSAlexander Motin 		else
1886831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
1887831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1888831f5dcfSAlexander Motin 	}
1889831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
1890831f5dcfSAlexander Motin 	slot->offset = 0;
1891ab00a509SMarius Strobl 	/* Set block size and request border interrupts on the SDMA boundary. */
1892ab00a509SMarius Strobl 	blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, ulmin(data->len, 512));
1893ab00a509SMarius Strobl 	WR2(slot, SDHCI_BLOCK_SIZE, blksz);
1894831f5dcfSAlexander Motin 	/* Set block count. */
1895ab00a509SMarius Strobl 	blkcnt = howmany(data->len, 512);
1896ab00a509SMarius Strobl 	WR2(slot, SDHCI_BLOCK_COUNT, blkcnt);
1897aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1898ab00a509SMarius Strobl 		slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
1899ab00a509SMarius Strobl 		    blksz, blkcnt);
1900831f5dcfSAlexander Motin }
1901831f5dcfSAlexander Motin 
1902c3a0f75aSOleksandr Tymoshenko void
1903831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
1904831f5dcfSAlexander Motin {
1905831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
19067e6ccea3SMarius Strobl 	size_t left;
1907831f5dcfSAlexander Motin 
1908831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
1909ecc2d997SRui Paulo 	 * Auxiliary restore point for the case when data interrupt
1910831f5dcfSAlexander Motin 	 * happened first. */
1911831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
1912831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1913831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
1914831f5dcfSAlexander Motin 	}
1915831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
1916915780d7SLuiz Otavio O Souza 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) &&
1917915780d7SLuiz Otavio O Souza 	    slot->curcmd->data != NULL) {
1918831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
19197e6ccea3SMarius Strobl 			left = data->len - slot->offset;
1920ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1921ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTREAD);
1922831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1923ab00a509SMarius Strobl 			    ulmin(left, slot->sdma_bbufsz));
1924831f5dcfSAlexander Motin 		} else
1925ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1926ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTWRITE);
1927831f5dcfSAlexander Motin 	}
1928a98788edSIan Lepore 	slot->data_done = 1;
1929831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
1930831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1931aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
1932aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1933831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1934831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1935831f5dcfSAlexander Motin 		sdhci_start(slot);
1936831f5dcfSAlexander Motin 		return;
1937831f5dcfSAlexander Motin 	}
1938831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
1939831f5dcfSAlexander Motin 	if (slot->cmd_done)
1940831f5dcfSAlexander Motin 		sdhci_start(slot);
1941831f5dcfSAlexander Motin }
1942831f5dcfSAlexander Motin 
1943a94a63f0SWarner Losh #ifdef MMCCAM
1944a94a63f0SWarner Losh static void
1945a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot)
1946a94a63f0SWarner Losh {
1947a94a63f0SWarner Losh 	union ccb *ccb;
1948ab00a509SMarius Strobl 	struct ccb_mmcio *mmcio;
1949a94a63f0SWarner Losh 
1950a94a63f0SWarner Losh 	ccb = slot->ccb;
1951a94a63f0SWarner Losh 	if (ccb == NULL)
1952a94a63f0SWarner Losh 		return;
1953a94a63f0SWarner Losh 
1954a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
1955a94a63f0SWarner Losh 	if (!(slot->flags & CMD_STARTED)) {
1956a94a63f0SWarner Losh 		slot->flags |= CMD_STARTED;
1957a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->cmd);
1958a94a63f0SWarner Losh 		return;
1959a94a63f0SWarner Losh 	}
1960a94a63f0SWarner Losh 
1961a94a63f0SWarner Losh 	/*
1962a94a63f0SWarner Losh 	 * Old stack doesn't use this!
1963a94a63f0SWarner Losh 	 * Enabling this code causes significant performance degradation
1964a94a63f0SWarner Losh 	 * and IRQ storms on BBB, Wandboard behaves fine.
1965a94a63f0SWarner Losh 	 * Not using this code does no harm...
1966a94a63f0SWarner Losh 	if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) {
1967a94a63f0SWarner Losh 		slot->flags |= STOP_STARTED;
1968a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->stop);
1969a94a63f0SWarner Losh 		return;
1970a94a63f0SWarner Losh 	}
1971a94a63f0SWarner Losh 	*/
1972aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1973a94a63f0SWarner Losh 		slot_printf(slot, "result: %d\n", mmcio->cmd.error);
1974a94a63f0SWarner Losh 	if (mmcio->cmd.error == 0 &&
1975a94a63f0SWarner Losh 	    (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
1976a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD);
1977a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_DATA);
1978a94a63f0SWarner Losh 	}
1979a94a63f0SWarner Losh 
1980a94a63f0SWarner Losh 	sdhci_req_done(slot);
1981a94a63f0SWarner Losh }
1982a94a63f0SWarner Losh #else
1983831f5dcfSAlexander Motin static void
1984831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
1985831f5dcfSAlexander Motin {
1986ab00a509SMarius Strobl 	const struct mmc_request *req;
1987831f5dcfSAlexander Motin 
1988831f5dcfSAlexander Motin 	req = slot->req;
1989831f5dcfSAlexander Motin 	if (req == NULL)
1990831f5dcfSAlexander Motin 		return;
1991831f5dcfSAlexander Motin 
1992831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
1993831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
1994831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
1995831f5dcfSAlexander Motin 		return;
1996831f5dcfSAlexander Motin 	}
1997915780d7SLuiz Otavio O Souza 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) &&
1998915780d7SLuiz Otavio O Souza 	    !(slot->flags & STOP_STARTED) && req->stop) {
1999831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
2000831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
2001831f5dcfSAlexander Motin 		return;
2002831f5dcfSAlexander Motin 	}
2003aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
20045b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
20055b69a497SAlexander Motin 	if (!req->cmd->error &&
2006915780d7SLuiz Otavio O Souza 	    ((slot->curcmd == req->stop &&
2007915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) ||
2008915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2009831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
2010831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
2011831f5dcfSAlexander Motin 	}
2012831f5dcfSAlexander Motin 
2013e64f01a9SIan Lepore 	sdhci_req_done(slot);
2014831f5dcfSAlexander Motin }
2015a94a63f0SWarner Losh #endif
2016831f5dcfSAlexander Motin 
2017d6b3aaf8SOleksandr Tymoshenko int
2018b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev,
2019b440e965SMarius Strobl     struct mmc_request *req)
2020831f5dcfSAlexander Motin {
2021831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2022831f5dcfSAlexander Motin 
2023831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2024831f5dcfSAlexander Motin 	if (slot->req != NULL) {
2025831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
2026831f5dcfSAlexander Motin 		return (EBUSY);
2027831f5dcfSAlexander Motin 	}
2028aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
20291bacf3beSMarius Strobl 		slot_printf(slot,
20301bacf3beSMarius Strobl 		    "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
2031831f5dcfSAlexander Motin 		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
20325b69a497SAlexander Motin 		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
20335b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
20345b69a497SAlexander Motin 	}
2035831f5dcfSAlexander Motin 	slot->req = req;
2036831f5dcfSAlexander Motin 	slot->flags = 0;
2037831f5dcfSAlexander Motin 	sdhci_start(slot);
2038831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2039bea2dca2SAlexander Motin 	if (dumping) {
2040bea2dca2SAlexander Motin 		while (slot->req != NULL) {
2041d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
2042bea2dca2SAlexander Motin 			DELAY(10);
2043bea2dca2SAlexander Motin 		}
2044bea2dca2SAlexander Motin 	}
2045831f5dcfSAlexander Motin 	return (0);
2046831f5dcfSAlexander Motin }
2047831f5dcfSAlexander Motin 
2048d6b3aaf8SOleksandr Tymoshenko int
2049b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev)
2050831f5dcfSAlexander Motin {
2051831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2052831f5dcfSAlexander Motin 	uint32_t val;
2053831f5dcfSAlexander Motin 
2054831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2055831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
2056831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2057831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
2058831f5dcfSAlexander Motin }
2059831f5dcfSAlexander Motin 
2060d6b3aaf8SOleksandr Tymoshenko int
2061b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev)
2062831f5dcfSAlexander Motin {
2063831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2064831f5dcfSAlexander Motin 	int err = 0;
2065831f5dcfSAlexander Motin 
2066831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2067831f5dcfSAlexander Motin 	while (slot->bus_busy)
2068d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
2069831f5dcfSAlexander Motin 	slot->bus_busy++;
2070831f5dcfSAlexander Motin 	/* Activate led. */
2071831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
2072831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2073831f5dcfSAlexander Motin 	return (err);
2074831f5dcfSAlexander Motin }
2075831f5dcfSAlexander Motin 
2076d6b3aaf8SOleksandr Tymoshenko int
2077b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev)
2078831f5dcfSAlexander Motin {
2079831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2080831f5dcfSAlexander Motin 
2081831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2082831f5dcfSAlexander Motin 	/* Deactivate led. */
2083831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
2084831f5dcfSAlexander Motin 	slot->bus_busy--;
2085831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2086d493985aSAlexander Motin 	wakeup(slot);
2087831f5dcfSAlexander Motin 	return (0);
2088831f5dcfSAlexander Motin }
2089831f5dcfSAlexander Motin 
2090831f5dcfSAlexander Motin static void
2091831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
2092831f5dcfSAlexander Motin {
2093831f5dcfSAlexander Motin 
2094831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2095831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
2096831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2097831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2098831f5dcfSAlexander Motin 		return;
2099831f5dcfSAlexander Motin 	}
2100831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
2101831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2102831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
2103831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2104831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
2105831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
2106831f5dcfSAlexander Motin 
2107831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
2108831f5dcfSAlexander Motin }
2109831f5dcfSAlexander Motin 
2110831f5dcfSAlexander Motin static void
2111831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
2112831f5dcfSAlexander Motin {
21131bacf3beSMarius Strobl 	struct mmc_data *data;
211415c440e1SWarner Losh 	size_t left;
2115ab00a509SMarius Strobl 	uint32_t sdma_bbufsz;
2116831f5dcfSAlexander Motin 
2117831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2118831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2119831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2120831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2121831f5dcfSAlexander Motin 		return;
2122831f5dcfSAlexander Motin 	}
2123831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2124831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
2125831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2126831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
2127831f5dcfSAlexander Motin 		    intmask);
2128831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2129831f5dcfSAlexander Motin 		return;
2130831f5dcfSAlexander Motin 	}
2131831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
2132831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2133acbaa69fSOleksandr Tymoshenko 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
2134831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2135831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2136831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
2137831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
2138831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2139831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
2140831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2141831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
2142831f5dcfSAlexander Motin 	}
2143831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
2144831f5dcfSAlexander Motin 		/* No need to continue after any error. */
2145a98788edSIan Lepore 		goto done;
2146831f5dcfSAlexander Motin 	}
2147831f5dcfSAlexander Motin 
2148aca38eabSMarius Strobl 	/* Handle tuning completion interrupt. */
2149aca38eabSMarius Strobl 	if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) &&
2150aca38eabSMarius Strobl 	    (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK ||
2151aca38eabSMarius Strobl 	    slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) {
2152aca38eabSMarius Strobl 		slot->req->flags |= MMC_TUNE_DONE;
2153aca38eabSMarius Strobl 		sdhci_finish_command(slot);
2154aca38eabSMarius Strobl 		sdhci_finish_data(slot);
2155aca38eabSMarius Strobl 		return;
2156aca38eabSMarius Strobl 	}
2157831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
2158c3a0f75aSOleksandr Tymoshenko 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
2159c3a0f75aSOleksandr Tymoshenko 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
2160c3a0f75aSOleksandr Tymoshenko 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
21611bacf3beSMarius Strobl 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot,
21621bacf3beSMarius Strobl 			    &intmask);
2163c3a0f75aSOleksandr Tymoshenko 			slot->flags |= PLATFORM_DATA_STARTED;
2164c3a0f75aSOleksandr Tymoshenko 		} else
2165831f5dcfSAlexander Motin 			sdhci_transfer_pio(slot);
2166c3a0f75aSOleksandr Tymoshenko 	}
2167831f5dcfSAlexander Motin 	/* Handle DMA border. */
2168831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
21691bacf3beSMarius Strobl 		data = slot->curcmd->data;
2170ab00a509SMarius Strobl 		sdma_bbufsz = slot->sdma_bbufsz;
2171831f5dcfSAlexander Motin 
2172831f5dcfSAlexander Motin 		/* Unload DMA buffer ... */
2173831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2174831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2175831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2176831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
2177831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
2178ab00a509SMarius Strobl 			    ulmin(left, sdma_bbufsz));
2179831f5dcfSAlexander Motin 		} else {
2180831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2181831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
2182831f5dcfSAlexander Motin 		}
2183831f5dcfSAlexander Motin 		/* ... and reload it again. */
2184ab00a509SMarius Strobl 		slot->offset += sdma_bbufsz;
2185831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2186831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2187831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2188831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREREAD);
2189831f5dcfSAlexander Motin 		} else {
2190831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
2191ab00a509SMarius Strobl 			    ulmin(left, sdma_bbufsz));
2192831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2193831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
2194831f5dcfSAlexander Motin 		}
2195ab00a509SMarius Strobl 		/*
2196ab00a509SMarius Strobl 		 * Interrupt aggregation: Mask border interrupt for the last
2197ab00a509SMarius Strobl 		 * bounce buffer.
2198ab00a509SMarius Strobl 		 */
2199ab00a509SMarius Strobl 		if (left == sdma_bbufsz) {
2200831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
2201831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2202831f5dcfSAlexander Motin 		}
2203831f5dcfSAlexander Motin 		/* Restart DMA. */
2204831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
2205831f5dcfSAlexander Motin 	}
2206831f5dcfSAlexander Motin 	/* We have got all data. */
2207c3a0f75aSOleksandr Tymoshenko 	if (intmask & SDHCI_INT_DATA_END) {
2208c3a0f75aSOleksandr Tymoshenko 		if (slot->flags & PLATFORM_DATA_STARTED) {
2209c3a0f75aSOleksandr Tymoshenko 			slot->flags &= ~PLATFORM_DATA_STARTED;
2210c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2211c3a0f75aSOleksandr Tymoshenko 		} else
2212831f5dcfSAlexander Motin 			sdhci_finish_data(slot);
2213831f5dcfSAlexander Motin 	}
2214a98788edSIan Lepore done:
2215a98788edSIan Lepore 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
2216a98788edSIan Lepore 		if (slot->flags & PLATFORM_DATA_STARTED) {
2217a98788edSIan Lepore 			slot->flags &= ~PLATFORM_DATA_STARTED;
2218a98788edSIan Lepore 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2219a98788edSIan Lepore 		} else
2220a98788edSIan Lepore 			sdhci_finish_data(slot);
2221a98788edSIan Lepore 	}
2222c3a0f75aSOleksandr Tymoshenko }
2223831f5dcfSAlexander Motin 
2224831f5dcfSAlexander Motin static void
22256dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err)
2226831f5dcfSAlexander Motin {
2227831f5dcfSAlexander Motin 
2228831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2229831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
22306dea80e6SMarius Strobl 		    "there is no active command.\n", acmd_err);
2231831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2232831f5dcfSAlexander Motin 		return;
2233831f5dcfSAlexander Motin 	}
22346dea80e6SMarius Strobl 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err);
2235831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_CMD);
2236831f5dcfSAlexander Motin }
2237831f5dcfSAlexander Motin 
2238d6b3aaf8SOleksandr Tymoshenko void
2239d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
2240831f5dcfSAlexander Motin {
22412b96b955SJustin Hibbits 	uint32_t intmask, present;
22426dea80e6SMarius Strobl 	uint16_t val16;
2243831f5dcfSAlexander Motin 
2244831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2245831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
2246831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
2247831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
2248831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
2249d6b3aaf8SOleksandr Tymoshenko 		return;
2250831f5dcfSAlexander Motin 	}
2251aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 2))
22525b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
22535b69a497SAlexander Motin 
2254aca38eabSMarius Strobl 	/* Handle tuning error interrupt. */
2255aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_TUNEERR)) {
22566dea80e6SMarius Strobl 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR);
2257aca38eabSMarius Strobl 		slot_printf(slot, "Tuning error indicated\n");
2258aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
2259aca38eabSMarius Strobl 		if (slot->curcmd) {
2260aca38eabSMarius Strobl 			slot->curcmd->error = MMC_ERR_BADCRC;
2261aca38eabSMarius Strobl 			sdhci_finish_command(slot);
2262aca38eabSMarius Strobl 		}
2263aca38eabSMarius Strobl 	}
2264aca38eabSMarius Strobl 	/* Handle re-tuning interrupt. */
2265aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_RETUNE))
2266aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
2267831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
2268831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2269639f59f0SIan Lepore 		present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
22702b96b955SJustin Hibbits 		slot->intmask &=
22712b96b955SJustin Hibbits 		    ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
22722b96b955SJustin Hibbits 		slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
22732b96b955SJustin Hibbits 		    SDHCI_INT_CARD_INSERT;
22742b96b955SJustin Hibbits 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
22752b96b955SJustin Hibbits 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2276831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
2277831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
2278b8bf08b1SIan Lepore 		sdhci_handle_card_present_locked(slot, present);
2279831f5dcfSAlexander Motin 	}
2280831f5dcfSAlexander Motin 	/* Handle command interrupts. */
2281831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
2282831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
2283831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
2284831f5dcfSAlexander Motin 	}
2285831f5dcfSAlexander Motin 	/* Handle data interrupts. */
2286831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
2287831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
22887e6ccea3SMarius Strobl 		/* Don't call data_irq in case of errored command. */
22897e586643SIan Lepore 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
2290831f5dcfSAlexander Motin 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
2291831f5dcfSAlexander Motin 	}
2292831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
2293831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
22946dea80e6SMarius Strobl 		/* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */
22956dea80e6SMarius Strobl 		val16 = RD2(slot, SDHCI_ACMD12_ERR);
2296831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
22976dea80e6SMarius Strobl 		sdhci_acmd_irq(slot, val16);
2298831f5dcfSAlexander Motin 	}
2299831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
2300831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
2301831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
2302aca38eabSMarius Strobl 		slot_printf(slot, "Card is consuming too much power!\n");
2303831f5dcfSAlexander Motin 	}
2304aca38eabSMarius Strobl 	intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE |
2305aca38eabSMarius Strobl 	    SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK |
2306aca38eabSMarius Strobl 	    SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER);
2307831f5dcfSAlexander Motin 	/* The rest is unknown. */
2308831f5dcfSAlexander Motin 	if (intmask) {
2309831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
2310831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
2311831f5dcfSAlexander Motin 		    intmask);
2312831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2313831f5dcfSAlexander Motin 	}
2314831f5dcfSAlexander Motin 
2315831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2316831f5dcfSAlexander Motin }
2317831f5dcfSAlexander Motin 
2318d6b3aaf8SOleksandr Tymoshenko int
23191bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which,
23201bacf3beSMarius Strobl     uintptr_t *result)
2321831f5dcfSAlexander Motin {
2322ab00a509SMarius Strobl 	const struct sdhci_slot *slot = device_get_ivars(child);
2323831f5dcfSAlexander Motin 
2324831f5dcfSAlexander Motin 	switch (which) {
2325831f5dcfSAlexander Motin 	default:
2326831f5dcfSAlexander Motin 		return (EINVAL);
2327831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2328bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
2329831f5dcfSAlexander Motin 		break;
2330831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2331bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
2332831f5dcfSAlexander Motin 		break;
2333831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2334bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
2335831f5dcfSAlexander Motin 		break;
2336831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2337bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
2338831f5dcfSAlexander Motin 		break;
2339831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2340bcd91d25SJayachandran C. 		*result = slot->host.f_min;
2341831f5dcfSAlexander Motin 		break;
2342831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
2343bcd91d25SJayachandran C. 		*result = slot->host.f_max;
2344831f5dcfSAlexander Motin 		break;
2345831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2346bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
2347831f5dcfSAlexander Motin 		break;
2348831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2349bcd91d25SJayachandran C. 		*result = slot->host.mode;
2350831f5dcfSAlexander Motin 		break;
2351831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2352bcd91d25SJayachandran C. 		*result = slot->host.ocr;
2353831f5dcfSAlexander Motin 		break;
2354831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2355bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
2356831f5dcfSAlexander Motin 		break;
2357831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2358bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
2359831f5dcfSAlexander Motin 		break;
2360aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2361aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED) {
2362aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) {
2363aca38eabSMarius Strobl 				*result = retune_req_reset;
2364aca38eabSMarius Strobl 				break;
2365aca38eabSMarius Strobl 			}
2366aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) {
2367aca38eabSMarius Strobl 				*result = retune_req_normal;
2368aca38eabSMarius Strobl 				break;
2369aca38eabSMarius Strobl 			}
2370aca38eabSMarius Strobl 		}
2371aca38eabSMarius Strobl 		*result = retune_req_none;
2372aca38eabSMarius Strobl 		break;
23730f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
23740f34084fSMarius Strobl 		*result = slot->host.ios.vccq;
23750f34084fSMarius Strobl 		break;
2376831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2377bcd91d25SJayachandran C. 		*result = slot->host.caps;
2378831f5dcfSAlexander Motin 		break;
2379831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2380bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
2381831f5dcfSAlexander Motin 		break;
23823a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2383aca38eabSMarius Strobl 		/*
2384aca38eabSMarius Strobl 		 * Re-tuning modes 1 and 2 restrict the maximum data length
2385aca38eabSMarius Strobl 		 * per read/write command to 4 MiB.
2386aca38eabSMarius Strobl 		 */
2387aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED &&
2388aca38eabSMarius Strobl 		    (slot->retune_mode == SDHCI_RETUNE_MODE_1 ||
2389aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_2)) {
2390aca38eabSMarius Strobl 			*result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE;
2391aca38eabSMarius Strobl 			break;
2392aca38eabSMarius Strobl 		}
2393bcd91d25SJayachandran C. 		*result = 65535;
23943a4a2557SAlexander Motin 		break;
239572dec079SMarius Strobl 	case MMCBR_IVAR_MAX_BUSY_TIMEOUT:
239672dec079SMarius Strobl 		/*
239772dec079SMarius Strobl 		 * Currently, sdhci_start_data() hardcodes 1 s for all CMDs.
239872dec079SMarius Strobl 		 */
239972dec079SMarius Strobl 		*result = 1000000;
240072dec079SMarius Strobl 		break;
2401831f5dcfSAlexander Motin 	}
2402831f5dcfSAlexander Motin 	return (0);
2403831f5dcfSAlexander Motin }
2404831f5dcfSAlexander Motin 
2405d6b3aaf8SOleksandr Tymoshenko int
24061bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which,
24071bacf3beSMarius Strobl     uintptr_t value)
2408831f5dcfSAlexander Motin {
2409831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
2410b440e965SMarius Strobl 	uint32_t clock, max_clock;
2411b440e965SMarius Strobl 	int i;
2412831f5dcfSAlexander Motin 
241315c440e1SWarner Losh 	if (sdhci_debug > 1)
241415c440e1SWarner Losh 		slot_printf(slot, "%s: var=%d\n", __func__, which);
2415831f5dcfSAlexander Motin 	switch (which) {
2416831f5dcfSAlexander Motin 	default:
2417831f5dcfSAlexander Motin 		return (EINVAL);
2418831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2419831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
2420831f5dcfSAlexander Motin 		break;
2421831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2422831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
2423831f5dcfSAlexander Motin 		break;
2424831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2425831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
2426831f5dcfSAlexander Motin 		break;
2427831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2428831f5dcfSAlexander Motin 		if (value > 0) {
242957677a3aSOleksandr Tymoshenko 			max_clock = slot->max_clk;
243057677a3aSOleksandr Tymoshenko 			clock = max_clock;
243157677a3aSOleksandr Tymoshenko 
243257677a3aSOleksandr Tymoshenko 			if (slot->version < SDHCI_SPEC_300) {
243357677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
243457677a3aSOleksandr Tymoshenko 				    i <<= 1) {
2435831f5dcfSAlexander Motin 					if (clock <= value)
2436831f5dcfSAlexander Motin 						break;
2437831f5dcfSAlexander Motin 					clock >>= 1;
2438831f5dcfSAlexander Motin 				}
2439b440e965SMarius Strobl 			} else {
244057677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
244157677a3aSOleksandr Tymoshenko 				    i += 2) {
244257677a3aSOleksandr Tymoshenko 					if (clock <= value)
244357677a3aSOleksandr Tymoshenko 						break;
244457677a3aSOleksandr Tymoshenko 					clock = max_clock / (i + 2);
244557677a3aSOleksandr Tymoshenko 				}
244657677a3aSOleksandr Tymoshenko 			}
244757677a3aSOleksandr Tymoshenko 
2448831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
2449831f5dcfSAlexander Motin 		} else
2450831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
2451831f5dcfSAlexander Motin 		break;
2452831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2453831f5dcfSAlexander Motin 		slot->host.mode = value;
2454831f5dcfSAlexander Motin 		break;
2455831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2456831f5dcfSAlexander Motin 		slot->host.ocr = value;
2457831f5dcfSAlexander Motin 		break;
2458831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2459831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
2460831f5dcfSAlexander Motin 		break;
2461831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2462831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
2463831f5dcfSAlexander Motin 		break;
24640f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
24650f34084fSMarius Strobl 		slot->host.ios.vccq = value;
24660f34084fSMarius Strobl 		break;
2467831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2468831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
2469831f5dcfSAlexander Motin 		break;
2470831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2471831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2472831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2473831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
24743a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2475aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2476831f5dcfSAlexander Motin 		return (EINVAL);
2477831f5dcfSAlexander Motin 	}
2478831f5dcfSAlexander Motin 	return (0);
2479831f5dcfSAlexander Motin }
2480831f5dcfSAlexander Motin 
248115c440e1SWarner Losh #ifdef MMCCAM
2482a94a63f0SWarner Losh void
2483d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot)
2484a94a63f0SWarner Losh {
2485ab00a509SMarius Strobl 
2486*505f6a0cSBjoern A. Zeeb 	if ((slot->devq = cam_simq_alloc(1)) == NULL)
2487a94a63f0SWarner Losh 		goto fail;
2488a94a63f0SWarner Losh 
2489a94a63f0SWarner Losh 	mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF);
2490a94a63f0SWarner Losh 	slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll,
2491a94a63f0SWarner Losh 	    "sdhci_slot", slot, device_get_unit(slot->bus),
2492a94a63f0SWarner Losh 	    &slot->sim_mtx, 1, 1, slot->devq);
2493a94a63f0SWarner Losh 
2494a94a63f0SWarner Losh 	if (slot->sim == NULL) {
2495a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2496a94a63f0SWarner Losh 		slot_printf(slot, "cannot allocate CAM SIM\n");
2497a94a63f0SWarner Losh 		goto fail;
2498a94a63f0SWarner Losh 	}
2499a94a63f0SWarner Losh 
2500a94a63f0SWarner Losh 	mtx_lock(&slot->sim_mtx);
2501a94a63f0SWarner Losh 	if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) {
2502*505f6a0cSBjoern A. Zeeb 		slot_printf(slot, "cannot register SCSI pass-through bus\n");
2503a94a63f0SWarner Losh 		cam_sim_free(slot->sim, FALSE);
2504a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2505a94a63f0SWarner Losh 		mtx_unlock(&slot->sim_mtx);
2506a94a63f0SWarner Losh 		goto fail;
2507a94a63f0SWarner Losh 	}
2508a94a63f0SWarner Losh 	mtx_unlock(&slot->sim_mtx);
2509*505f6a0cSBjoern A. Zeeb 
2510a94a63f0SWarner Losh 	/* End CAM-specific init */
2511a94a63f0SWarner Losh 	slot->card_present = 0;
2512a94a63f0SWarner Losh 	sdhci_card_task(slot, 0);
2513a94a63f0SWarner Losh 	return;
2514a94a63f0SWarner Losh 
2515a94a63f0SWarner Losh fail:
2516a94a63f0SWarner Losh 	if (slot->sim != NULL) {
2517a94a63f0SWarner Losh 		mtx_lock(&slot->sim_mtx);
2518a94a63f0SWarner Losh 		xpt_bus_deregister(cam_sim_path(slot->sim));
2519a94a63f0SWarner Losh 		cam_sim_free(slot->sim, FALSE);
2520a94a63f0SWarner Losh 		mtx_unlock(&slot->sim_mtx);
2521a94a63f0SWarner Losh 	}
2522a94a63f0SWarner Losh 
2523a94a63f0SWarner Losh 	if (slot->devq != NULL)
2524a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2525a94a63f0SWarner Losh }
2526a94a63f0SWarner Losh 
2527a94a63f0SWarner Losh static void
2528a94a63f0SWarner Losh sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb)
2529a94a63f0SWarner Losh {
2530a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2531a94a63f0SWarner Losh 
2532a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2533a94a63f0SWarner Losh 
2534a94a63f0SWarner Losh 	sdhci_cam_request(slot, ccb);
2535a94a63f0SWarner Losh }
2536a94a63f0SWarner Losh 
2537a94a63f0SWarner Losh void
2538a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb)
2539a94a63f0SWarner Losh {
2540a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2541a94a63f0SWarner Losh 
2542a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2543a94a63f0SWarner Losh 	if (slot == NULL) {
2544a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2545a94a63f0SWarner Losh 		xpt_done(ccb);
2546a94a63f0SWarner Losh 		return;
2547a94a63f0SWarner Losh 	}
2548a94a63f0SWarner Losh 
2549a94a63f0SWarner Losh 	mtx_assert(&slot->sim_mtx, MA_OWNED);
2550a94a63f0SWarner Losh 
2551a94a63f0SWarner Losh 	switch (ccb->ccb_h.func_code) {
2552a94a63f0SWarner Losh 	case XPT_PATH_INQ:
2553a94a63f0SWarner Losh 	{
2554a94a63f0SWarner Losh 		struct ccb_pathinq *cpi;
2555a94a63f0SWarner Losh 
2556a94a63f0SWarner Losh 		cpi = &ccb->cpi;
2557a94a63f0SWarner Losh 		cpi->version_num = 1;
2558a94a63f0SWarner Losh 		cpi->hba_inquiry = 0;
2559a94a63f0SWarner Losh 		cpi->target_sprt = 0;
2560a94a63f0SWarner Losh 		cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
2561a94a63f0SWarner Losh 		cpi->hba_eng_cnt = 0;
2562a94a63f0SWarner Losh 		cpi->max_target = 0;
2563a94a63f0SWarner Losh 		cpi->max_lun = 0;
2564a94a63f0SWarner Losh 		cpi->initiator_id = 1;
2565a94a63f0SWarner Losh 		cpi->maxio = MAXPHYS;
2566a94a63f0SWarner Losh 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2567a94a63f0SWarner Losh 		strncpy(cpi->hba_vid, "Deglitch Networks", HBA_IDLEN);
2568a94a63f0SWarner Losh 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2569a94a63f0SWarner Losh 		cpi->unit_number = cam_sim_unit(sim);
2570a94a63f0SWarner Losh 		cpi->bus_id = cam_sim_bus(sim);
2571a94a63f0SWarner Losh 		cpi->base_transfer_speed = 100; /* XXX WTF? */
2572a94a63f0SWarner Losh 		cpi->protocol = PROTO_MMCSD;
2573a94a63f0SWarner Losh 		cpi->protocol_version = SCSI_REV_0;
2574a94a63f0SWarner Losh 		cpi->transport = XPORT_MMCSD;
2575a94a63f0SWarner Losh 		cpi->transport_version = 0;
2576a94a63f0SWarner Losh 
2577a94a63f0SWarner Losh 		cpi->ccb_h.status = CAM_REQ_CMP;
2578a94a63f0SWarner Losh 		break;
2579a94a63f0SWarner Losh 	}
2580a94a63f0SWarner Losh 	case XPT_GET_TRAN_SETTINGS:
2581a94a63f0SWarner Losh 	{
2582a94a63f0SWarner Losh 		struct ccb_trans_settings *cts = &ccb->cts;
2583a94a63f0SWarner Losh 
2584a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2585a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n");
2586a94a63f0SWarner Losh 
2587a94a63f0SWarner Losh 		cts->protocol = PROTO_MMCSD;
2588a94a63f0SWarner Losh 		cts->protocol_version = 1;
2589a94a63f0SWarner Losh 		cts->transport = XPORT_MMCSD;
2590a94a63f0SWarner Losh 		cts->transport_version = 1;
2591a94a63f0SWarner Losh 		cts->xport_specific.valid = 0;
2592a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_ocr = slot->host.host_ocr;
2593a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_min = slot->host.f_min;
2594a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_max = slot->host.f_max;
2595a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_caps = slot->host.caps;
2596a94a63f0SWarner Losh 		memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios));
2597a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2598a94a63f0SWarner Losh 		break;
2599a94a63f0SWarner Losh 	}
2600a94a63f0SWarner Losh 	case XPT_SET_TRAN_SETTINGS:
2601a94a63f0SWarner Losh 	{
2602a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2603a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n");
2604a94a63f0SWarner Losh 		sdhci_cam_settran_settings(slot, ccb);
2605a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2606a94a63f0SWarner Losh 		break;
2607a94a63f0SWarner Losh 	}
2608a94a63f0SWarner Losh 	case XPT_RESET_BUS:
2609a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2610a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n");
2611a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2612a94a63f0SWarner Losh 		break;
2613a94a63f0SWarner Losh 	case XPT_MMC_IO:
2614a94a63f0SWarner Losh 		/*
2615a94a63f0SWarner Losh 		 * Here is the HW-dependent part of
2616a94a63f0SWarner Losh 		 * sending the command to the underlying h/w
2617a94a63f0SWarner Losh 		 * At some point in the future an interrupt comes.
2618a94a63f0SWarner Losh 		 * Then the request will be marked as completed.
2619a94a63f0SWarner Losh 		 */
2620aca38eabSMarius Strobl 		if (__predict_false(sdhci_debug > 1))
2621a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_MMC_IO\n");
2622a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INPROG;
2623a94a63f0SWarner Losh 
2624a94a63f0SWarner Losh 		sdhci_cam_handle_mmcio(sim, ccb);
2625a94a63f0SWarner Losh 		return;
2626a94a63f0SWarner Losh 		/* NOTREACHED */
2627a94a63f0SWarner Losh 		break;
2628a94a63f0SWarner Losh 	default:
2629a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INVALID;
2630a94a63f0SWarner Losh 		break;
2631a94a63f0SWarner Losh 	}
2632a94a63f0SWarner Losh 	xpt_done(ccb);
2633a94a63f0SWarner Losh 	return;
2634a94a63f0SWarner Losh }
2635a94a63f0SWarner Losh 
2636a94a63f0SWarner Losh void
2637a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim)
2638a94a63f0SWarner Losh {
2639a94a63f0SWarner Losh 	return;
2640a94a63f0SWarner Losh }
2641a94a63f0SWarner Losh 
26426dea80e6SMarius Strobl static int
2643ab00a509SMarius Strobl sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot,
2644ab00a509SMarius Strobl     int proposed_clock)
26456dea80e6SMarius Strobl {
2646a94a63f0SWarner Losh 	int max_clock, clock, i;
2647a94a63f0SWarner Losh 
2648a94a63f0SWarner Losh 	if (proposed_clock == 0)
2649a94a63f0SWarner Losh 		return 0;
2650a94a63f0SWarner Losh 	max_clock = slot->max_clk;
2651a94a63f0SWarner Losh 	clock = max_clock;
2652a94a63f0SWarner Losh 
2653a94a63f0SWarner Losh 	if (slot->version < SDHCI_SPEC_300) {
2654*505f6a0cSBjoern A. Zeeb 		for (i = 0; i < SDHCI_200_MAX_DIVIDER; i <<= 1) {
2655a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2656a94a63f0SWarner Losh 				break;
2657a94a63f0SWarner Losh 			clock >>= 1;
2658a94a63f0SWarner Losh 		}
2659a94a63f0SWarner Losh 	} else {
2660*505f6a0cSBjoern A. Zeeb 		for (i = 0; i < SDHCI_300_MAX_DIVIDER; i += 2) {
2661a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2662a94a63f0SWarner Losh 				break;
2663a94a63f0SWarner Losh 			clock = max_clock / (i + 2);
2664a94a63f0SWarner Losh 		}
2665a94a63f0SWarner Losh 	}
2666a94a63f0SWarner Losh 	return clock;
2667a94a63f0SWarner Losh }
2668a94a63f0SWarner Losh 
2669ab00a509SMarius Strobl static int
2670a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb)
2671a94a63f0SWarner Losh {
2672a94a63f0SWarner Losh 	struct mmc_ios *ios;
2673ab00a509SMarius Strobl 	const struct mmc_ios *new_ios;
2674ab00a509SMarius Strobl 	const struct ccb_trans_settings_mmc *cts;
2675a94a63f0SWarner Losh 
2676a94a63f0SWarner Losh 	ios = &slot->host.ios;
2677a94a63f0SWarner Losh 	cts = &ccb->cts.proto_specific.mmc;
2678a94a63f0SWarner Losh 	new_ios = &cts->ios;
2679a94a63f0SWarner Losh 
2680a94a63f0SWarner Losh 	/* Update only requested fields */
2681a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CLK) {
2682a94a63f0SWarner Losh 		ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock);
2683a94a63f0SWarner Losh 		slot_printf(slot, "Clock => %d\n", ios->clock);
2684a94a63f0SWarner Losh 	}
2685a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_VDD) {
2686a94a63f0SWarner Losh 		ios->vdd = new_ios->vdd;
2687a94a63f0SWarner Losh 		slot_printf(slot, "VDD => %d\n", ios->vdd);
2688a94a63f0SWarner Losh 	}
2689a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CS) {
2690a94a63f0SWarner Losh 		ios->chip_select = new_ios->chip_select;
2691a94a63f0SWarner Losh 		slot_printf(slot, "CS => %d\n", ios->chip_select);
2692a94a63f0SWarner Losh 	}
2693a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BW) {
2694a94a63f0SWarner Losh 		ios->bus_width = new_ios->bus_width;
2695a94a63f0SWarner Losh 		slot_printf(slot, "Bus width => %d\n", ios->bus_width);
2696a94a63f0SWarner Losh 	}
2697a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_PM) {
2698a94a63f0SWarner Losh 		ios->power_mode = new_ios->power_mode;
2699a94a63f0SWarner Losh 		slot_printf(slot, "Power mode => %d\n", ios->power_mode);
2700a94a63f0SWarner Losh 	}
2701a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BT) {
2702a94a63f0SWarner Losh 		ios->timing = new_ios->timing;
2703a94a63f0SWarner Losh 		slot_printf(slot, "Timing => %d\n", ios->timing);
2704a94a63f0SWarner Losh 	}
2705a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BM) {
2706a94a63f0SWarner Losh 		ios->bus_mode = new_ios->bus_mode;
2707a94a63f0SWarner Losh 		slot_printf(slot, "Bus mode => %d\n", ios->bus_mode);
2708a94a63f0SWarner Losh 	}
2709a94a63f0SWarner Losh 
2710a94a63f0SWarner Losh 	/* XXX Provide a way to call a chip-specific IOS update, required for TI */
2711a94a63f0SWarner Losh 	return (sdhci_cam_update_ios(slot));
2712a94a63f0SWarner Losh }
2713a94a63f0SWarner Losh 
2714ab00a509SMarius Strobl static int
2715a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot)
2716a94a63f0SWarner Losh {
2717a94a63f0SWarner Losh 	struct mmc_ios *ios = &slot->host.ios;
2718a94a63f0SWarner Losh 
2719a94a63f0SWarner Losh 	slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n",
2720a94a63f0SWarner Losh 		    __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing);
2721a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2722a94a63f0SWarner Losh 	/* Do full reset on bus power down to clear from any state. */
2723a94a63f0SWarner Losh 	if (ios->power_mode == power_off) {
2724a94a63f0SWarner Losh 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
2725a94a63f0SWarner Losh 		sdhci_init(slot);
2726a94a63f0SWarner Losh 	}
2727a94a63f0SWarner Losh 	/* Configure the bus. */
2728a94a63f0SWarner Losh 	sdhci_set_clock(slot, ios->clock);
2729a94a63f0SWarner Losh 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
2730a94a63f0SWarner Losh 	if (ios->bus_width == bus_width_8) {
2731a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
2732a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2733a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_4) {
2734a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2735a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
2736a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_1) {
2737a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2738a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2739a94a63f0SWarner Losh 	} else {
2740a94a63f0SWarner Losh 		panic("Invalid bus width: %d", ios->bus_width);
2741a94a63f0SWarner Losh 	}
2742a94a63f0SWarner Losh 	if (ios->timing == bus_timing_hs &&
2743a94a63f0SWarner Losh 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
2744a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_HISPD;
2745a94a63f0SWarner Losh 	else
2746a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
2747a94a63f0SWarner Losh 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
2748a94a63f0SWarner Losh 	/* Some controllers like reset after bus changes. */
2749a94a63f0SWarner Losh 	if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
2750a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2751a94a63f0SWarner Losh 
2752a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2753a94a63f0SWarner Losh 	return (0);
2754a94a63f0SWarner Losh }
2755a94a63f0SWarner Losh 
2756ab00a509SMarius Strobl static int
2757a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb)
2758a94a63f0SWarner Losh {
2759ab00a509SMarius Strobl 	const struct ccb_mmcio *mmcio;
2760a94a63f0SWarner Losh 
2761a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
2762a94a63f0SWarner Losh 
2763a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2764a94a63f0SWarner Losh /*	if (slot->req != NULL) {
2765a94a63f0SWarner Losh 		SDHCI_UNLOCK(slot);
2766a94a63f0SWarner Losh 		return (EBUSY);
2767a94a63f0SWarner Losh 	}
2768a94a63f0SWarner Losh */
2769aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
2770a94a63f0SWarner Losh 		slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
2771a94a63f0SWarner Losh 			    mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags,
2772a94a63f0SWarner Losh 			    mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0,
2773a94a63f0SWarner Losh 			    mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0);
2774a94a63f0SWarner Losh 	}
2775a94a63f0SWarner Losh 	if (mmcio->cmd.data != NULL) {
2776a94a63f0SWarner Losh 		if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0)
2777a94a63f0SWarner Losh 			panic("data->len = %d, data->flags = %d -- something is b0rked",
2778a94a63f0SWarner Losh 			    (int)mmcio->cmd.data->len, mmcio->cmd.data->flags);
2779a94a63f0SWarner Losh 	}
2780a94a63f0SWarner Losh 	slot->ccb = ccb;
2781a94a63f0SWarner Losh 	slot->flags = 0;
2782a94a63f0SWarner Losh 	sdhci_start(slot);
2783a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2784a94a63f0SWarner Losh 	if (dumping) {
2785a94a63f0SWarner Losh 		while (slot->ccb != NULL) {
2786a94a63f0SWarner Losh 			sdhci_generic_intr(slot);
2787a94a63f0SWarner Losh 			DELAY(10);
2788a94a63f0SWarner Losh 		}
2789a94a63f0SWarner Losh 	}
2790a94a63f0SWarner Losh 	return (0);
2791a94a63f0SWarner Losh }
279215c440e1SWarner Losh #endif /* MMCCAM */
2793a94a63f0SWarner Losh 
2794ab00a509SMarius Strobl MODULE_VERSION(sdhci, SDHCI_VERSION);
2795