xref: /freebsd/sys/dev/sdhci/sdhci.c (revision b18f2ef413fb8c3ff5384be3fc92c60e0937755d)
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 
707029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
717029da5cSPawel Biernacki     "sdhci driver");
72831f5dcfSAlexander Motin 
73a94a63f0SWarner Losh static int sdhci_debug = 0;
747e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0,
757e6ccea3SMarius Strobl     "Debug level");
760f34084fSMarius Strobl u_int sdhci_quirk_clear = 0;
770f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear,
780f34084fSMarius Strobl     0, "Mask of quirks to clear");
790f34084fSMarius Strobl u_int sdhci_quirk_set = 0;
800f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0,
810f34084fSMarius Strobl     "Mask of quirks to set");
825b69a497SAlexander Motin 
83d6b3aaf8SOleksandr Tymoshenko #define	RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
84d6b3aaf8SOleksandr Tymoshenko #define	RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
85d6b3aaf8SOleksandr Tymoshenko #define	RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
86d6b3aaf8SOleksandr Tymoshenko #define	RD_MULTI_4(slot, off, ptr, count)	\
87d6b3aaf8SOleksandr Tymoshenko     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
88831f5dcfSAlexander Motin 
89d6b3aaf8SOleksandr Tymoshenko #define	WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
90d6b3aaf8SOleksandr Tymoshenko #define	WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
91d6b3aaf8SOleksandr Tymoshenko #define	WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
92d6b3aaf8SOleksandr Tymoshenko #define	WR_MULTI_4(slot, off, ptr, count)	\
93d6b3aaf8SOleksandr Tymoshenko     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
94831f5dcfSAlexander Motin 
956dea80e6SMarius Strobl static void sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err);
96aca38eabSMarius Strobl static void sdhci_card_poll(void *arg);
97aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending);
986dea80e6SMarius Strobl static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask);
996dea80e6SMarius Strobl static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask);
100aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset);
1016dea80e6SMarius Strobl static void sdhci_handle_card_present_locked(struct sdhci_slot *slot,
1026dea80e6SMarius Strobl     bool is_present);
1036dea80e6SMarius Strobl static void sdhci_finish_command(struct sdhci_slot *slot);
1046dea80e6SMarius Strobl static void sdhci_init(struct sdhci_slot *slot);
1056dea80e6SMarius Strobl static void sdhci_read_block_pio(struct sdhci_slot *slot);
1066dea80e6SMarius Strobl static void sdhci_req_done(struct sdhci_slot *slot);
107aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req);
1086dea80e6SMarius Strobl static void sdhci_reset(struct sdhci_slot *slot, uint8_t mask);
109aca38eabSMarius Strobl static void sdhci_retune(void *arg);
110831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
1116dea80e6SMarius Strobl static void sdhci_set_power(struct sdhci_slot *slot, u_char power);
1126dea80e6SMarius Strobl static void sdhci_set_transfer_mode(struct sdhci_slot *slot,
113ab00a509SMarius Strobl    const struct mmc_data *data);
114831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot);
1156dea80e6SMarius Strobl static void sdhci_timeout(void *arg);
1166dea80e6SMarius Strobl static void sdhci_start_command(struct sdhci_slot *slot,
1176dea80e6SMarius Strobl    struct mmc_command *cmd);
118ab00a509SMarius Strobl static void sdhci_start_data(struct sdhci_slot *slot,
119ab00a509SMarius Strobl    const struct mmc_data *data);
1206dea80e6SMarius Strobl static void sdhci_write_block_pio(struct sdhci_slot *slot);
1216dea80e6SMarius Strobl static void sdhci_transfer_pio(struct sdhci_slot *slot);
122831f5dcfSAlexander Motin 
12315c440e1SWarner Losh #ifdef MMCCAM
124a94a63f0SWarner Losh /* CAM-related */
125a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb);
126ab00a509SMarius Strobl static int sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot,
1276dea80e6SMarius Strobl     int proposed_clock);
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 {
18827d72fe1SBjoern A. Zeeb 	char buf[128];
189d6b3aaf8SOleksandr Tymoshenko 	va_list ap;
190d6b3aaf8SOleksandr Tymoshenko 	int retval;
191d6b3aaf8SOleksandr Tymoshenko 
19227d72fe1SBjoern A. Zeeb 	/*
19327d72fe1SBjoern A. Zeeb 	 * Make sure we print a single line all together rather than in two
19427d72fe1SBjoern A. Zeeb 	 * halves to avoid console gibberish bingo.
19527d72fe1SBjoern A. Zeeb 	 */
196d6b3aaf8SOleksandr Tymoshenko 	va_start(ap, fmt);
19727d72fe1SBjoern A. Zeeb 	retval = vsnprintf(buf, sizeof(buf), fmt, ap);
198d6b3aaf8SOleksandr Tymoshenko 	va_end(ap);
19927d72fe1SBjoern A. Zeeb 
20027d72fe1SBjoern A. Zeeb 	retval += printf("%s-slot%d: %s",
20127d72fe1SBjoern A. Zeeb 	    device_get_nameunit(slot->bus), slot->num, buf);
202d6b3aaf8SOleksandr Tymoshenko 	return (retval);
203d6b3aaf8SOleksandr Tymoshenko }
204d6b3aaf8SOleksandr Tymoshenko 
205831f5dcfSAlexander Motin static void
206831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot)
207831f5dcfSAlexander Motin {
2087e6ccea3SMarius Strobl 
209831f5dcfSAlexander Motin 	slot_printf(slot,
210831f5dcfSAlexander Motin 	    "============== REGISTER DUMP ==============\n");
211831f5dcfSAlexander Motin 
212831f5dcfSAlexander Motin 	slot_printf(slot, "Sys addr: 0x%08x | Version:  0x%08x\n",
213831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
214831f5dcfSAlexander Motin 	slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
215831f5dcfSAlexander Motin 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
216831f5dcfSAlexander Motin 	slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
217831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
218831f5dcfSAlexander Motin 	slot_printf(slot, "Present:  0x%08x | Host ctl: 0x%08x\n",
219831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
220831f5dcfSAlexander Motin 	slot_printf(slot, "Power:    0x%08x | Blk gap:  0x%08x\n",
221831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
222831f5dcfSAlexander Motin 	slot_printf(slot, "Wake-up:  0x%08x | Clock:    0x%08x\n",
223831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
224831f5dcfSAlexander Motin 	slot_printf(slot, "Timeout:  0x%08x | Int stat: 0x%08x\n",
225831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
226831f5dcfSAlexander Motin 	slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
227831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
2289dbf8c46SMarius Strobl 	slot_printf(slot, "AC12 err: 0x%08x | Host ctl2:0x%08x\n",
2299dbf8c46SMarius Strobl 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2));
2309dbf8c46SMarius Strobl 	slot_printf(slot, "Caps:     0x%08x | Caps2:    0x%08x\n",
2319dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2));
2329dbf8c46SMarius Strobl 	slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n",
2339dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR));
2349dbf8c46SMarius Strobl 	slot_printf(slot, "ADMA addr:0x%08x | Slot int: 0x%08x\n",
2359dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS));
236831f5dcfSAlexander Motin 
237831f5dcfSAlexander Motin 	slot_printf(slot,
238831f5dcfSAlexander Motin 	    "===========================================\n");
239831f5dcfSAlexander Motin }
240831f5dcfSAlexander Motin 
241831f5dcfSAlexander Motin static void
242831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
243831f5dcfSAlexander Motin {
244831f5dcfSAlexander Motin 	int timeout;
245b440e965SMarius Strobl 	uint32_t clock;
246831f5dcfSAlexander Motin 
247d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
2486e37fb2bSIan Lepore 		if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
249831f5dcfSAlexander Motin 			return;
250831f5dcfSAlexander Motin 	}
251831f5dcfSAlexander Motin 
252831f5dcfSAlexander Motin 	/* Some controllers need this kick or reset won't work. */
253831f5dcfSAlexander Motin 	if ((mask & SDHCI_RESET_ALL) == 0 &&
254d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
255831f5dcfSAlexander Motin 		/* This is to force an update */
256831f5dcfSAlexander Motin 		clock = slot->clock;
257831f5dcfSAlexander Motin 		slot->clock = 0;
258831f5dcfSAlexander Motin 		sdhci_set_clock(slot, clock);
259831f5dcfSAlexander Motin 	}
260831f5dcfSAlexander Motin 
261d8208d9eSAlexander Motin 	if (mask & SDHCI_RESET_ALL) {
262831f5dcfSAlexander Motin 		slot->clock = 0;
263d8208d9eSAlexander Motin 		slot->power = 0;
264d8208d9eSAlexander Motin 	}
265831f5dcfSAlexander Motin 
26661bc42f7SIan Lepore 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
26761bc42f7SIan Lepore 
26861bc42f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
26961bc42f7SIan Lepore 		/*
27061bc42f7SIan Lepore 		 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
27161bc42f7SIan Lepore 		 * specification.  The reset bit has internal propagation delay,
27261bc42f7SIan Lepore 		 * so a fast read after write returns 0 even if reset process is
27361bc42f7SIan Lepore 		 * in progress.  The workaround is to poll for 1 before polling
27461bc42f7SIan Lepore 		 * for 0.  In the worst case, if we miss seeing it asserted the
27561bc42f7SIan Lepore 		 * time we spent waiting is enough to ensure the reset finishes.
27661bc42f7SIan Lepore 		 */
27761bc42f7SIan Lepore 		timeout = 10000;
27861bc42f7SIan Lepore 		while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
27961bc42f7SIan Lepore 			if (timeout <= 0)
28061bc42f7SIan Lepore 				break;
28161bc42f7SIan Lepore 			timeout--;
28261bc42f7SIan Lepore 			DELAY(1);
28361bc42f7SIan Lepore 		}
28461bc42f7SIan Lepore 	}
28561bc42f7SIan Lepore 
286831f5dcfSAlexander Motin 	/* Wait max 100 ms */
28761bc42f7SIan Lepore 	timeout = 10000;
288831f5dcfSAlexander Motin 	/* Controller clears the bits when it's done */
28961bc42f7SIan Lepore 	while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
29061bc42f7SIan Lepore 		if (timeout <= 0) {
29161bc42f7SIan Lepore 			slot_printf(slot, "Reset 0x%x never completed.\n",
29261bc42f7SIan Lepore 			    mask);
293831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
294831f5dcfSAlexander Motin 			return;
295831f5dcfSAlexander Motin 		}
296831f5dcfSAlexander Motin 		timeout--;
29761bc42f7SIan Lepore 		DELAY(10);
298831f5dcfSAlexander Motin 	}
299831f5dcfSAlexander Motin }
300831f5dcfSAlexander Motin 
301aca38eabSMarius Strobl static uint32_t
302ab00a509SMarius Strobl sdhci_tuning_intmask(const struct sdhci_slot *slot)
303aca38eabSMarius Strobl {
304aca38eabSMarius Strobl 	uint32_t intmask;
305aca38eabSMarius Strobl 
306aca38eabSMarius Strobl 	intmask = 0;
30778f8baa8SMarius Strobl 	if (slot->opt & SDHCI_TUNING_ENABLED) {
308aca38eabSMarius Strobl 		intmask |= SDHCI_INT_TUNEERR;
309aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_2 ||
310aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_3)
311aca38eabSMarius Strobl 			intmask |= SDHCI_INT_RETUNE;
312aca38eabSMarius Strobl 	}
313aca38eabSMarius Strobl 	return (intmask);
314aca38eabSMarius Strobl }
315aca38eabSMarius Strobl 
316831f5dcfSAlexander Motin static void
317831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot)
318831f5dcfSAlexander Motin {
319831f5dcfSAlexander Motin 
320831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
321831f5dcfSAlexander Motin 
322831f5dcfSAlexander Motin 	/* Enable interrupts. */
323831f5dcfSAlexander Motin 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
324831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
325831f5dcfSAlexander Motin 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
326831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
327831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
3283685b398SWarner Losh 	    SDHCI_INT_ACMD12ERR;
329639f59f0SIan Lepore 
330639f59f0SIan Lepore 	if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
331639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
332639f59f0SIan Lepore 		slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
333639f59f0SIan Lepore 	}
334639f59f0SIan Lepore 
335cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
336831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
337831f5dcfSAlexander Motin }
338831f5dcfSAlexander Motin 
339831f5dcfSAlexander Motin static void
340831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
341831f5dcfSAlexander Motin {
34293efdc63SAdrian Chadd 	uint32_t clk_base;
34393efdc63SAdrian Chadd 	uint32_t clk_sel;
344831f5dcfSAlexander Motin 	uint32_t res;
345831f5dcfSAlexander Motin 	uint16_t clk;
3468f3b7d56SOleksandr Tymoshenko 	uint16_t div;
347831f5dcfSAlexander Motin 	int timeout;
348831f5dcfSAlexander Motin 
349831f5dcfSAlexander Motin 	if (clock == slot->clock)
350831f5dcfSAlexander Motin 		return;
351831f5dcfSAlexander Motin 	slot->clock = clock;
352831f5dcfSAlexander Motin 
353831f5dcfSAlexander Motin 	/* Turn off the clock. */
3544ddc0172SIan Lepore 	clk = RD2(slot, SDHCI_CLOCK_CONTROL);
3554ddc0172SIan Lepore 	WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
356b440e965SMarius Strobl 	/* If no clock requested - leave it so. */
357831f5dcfSAlexander Motin 	if (clock == 0)
358831f5dcfSAlexander Motin 		return;
359ceb9e9f7SIan Lepore 
36093efdc63SAdrian Chadd 	/* Determine the clock base frequency */
36193efdc63SAdrian Chadd 	clk_base = slot->max_clk;
36293efdc63SAdrian Chadd 	if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
3631bacf3beSMarius Strobl 		clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) &
3641bacf3beSMarius Strobl 		    BCM577XX_CTRL_CLKSEL_MASK;
36593efdc63SAdrian Chadd 
3661bacf3beSMarius Strobl 		/*
3671bacf3beSMarius Strobl 		 * Select clock source appropriate for the requested frequency.
3681bacf3beSMarius Strobl 		 */
36993efdc63SAdrian Chadd 		if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
37093efdc63SAdrian Chadd 			clk_base = BCM577XX_ALT_CLOCK_BASE;
3711bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ <<
3721bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
37393efdc63SAdrian Chadd 		} else {
3741bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT <<
3751bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
37693efdc63SAdrian Chadd 		}
37793efdc63SAdrian Chadd 
37893efdc63SAdrian Chadd 		WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
37993efdc63SAdrian Chadd 	}
38093efdc63SAdrian Chadd 
381ceb9e9f7SIan Lepore 	/* Recalculate timeout clock frequency based on the new sd clock. */
382ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
383ceb9e9f7SIan Lepore 		slot->timeout_clk = slot->clock / 1000;
384ceb9e9f7SIan Lepore 
3858f3b7d56SOleksandr Tymoshenko 	if (slot->version < SDHCI_SPEC_300) {
386831f5dcfSAlexander Motin 		/* Looking for highest freq <= clock. */
38793efdc63SAdrian Chadd 		res = clk_base;
38857677a3aSOleksandr Tymoshenko 		for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
389831f5dcfSAlexander Motin 			if (res <= clock)
390831f5dcfSAlexander Motin 				break;
391831f5dcfSAlexander Motin 			res >>= 1;
392831f5dcfSAlexander Motin 		}
393831f5dcfSAlexander Motin 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
3948f3b7d56SOleksandr Tymoshenko 		div >>= 1;
395c11bbc7dSMarius Strobl 	} else {
3968f3b7d56SOleksandr Tymoshenko 		/* Version 3.0 divisors are multiples of two up to 1023 * 2 */
39793efdc63SAdrian Chadd 		if (clock >= clk_base)
39857677a3aSOleksandr Tymoshenko 			div = 0;
3998f3b7d56SOleksandr Tymoshenko 		else {
40057677a3aSOleksandr Tymoshenko 			for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
40193efdc63SAdrian Chadd 				if ((clk_base / div) <= clock)
4028f3b7d56SOleksandr Tymoshenko 					break;
4038f3b7d56SOleksandr Tymoshenko 			}
4048f3b7d56SOleksandr Tymoshenko 		}
4058f3b7d56SOleksandr Tymoshenko 		div >>= 1;
4068f3b7d56SOleksandr Tymoshenko 	}
4078f3b7d56SOleksandr Tymoshenko 
4088f3b7d56SOleksandr Tymoshenko 	if (bootverbose || sdhci_debug)
40993efdc63SAdrian Chadd 		slot_printf(slot, "Divider %d for freq %d (base %d)\n",
41093efdc63SAdrian Chadd 			div, clock, clk_base);
4118f3b7d56SOleksandr Tymoshenko 
412831f5dcfSAlexander Motin 	/* Now we have got divider, set it. */
4138f3b7d56SOleksandr Tymoshenko 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
4148f3b7d56SOleksandr Tymoshenko 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
4158f3b7d56SOleksandr Tymoshenko 		<< SDHCI_DIVIDER_HI_SHIFT;
4168f3b7d56SOleksandr Tymoshenko 
417831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
418831f5dcfSAlexander Motin 	/* Enable clock. */
419831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_INT_EN;
420831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
421831f5dcfSAlexander Motin 	/* Wait up to 10 ms until it stabilize. */
422831f5dcfSAlexander Motin 	timeout = 10;
423831f5dcfSAlexander Motin 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
424831f5dcfSAlexander Motin 		& SDHCI_CLOCK_INT_STABLE)) {
425831f5dcfSAlexander Motin 		if (timeout == 0) {
426831f5dcfSAlexander Motin 			slot_printf(slot,
427831f5dcfSAlexander Motin 			    "Internal clock never stabilised.\n");
428831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
429831f5dcfSAlexander Motin 			return;
430831f5dcfSAlexander Motin 		}
431831f5dcfSAlexander Motin 		timeout--;
432831f5dcfSAlexander Motin 		DELAY(1000);
433831f5dcfSAlexander Motin 	}
434831f5dcfSAlexander Motin 	/* Pass clock signal to the bus. */
435831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_CARD_EN;
436831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
437831f5dcfSAlexander Motin }
438831f5dcfSAlexander Motin 
439831f5dcfSAlexander Motin static void
440831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power)
441831f5dcfSAlexander Motin {
44285083a80SMarius Strobl 	int i;
443831f5dcfSAlexander Motin 	uint8_t pwr;
444831f5dcfSAlexander Motin 
445831f5dcfSAlexander Motin 	if (slot->power == power)
446831f5dcfSAlexander Motin 		return;
447d6b3aaf8SOleksandr Tymoshenko 
448831f5dcfSAlexander Motin 	slot->power = power;
449831f5dcfSAlexander Motin 
450831f5dcfSAlexander Motin 	/* Turn off the power. */
451831f5dcfSAlexander Motin 	pwr = 0;
452831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
453b440e965SMarius Strobl 	/* If power down requested - leave it so. */
454831f5dcfSAlexander Motin 	if (power == 0)
455831f5dcfSAlexander Motin 		return;
456831f5dcfSAlexander Motin 	/* Set voltage. */
457831f5dcfSAlexander Motin 	switch (1 << power) {
458831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
459831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
460831f5dcfSAlexander Motin 		break;
461831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
462831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
463831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
464831f5dcfSAlexander Motin 		break;
465831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
466831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
467831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
468831f5dcfSAlexander Motin 		break;
469831f5dcfSAlexander Motin 	}
470831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
47185083a80SMarius Strobl 	/*
47285083a80SMarius Strobl 	 * Turn on VDD1 power.  Note that at least some Intel controllers can
47385083a80SMarius Strobl 	 * fail to enable bus power on the first try after transiting from D3
4748022c8ebSMarius Strobl 	 * to D0, so we give them up to 2 ms.
47585083a80SMarius Strobl 	 */
476831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
47785083a80SMarius Strobl 	for (i = 0; i < 20; i++) {
478831f5dcfSAlexander Motin 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
47985083a80SMarius Strobl 		if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)
48085083a80SMarius Strobl 			break;
48185083a80SMarius Strobl 		DELAY(100);
48285083a80SMarius Strobl 	}
48385083a80SMarius Strobl 	if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON))
4844d52f81dSIan Lepore 		slot_printf(slot, "Bus power failed to enable\n");
485a2832f9fSMarius Strobl 
486a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) {
487a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10);
488a2832f9fSMarius Strobl 		DELAY(10);
489a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
490a2832f9fSMarius Strobl 		DELAY(300);
491a2832f9fSMarius Strobl 	}
492831f5dcfSAlexander Motin }
493831f5dcfSAlexander Motin 
494831f5dcfSAlexander Motin static void
495831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
496831f5dcfSAlexander Motin {
497831f5dcfSAlexander Motin 	uint32_t data;
498831f5dcfSAlexander Motin 	char *buffer;
499831f5dcfSAlexander Motin 	size_t left;
500831f5dcfSAlexander Motin 
501831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
502831f5dcfSAlexander Motin 	buffer += slot->offset;
503831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
5045d5ae066SIlya Bakulin #ifdef MMCCAM
5055d5ae066SIlya Bakulin 	if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE)
5065d5ae066SIlya Bakulin 		left = min(slot->curcmd->data->block_size,
5075d5ae066SIlya Bakulin 		    slot->curcmd->data->len - slot->offset);
5085d5ae066SIlya Bakulin 	else
5095d5ae066SIlya Bakulin #endif
510831f5dcfSAlexander Motin 		left = min(512, slot->curcmd->data->len - slot->offset);
511831f5dcfSAlexander Motin 	slot->offset += left;
512831f5dcfSAlexander Motin 
513831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
514d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
515831f5dcfSAlexander Motin 		DELAY(10);
516ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
517831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
518831f5dcfSAlexander Motin 		while (left > 3) {
519831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
520831f5dcfSAlexander Motin 			buffer[0] = data;
521831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
522831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
523831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
524831f5dcfSAlexander Motin 			buffer += 4;
525831f5dcfSAlexander Motin 			left -= 4;
526831f5dcfSAlexander Motin 		}
527831f5dcfSAlexander Motin 	} else {
528d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
529831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
530831f5dcfSAlexander Motin 		left &= 3;
531831f5dcfSAlexander Motin 	}
532831f5dcfSAlexander Motin 	/* Handle uneven size case. */
533831f5dcfSAlexander Motin 	if (left > 0) {
534831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
535831f5dcfSAlexander Motin 		while (left > 0) {
536831f5dcfSAlexander Motin 			*(buffer++) = data;
537831f5dcfSAlexander Motin 			data >>= 8;
538831f5dcfSAlexander Motin 			left--;
539831f5dcfSAlexander Motin 		}
540831f5dcfSAlexander Motin 	}
541831f5dcfSAlexander Motin }
542831f5dcfSAlexander Motin 
543831f5dcfSAlexander Motin static void
544831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
545831f5dcfSAlexander Motin {
546831f5dcfSAlexander Motin 	uint32_t data = 0;
547831f5dcfSAlexander Motin 	char *buffer;
548831f5dcfSAlexander Motin 	size_t left;
549831f5dcfSAlexander Motin 
550831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
551831f5dcfSAlexander Motin 	buffer += slot->offset;
552831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
5535d5ae066SIlya Bakulin #ifdef MMCCAM
5545d5ae066SIlya Bakulin 	if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) {
5555d5ae066SIlya Bakulin 		left = min(slot->curcmd->data->block_size,
5565d5ae066SIlya Bakulin 		    slot->curcmd->data->len - slot->offset);
5575d5ae066SIlya Bakulin 	} else
5585d5ae066SIlya Bakulin #endif
559831f5dcfSAlexander Motin 		left = min(512, slot->curcmd->data->len - slot->offset);
560831f5dcfSAlexander Motin 	slot->offset += left;
561831f5dcfSAlexander Motin 
562ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
563831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
564831f5dcfSAlexander Motin 		while (left > 3) {
565831f5dcfSAlexander Motin 			data = buffer[0] +
566831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
567831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
568831f5dcfSAlexander Motin 			    (buffer[3] << 24);
569831f5dcfSAlexander Motin 			left -= 4;
570831f5dcfSAlexander Motin 			buffer += 4;
571831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
572831f5dcfSAlexander Motin 		}
573831f5dcfSAlexander Motin 	} else {
574d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
575831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
576831f5dcfSAlexander Motin 		left &= 3;
577831f5dcfSAlexander Motin 	}
578831f5dcfSAlexander Motin 	/* Handle uneven size case. */
579831f5dcfSAlexander Motin 	if (left > 0) {
580831f5dcfSAlexander Motin 		while (left > 0) {
581831f5dcfSAlexander Motin 			data <<= 8;
582831f5dcfSAlexander Motin 			data += *(buffer++);
583831f5dcfSAlexander Motin 			left--;
584831f5dcfSAlexander Motin 		}
585831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
586831f5dcfSAlexander Motin 	}
587831f5dcfSAlexander Motin }
588831f5dcfSAlexander Motin 
589831f5dcfSAlexander Motin static void
590831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
591831f5dcfSAlexander Motin {
592831f5dcfSAlexander Motin 
593831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
594831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
595831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
596831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
597831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
598831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
599831f5dcfSAlexander Motin 				break;
600831f5dcfSAlexander Motin 		}
601831f5dcfSAlexander Motin 	} else {
602831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
603831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
604831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
605831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
606831f5dcfSAlexander Motin 				break;
607831f5dcfSAlexander Motin 		}
608831f5dcfSAlexander Motin 	}
609831f5dcfSAlexander Motin }
610831f5dcfSAlexander Motin 
611831f5dcfSAlexander Motin static void
6127e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused)
613831f5dcfSAlexander Motin {
614831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
6157e6ccea3SMarius Strobl 	device_t d;
616831f5dcfSAlexander Motin 
617831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
6186e37fb2bSIan Lepore 	if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
619a94a63f0SWarner Losh #ifdef MMCCAM
620a94a63f0SWarner Losh 		if (slot->card_present == 0) {
621a94a63f0SWarner Losh #else
622831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
623a94a63f0SWarner Losh #endif
624831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
625639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
626639f59f0SIan Lepore 				slot_printf(slot, "Card inserted\n");
627a94a63f0SWarner Losh #ifdef MMCCAM
628a94a63f0SWarner Losh 			slot->card_present = 1;
629c7a49948SEmmanuel Vadot 			mmccam_start_discovery(slot->sim);
630a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
631a94a63f0SWarner Losh #else
632aca38eabSMarius Strobl 			d = slot->dev = device_add_child(slot->bus, "mmc", -1);
633831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
634aca38eabSMarius Strobl 			if (d) {
635aca38eabSMarius Strobl 				device_set_ivars(d, slot);
636aca38eabSMarius Strobl 				(void)device_probe_and_attach(d);
637aca38eabSMarius Strobl 			}
638a94a63f0SWarner Losh #endif
639831f5dcfSAlexander Motin 		} else
640831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
641831f5dcfSAlexander Motin 	} else {
642a94a63f0SWarner Losh #ifdef MMCCAM
643a94a63f0SWarner Losh 		if (slot->card_present == 1) {
644a94a63f0SWarner Losh #else
645831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
646a94a63f0SWarner Losh #endif
647831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
648639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
649639f59f0SIan Lepore 				slot_printf(slot, "Card removed\n");
6507e6ccea3SMarius Strobl 			d = slot->dev;
651831f5dcfSAlexander Motin 			slot->dev = NULL;
652a94a63f0SWarner Losh #ifdef MMCCAM
653a94a63f0SWarner Losh 			slot->card_present = 0;
654c7a49948SEmmanuel Vadot 			mmccam_start_discovery(slot->sim);
655a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
656a94a63f0SWarner Losh #else
657aca38eabSMarius Strobl 			slot->intmask &= ~sdhci_tuning_intmask(slot);
658cc22204bSMarius Strobl 			WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
659aca38eabSMarius Strobl 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
660aca38eabSMarius Strobl 			slot->opt &= ~SDHCI_TUNING_ENABLED;
661831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
662aca38eabSMarius Strobl 			callout_drain(&slot->retune_callout);
663d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
664a94a63f0SWarner Losh #endif
665831f5dcfSAlexander Motin 		} else
666831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
667831f5dcfSAlexander Motin 	}
668831f5dcfSAlexander Motin }
669831f5dcfSAlexander Motin 
670b8bf08b1SIan Lepore static void
671b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
672639f59f0SIan Lepore {
673639f59f0SIan Lepore 	bool was_present;
674639f59f0SIan Lepore 
675639f59f0SIan Lepore 	/*
676639f59f0SIan Lepore 	 * If there was no card and now there is one, schedule the task to
677639f59f0SIan Lepore 	 * create the child device after a short delay.  The delay is to
678639f59f0SIan Lepore 	 * debounce the card insert (sometimes the card detect pin stabilizes
679639f59f0SIan Lepore 	 * before the other pins have made good contact).
680639f59f0SIan Lepore 	 *
681639f59f0SIan Lepore 	 * If there was a card present and now it's gone, immediately schedule
682639f59f0SIan Lepore 	 * the task to delete the child device.  No debouncing -- gone is gone,
683639f59f0SIan Lepore 	 * because once power is removed, a full card re-init is needed, and
684639f59f0SIan Lepore 	 * that happens by deleting and recreating the child device.
685639f59f0SIan Lepore 	 */
686a94a63f0SWarner Losh #ifdef MMCCAM
687a94a63f0SWarner Losh 	was_present = slot->card_present;
688a94a63f0SWarner Losh #else
689639f59f0SIan Lepore 	was_present = slot->dev != NULL;
690a94a63f0SWarner Losh #endif
691639f59f0SIan Lepore 	if (!was_present && is_present) {
692639f59f0SIan Lepore 		taskqueue_enqueue_timeout(taskqueue_swi_giant,
693639f59f0SIan Lepore 		    &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
694639f59f0SIan Lepore 	} else if (was_present && !is_present) {
695639f59f0SIan Lepore 		taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
696639f59f0SIan Lepore 	}
697b8bf08b1SIan Lepore }
698b8bf08b1SIan Lepore 
699b8bf08b1SIan Lepore void
700b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
701b8bf08b1SIan Lepore {
702b8bf08b1SIan Lepore 
703b8bf08b1SIan Lepore 	SDHCI_LOCK(slot);
704b8bf08b1SIan Lepore 	sdhci_handle_card_present_locked(slot, is_present);
705639f59f0SIan Lepore 	SDHCI_UNLOCK(slot);
706639f59f0SIan Lepore }
707639f59f0SIan Lepore 
708639f59f0SIan Lepore static void
709639f59f0SIan Lepore sdhci_card_poll(void *arg)
710639f59f0SIan Lepore {
711639f59f0SIan Lepore 	struct sdhci_slot *slot = arg;
712639f59f0SIan Lepore 
713639f59f0SIan Lepore 	sdhci_handle_card_present(slot,
714639f59f0SIan Lepore 	    SDHCI_GET_CARD_PRESENT(slot->bus, slot));
715639f59f0SIan Lepore 	callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
716639f59f0SIan Lepore 	    sdhci_card_poll, slot);
717639f59f0SIan Lepore }
718639f59f0SIan Lepore 
719ab00a509SMarius Strobl static int
720ab00a509SMarius Strobl sdhci_dma_alloc(struct sdhci_slot *slot)
721ab00a509SMarius Strobl {
722ab00a509SMarius Strobl 	int err;
723ab00a509SMarius Strobl 
724ab00a509SMarius Strobl 	if (!(slot->quirks & SDHCI_QUIRK_BROKEN_SDMA_BOUNDARY)) {
725ab00a509SMarius Strobl 		if (MAXPHYS <= 1024 * 4)
726ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_4K;
727ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 8)
728ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_8K;
729ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 16)
730ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_16K;
731ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 32)
732ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_32K;
733ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 64)
734ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_64K;
735ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 128)
736ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_128K;
737ab00a509SMarius Strobl 		else if (MAXPHYS <= 1024 * 256)
738ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_256K;
739ab00a509SMarius Strobl 		else
740ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_512K;
741ab00a509SMarius Strobl 	}
742ab00a509SMarius Strobl 	slot->sdma_bbufsz = SDHCI_SDMA_BNDRY_TO_BBUFSZ(slot->sdma_boundary);
743ab00a509SMarius Strobl 
744ab00a509SMarius Strobl 	/*
745ab00a509SMarius Strobl 	 * Allocate the DMA tag for an SDMA bounce buffer.
746ab00a509SMarius Strobl 	 * Note that the SDHCI specification doesn't state any alignment
747ab00a509SMarius Strobl 	 * constraint for the SDMA system address.  However, controllers
748ab00a509SMarius Strobl 	 * typically ignore the SDMA boundary bits in SDHCI_DMA_ADDRESS when
749ab00a509SMarius Strobl 	 * forming the actual address of data, requiring the SDMA buffer to
750ab00a509SMarius Strobl 	 * be aligned to the SDMA boundary.
751ab00a509SMarius Strobl 	 */
752ab00a509SMarius Strobl 	err = bus_dma_tag_create(bus_get_dma_tag(slot->bus), slot->sdma_bbufsz,
753ab00a509SMarius Strobl 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
754ab00a509SMarius Strobl 	    slot->sdma_bbufsz, 1, slot->sdma_bbufsz, BUS_DMA_ALLOCNOW,
755ab00a509SMarius Strobl 	    NULL, NULL, &slot->dmatag);
756ab00a509SMarius Strobl 	if (err != 0) {
757ab00a509SMarius Strobl 		slot_printf(slot, "Can't create DMA tag for SDMA\n");
758ab00a509SMarius Strobl 		return (err);
759ab00a509SMarius Strobl 	}
760ab00a509SMarius Strobl 	/* Allocate DMA memory for the SDMA bounce buffer. */
761ab00a509SMarius Strobl 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
762ab00a509SMarius Strobl 	    BUS_DMA_NOWAIT, &slot->dmamap);
763ab00a509SMarius Strobl 	if (err != 0) {
764ab00a509SMarius Strobl 		slot_printf(slot, "Can't alloc DMA memory for SDMA\n");
765ab00a509SMarius Strobl 		bus_dma_tag_destroy(slot->dmatag);
766ab00a509SMarius Strobl 		return (err);
767ab00a509SMarius Strobl 	}
768ab00a509SMarius Strobl 	/* Map the memory of the SDMA bounce buffer. */
769ab00a509SMarius Strobl 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
770ab00a509SMarius Strobl 	    (void *)slot->dmamem, slot->sdma_bbufsz, sdhci_getaddr,
771ab00a509SMarius Strobl 	    &slot->paddr, 0);
772ab00a509SMarius Strobl 	if (err != 0 || slot->paddr == 0) {
773ab00a509SMarius Strobl 		slot_printf(slot, "Can't load DMA memory for SDMA\n");
774ab00a509SMarius Strobl 		bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
775ab00a509SMarius Strobl 		bus_dma_tag_destroy(slot->dmatag);
776ab00a509SMarius Strobl 		if (err)
777ab00a509SMarius Strobl 			return (err);
778ab00a509SMarius Strobl 		else
779ab00a509SMarius Strobl 			return (EFAULT);
780ab00a509SMarius Strobl 	}
781ab00a509SMarius Strobl 
782ab00a509SMarius Strobl 	return (0);
783ab00a509SMarius Strobl }
784ab00a509SMarius Strobl 
785ab00a509SMarius Strobl static void
786ab00a509SMarius Strobl sdhci_dma_free(struct sdhci_slot *slot)
787ab00a509SMarius Strobl {
788ab00a509SMarius Strobl 
789ab00a509SMarius Strobl 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
790ab00a509SMarius Strobl 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
791ab00a509SMarius Strobl 	bus_dma_tag_destroy(slot->dmatag);
792ab00a509SMarius Strobl }
793ab00a509SMarius Strobl 
794d6b3aaf8SOleksandr Tymoshenko int
795d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
796831f5dcfSAlexander Motin {
797aca38eabSMarius Strobl 	kobjop_desc_t kobj_desc;
798aca38eabSMarius Strobl 	kobj_method_t *kobj_method;
7990f34084fSMarius Strobl 	uint32_t caps, caps2, freq, host_caps;
800d6b3aaf8SOleksandr Tymoshenko 	int err;
801831f5dcfSAlexander Motin 
802831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
803a94a63f0SWarner Losh 
804d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
805d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
806d6b3aaf8SOleksandr Tymoshenko 
807d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
808d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
8090f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) {
8108f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
8110f34084fSMarius Strobl 		caps2 = slot->caps2;
8120f34084fSMarius Strobl 	} else {
813831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
8140f34084fSMarius Strobl 		if (slot->version >= SDHCI_SPEC_300)
8150f34084fSMarius Strobl 			caps2 = RD4(slot, SDHCI_CAPABILITIES2);
8160f34084fSMarius Strobl 		else
8170f34084fSMarius Strobl 			caps2 = 0;
8180f34084fSMarius Strobl 	}
8197fcf4780SMarius Strobl 	if (slot->version >= SDHCI_SPEC_300) {
8207fcf4780SMarius Strobl 		if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE &&
8217fcf4780SMarius Strobl 		    (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) {
822ab00a509SMarius Strobl 			slot_printf(slot,
8237fcf4780SMarius Strobl 			    "Driver doesn't support shared bus slots\n");
8247fcf4780SMarius Strobl 			SDHCI_LOCK_DESTROY(slot);
8257fcf4780SMarius Strobl 			return (ENXIO);
8267fcf4780SMarius Strobl 		} else if ((caps & SDHCI_SLOTTYPE_MASK) ==
8277fcf4780SMarius Strobl 		    SDHCI_SLOTTYPE_EMBEDDED) {
8287fcf4780SMarius Strobl 			slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE;
8297fcf4780SMarius Strobl 		}
8307fcf4780SMarius Strobl 	}
831831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
83233aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
83387a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
83487a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
83533aad34dSOleksandr Tymoshenko 	else
83687a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
83787a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
83887a6a871SIan Lepore 	if (freq != 0)
83987a6a871SIan Lepore 		slot->max_clk = freq * 1000000;
84087a6a871SIan Lepore 	/*
84187a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
84287a6a871SIan Lepore 	 * hasn't already set max_clk we're probably not going to work right
84387a6a871SIan Lepore 	 * with an assumption, so complain about it.
84487a6a871SIan Lepore 	 */
845831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
84687a6a871SIan Lepore 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
847ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't specify base clock "
8481bacf3beSMarius Strobl 		    "frequency, using %dMHz as default.\n",
8491bacf3beSMarius Strobl 		    SDHCI_DEFAULT_MAX_FREQ);
850831f5dcfSAlexander Motin 	}
851a2832f9fSMarius Strobl 	/* Calculate/set timeout clock frequency. */
8528f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
8538f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
854a2832f9fSMarius Strobl 	} else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) {
855a2832f9fSMarius Strobl 		slot->timeout_clk = 1000;
8568f3b7d56SOleksandr Tymoshenko 	} else {
8571bacf3beSMarius Strobl 		slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >>
8581bacf3beSMarius Strobl 		    SDHCI_TIMEOUT_CLK_SHIFT;
8598f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
8608f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
8618f3b7d56SOleksandr Tymoshenko 	}
86287a6a871SIan Lepore 	/*
86387a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
86487a6a871SIan Lepore 	 * hasn't already set timeout_clk we'll probably work okay using the
86587a6a871SIan Lepore 	 * max timeout, but still mention it.
86687a6a871SIan Lepore 	 */
867831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
868ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't specify timeout clock "
869ceb9e9f7SIan Lepore 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
870ceb9e9f7SIan Lepore 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
871831f5dcfSAlexander Motin 	}
872831f5dcfSAlexander Motin 
87357677a3aSOleksandr Tymoshenko 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
874831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
875831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
876831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
877831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
878831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
879831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
88049dfdf63SIan Lepore 	/*
88149dfdf63SIan Lepore 	 * 1.8V VDD is not supposed to be used for removable cards.  Hardware
88249dfdf63SIan Lepore 	 * prior to v3.0 had no way to indicate embedded slots, but did
88349dfdf63SIan Lepore 	 * sometimes support 1.8v for non-removable devices.
88449dfdf63SIan Lepore 	 */
88549dfdf63SIan Lepore 	if ((caps & SDHCI_CAN_VDD_180) && (slot->version < SDHCI_SPEC_300 ||
88649dfdf63SIan Lepore 	    (slot->opt & SDHCI_SLOT_EMBEDDED)))
887831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
888831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
889ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't report any "
890831f5dcfSAlexander Motin 		    "support voltages.\n");
891831f5dcfSAlexander Motin 	}
892aca38eabSMarius Strobl 
8930f34084fSMarius Strobl 	host_caps = MMC_CAP_4_BIT_DATA;
8942d1731b8SIan Lepore 	if (caps & SDHCI_CAN_DO_8BITBUS)
8950f34084fSMarius Strobl 		host_caps |= MMC_CAP_8_BIT_DATA;
896831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
8970f34084fSMarius Strobl 		host_caps |= MMC_CAP_HSPEED;
89872dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC)
8990f34084fSMarius Strobl 		host_caps |= MMC_CAP_BOOT_NOACC;
90072dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY)
9010f34084fSMarius Strobl 		host_caps |= MMC_CAP_WAIT_WHILE_BUSY;
902aca38eabSMarius Strobl 
903aca38eabSMarius Strobl 	/* Determine supported UHS-I and eMMC modes. */
9040f34084fSMarius Strobl 	if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50))
9050f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
9060f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_SDR104) {
9070f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
9080f34084fSMarius Strobl 		if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200))
9090f34084fSMarius Strobl 			host_caps |= MMC_CAP_MMC_HS200;
9100f34084fSMarius Strobl 	} else if (caps2 & SDHCI_CAN_SDR50)
9110f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR50;
9120f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_DDR50 &&
9130f34084fSMarius Strobl 	    !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50))
9140f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_DDR50;
9150f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_DDR52)
9160f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_DDR52;
9170f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 &&
9180f34084fSMarius Strobl 	    caps2 & SDHCI_CAN_MMC_HS400)
9190f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
920835998c2SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 &&
921835998c2SMarius Strobl 	    caps2 & SDHCI_CAN_SDR104)
922835998c2SMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
923aca38eabSMarius Strobl 
924aca38eabSMarius Strobl 	/*
925aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes if the set_uhs_timing method is the
926aca38eabSMarius Strobl 	 * default NULL implementation.
927aca38eabSMarius Strobl 	 */
928aca38eabSMarius Strobl 	kobj_desc = &sdhci_set_uhs_timing_desc;
929aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
930aca38eabSMarius Strobl 	    kobj_desc);
931aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
932aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
933aca38eabSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
934aca38eabSMarius Strobl 		    MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400);
935aca38eabSMarius Strobl 
936aca38eabSMarius Strobl #define	SDHCI_CAP_MODES_TUNING(caps2)					\
937aca38eabSMarius Strobl     (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) |		\
938aca38eabSMarius Strobl     MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 |	\
939aca38eabSMarius Strobl     MMC_CAP_MMC_HS400)
940aca38eabSMarius Strobl 
941aca38eabSMarius Strobl 	/*
942aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes that require (re-)tuning if either
943aca38eabSMarius Strobl 	 * the tune or re-tune method is the default NULL implementation.
944aca38eabSMarius Strobl 	 */
945aca38eabSMarius Strobl 	kobj_desc = &mmcbr_tune_desc;
946aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
947aca38eabSMarius Strobl 	    kobj_desc);
948aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
949aca38eabSMarius Strobl 		goto no_tuning;
950aca38eabSMarius Strobl 	kobj_desc = &mmcbr_retune_desc;
951aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
952aca38eabSMarius Strobl 	    kobj_desc);
953aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt) {
954aca38eabSMarius Strobl no_tuning:
955aca38eabSMarius Strobl 		host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2));
956aca38eabSMarius Strobl 	}
957aca38eabSMarius Strobl 
958aca38eabSMarius Strobl 	/* Allocate tuning structures and determine tuning parameters. */
959aca38eabSMarius Strobl 	if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) {
960aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_SUPPORTED;
961aca38eabSMarius Strobl 		slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF,
962aca38eabSMarius Strobl 		    M_WAITOK);
963aca38eabSMarius Strobl 		slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF,
964aca38eabSMarius Strobl 		    M_WAITOK);
965aca38eabSMarius Strobl 		slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF,
966aca38eabSMarius Strobl 		    M_WAITOK);
967aca38eabSMarius Strobl 		if (caps2 & SDHCI_TUNE_SDR50)
968aca38eabSMarius Strobl 			slot->opt |= SDHCI_SDR50_NEEDS_TUNING;
969aca38eabSMarius Strobl 		slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >>
970aca38eabSMarius Strobl 		    SDHCI_RETUNE_MODES_SHIFT;
971aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_1) {
972aca38eabSMarius Strobl 			slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >>
973aca38eabSMarius Strobl 			    SDHCI_RETUNE_CNT_SHIFT;
974aca38eabSMarius Strobl 			if (slot->retune_count > 0xb) {
975ab00a509SMarius Strobl 				slot_printf(slot, "Unknown re-tuning count "
976aca38eabSMarius Strobl 				    "%x, using 1 sec\n", slot->retune_count);
977aca38eabSMarius Strobl 				slot->retune_count = 1;
978aca38eabSMarius Strobl 			} else if (slot->retune_count != 0)
979aca38eabSMarius Strobl 				slot->retune_count =
980aca38eabSMarius Strobl 				    1 << (slot->retune_count - 1);
981aca38eabSMarius Strobl 		}
982aca38eabSMarius Strobl 	}
983aca38eabSMarius Strobl 
984aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING
985aca38eabSMarius Strobl 
986aca38eabSMarius Strobl 	/* Determine supported VCCQ signaling levels. */
9870f34084fSMarius Strobl 	host_caps |= MMC_CAP_SIGNALING_330;
9880f34084fSMarius Strobl 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
989aca38eabSMarius Strobl 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
9900f34084fSMarius Strobl 	    MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 |
9910f34084fSMarius Strobl 	    MMC_CAP_MMC_HS400_180))
992aca38eabSMarius Strobl 		host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180;
993aca38eabSMarius Strobl 
994aca38eabSMarius Strobl 	/*
995aca38eabSMarius Strobl 	 * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the
996aca38eabSMarius Strobl 	 * default NULL implementation.  Disable 1.2 V support if it's the
997aca38eabSMarius Strobl 	 * generic SDHCI implementation.
998aca38eabSMarius Strobl 	 */
999aca38eabSMarius Strobl 	kobj_desc = &mmcbr_switch_vccq_desc;
1000aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
1001aca38eabSMarius Strobl 	    kobj_desc);
1002aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
1003aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180);
1004aca38eabSMarius Strobl 	else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq)
1005aca38eabSMarius Strobl 		host_caps &= ~MMC_CAP_SIGNALING_120;
1006aca38eabSMarius Strobl 
1007aca38eabSMarius Strobl 	/* Determine supported driver types (type B is always mandatory). */
1008f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_A)
10090f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_A;
1010f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_C)
10110f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_C;
1012f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_D)
10130f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_D;
10140f34084fSMarius Strobl 	slot->host.caps = host_caps;
10150f34084fSMarius Strobl 
1016831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
1017831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
1018831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
1019d6b3aaf8SOleksandr Tymoshenko 
1020d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
1021831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
1022d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
1023831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
1024a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE)
1025a2832f9fSMarius Strobl 		slot->opt |= SDHCI_NON_REMOVABLE;
1026831f5dcfSAlexander Motin 
1027c3a0f75aSOleksandr Tymoshenko 	/*
1028c3a0f75aSOleksandr Tymoshenko 	 * Use platform-provided transfer backend
1029c3a0f75aSOleksandr Tymoshenko 	 * with PIO as a fallback mechanism
1030c3a0f75aSOleksandr Tymoshenko 	 */
1031c3a0f75aSOleksandr Tymoshenko 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
1032c3a0f75aSOleksandr Tymoshenko 		slot->opt &= ~SDHCI_HAVE_DMA;
1033c3a0f75aSOleksandr Tymoshenko 
1034ab00a509SMarius Strobl 	if (slot->opt & SDHCI_HAVE_DMA) {
1035ab00a509SMarius Strobl 		err = sdhci_dma_alloc(slot);
1036ab00a509SMarius Strobl 		if (err != 0) {
1037ab00a509SMarius Strobl 			if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1038ab00a509SMarius Strobl 				free(slot->tune_req, M_DEVBUF);
1039ab00a509SMarius Strobl 				free(slot->tune_cmd, M_DEVBUF);
1040ab00a509SMarius Strobl 				free(slot->tune_data, M_DEVBUF);
1041ab00a509SMarius Strobl 			}
1042ab00a509SMarius Strobl 			SDHCI_LOCK_DESTROY(slot);
1043ab00a509SMarius Strobl 			return (err);
1044ab00a509SMarius Strobl 		}
1045ab00a509SMarius Strobl 	}
1046ab00a509SMarius Strobl 
10475b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
10480f34084fSMarius Strobl 		slot_printf(slot,
10497fcf4780SMarius Strobl 		    "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n",
1050831f5dcfSAlexander Motin 		    slot->max_clk / 1000000,
1051831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
10520f34084fSMarius Strobl 		    (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
10530f34084fSMarius Strobl 			((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
1054831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
1055831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
10567fcf4780SMarius Strobl 		    ((caps & SDHCI_CAN_VDD_180) &&
10577fcf4780SMarius Strobl 		    (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "",
10580f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "",
10590f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "",
1060aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "",
1061aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "",
1062aca38eabSMarius Strobl 		    (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "",
10637fcf4780SMarius Strobl 		    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO",
10647fcf4780SMarius Strobl 		    (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" :
10657fcf4780SMarius Strobl 		    (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" :
10667fcf4780SMarius Strobl 		    "removable");
10670f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 |
10680f34084fSMarius Strobl 		    MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE))
10690f34084fSMarius Strobl 			slot_printf(slot, "eMMC:%s%s%s%s\n",
10700f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "",
10710f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "",
10720f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "",
10730f34084fSMarius Strobl 			    ((host_caps &
10740f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ==
10750f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ?
10760f34084fSMarius Strobl 			    " HS400ES" : "");
10770f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
10780f34084fSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104))
10790f34084fSMarius Strobl 			slot_printf(slot, "UHS-I:%s%s%s%s%s\n",
10800f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "",
10810f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "",
10820f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "",
10830f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "",
10840f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : "");
1085aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_SUPPORTED)
1086aca38eabSMarius Strobl 			slot_printf(slot, "Re-tuning count %d secs, mode %d\n",
1087aca38eabSMarius Strobl 			    slot->retune_count, slot->retune_mode + 1);
1088831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1089831f5dcfSAlexander Motin 	}
1090831f5dcfSAlexander Motin 
1091ba6fc1c7SLuiz Otavio O Souza 	slot->timeout = 10;
1092ba6fc1c7SLuiz Otavio O Souza 	SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
1093ba6fc1c7SLuiz Otavio O Souza 	    SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
10944d52f81dSIan Lepore 	    "timeout", CTLFLAG_RWTUN, &slot->timeout, 0,
1095ba6fc1c7SLuiz Otavio O Souza 	    "Maximum timeout for SDHCI transfers (in secs)");
1096831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
1097639f59f0SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
1098639f59f0SIan Lepore 		sdhci_card_task, slot);
1099639f59f0SIan Lepore 	callout_init(&slot->card_poll_callout, 1);
1100e64f01a9SIan Lepore 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
1101aca38eabSMarius Strobl 	callout_init_mtx(&slot->retune_callout, &slot->mtx, 0);
1102ba6fc1c7SLuiz Otavio O Souza 
1103639f59f0SIan Lepore 	if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
1104639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
1105639f59f0SIan Lepore 		callout_reset(&slot->card_poll_callout,
1106639f59f0SIan Lepore 		    SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
1107639f59f0SIan Lepore 	}
1108639f59f0SIan Lepore 
1109aca38eabSMarius Strobl 	sdhci_init(slot);
1110aca38eabSMarius Strobl 
1111831f5dcfSAlexander Motin 	return (0);
1112831f5dcfSAlexander Motin }
1113831f5dcfSAlexander Motin 
1114d91f1a10SIlya Bakulin #ifndef MMCCAM
1115d6b3aaf8SOleksandr Tymoshenko void
1116d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
1117831f5dcfSAlexander Motin {
11187e6ccea3SMarius Strobl 
1119d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
1120d6b3aaf8SOleksandr Tymoshenko }
1121d91f1a10SIlya Bakulin #endif
1122831f5dcfSAlexander Motin 
1123d6b3aaf8SOleksandr Tymoshenko int
1124d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
1125d6b3aaf8SOleksandr Tymoshenko {
1126831f5dcfSAlexander Motin 	device_t d;
1127831f5dcfSAlexander Motin 
1128e64f01a9SIan Lepore 	callout_drain(&slot->timeout_callout);
1129639f59f0SIan Lepore 	callout_drain(&slot->card_poll_callout);
1130aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1131831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
1132639f59f0SIan Lepore 	taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
1133831f5dcfSAlexander Motin 
1134831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1135831f5dcfSAlexander Motin 	d = slot->dev;
1136831f5dcfSAlexander Motin 	slot->dev = NULL;
1137831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1138831f5dcfSAlexander Motin 	if (d != NULL)
1139d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
1140831f5dcfSAlexander Motin 
1141831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1142831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
1143831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1144ab00a509SMarius Strobl 	if (slot->opt & SDHCI_HAVE_DMA)
1145ab00a509SMarius Strobl 		sdhci_dma_free(slot);
1146aca38eabSMarius Strobl 	if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1147aca38eabSMarius Strobl 		free(slot->tune_req, M_DEVBUF);
1148aca38eabSMarius Strobl 		free(slot->tune_cmd, M_DEVBUF);
1149aca38eabSMarius Strobl 		free(slot->tune_data, M_DEVBUF);
1150aca38eabSMarius Strobl 	}
1151d6b3aaf8SOleksandr Tymoshenko 
1152831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
1153d6b3aaf8SOleksandr Tymoshenko 
1154831f5dcfSAlexander Motin 	return (0);
1155831f5dcfSAlexander Motin }
1156831f5dcfSAlexander Motin 
1157d6b3aaf8SOleksandr Tymoshenko int
1158d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
115992bf0e27SAlexander Motin {
11607e6ccea3SMarius Strobl 
1161aca38eabSMarius Strobl 	/*
1162aca38eabSMarius Strobl 	 * We expect the MMC layer to issue initial tuning after resume.
1163aca38eabSMarius Strobl 	 * Otherwise, we'd need to indicate re-tuning including circuit reset
1164aca38eabSMarius Strobl 	 * being required at least for re-tuning modes 1 and 2 ourselves.
1165aca38eabSMarius Strobl 	 */
1166aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1167aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1168aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1169d6b3aaf8SOleksandr Tymoshenko 	sdhci_reset(slot, SDHCI_RESET_ALL);
1170aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
117192bf0e27SAlexander Motin 
117292bf0e27SAlexander Motin 	return (0);
117392bf0e27SAlexander Motin }
117492bf0e27SAlexander Motin 
1175d6b3aaf8SOleksandr Tymoshenko int
1176d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
117792bf0e27SAlexander Motin {
11787e6ccea3SMarius Strobl 
1179aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1180d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
1181aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
118292bf0e27SAlexander Motin 
1183d6b3aaf8SOleksandr Tymoshenko 	return (0);
118492bf0e27SAlexander Motin }
118592bf0e27SAlexander Motin 
118657677a3aSOleksandr Tymoshenko uint32_t
1187b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot)
118857677a3aSOleksandr Tymoshenko {
11897e6ccea3SMarius Strobl 
119057677a3aSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
119157677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
119257677a3aSOleksandr Tymoshenko 	else
119357677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
119457677a3aSOleksandr Tymoshenko }
119557677a3aSOleksandr Tymoshenko 
11966e37fb2bSIan Lepore bool
1197b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot)
11986e37fb2bSIan Lepore {
11996e37fb2bSIan Lepore 
1200639f59f0SIan Lepore 	if (slot->opt & SDHCI_NON_REMOVABLE)
1201639f59f0SIan Lepore 		return true;
1202639f59f0SIan Lepore 
12036e37fb2bSIan Lepore 	return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
12046e37fb2bSIan Lepore }
12056e37fb2bSIan Lepore 
12060f34084fSMarius Strobl void
12070f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot)
12080f34084fSMarius Strobl {
1209ab00a509SMarius Strobl 	const struct mmc_ios *ios;
12100f34084fSMarius Strobl 	uint16_t hostctrl2;
12110f34084fSMarius Strobl 
12120f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
12130f34084fSMarius Strobl 		return;
12140f34084fSMarius Strobl 
1215aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
12160f34084fSMarius Strobl 	ios = &slot->host.ios;
12170f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
12180f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
12190f34084fSMarius Strobl 	hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK;
1220aca38eabSMarius Strobl 	if (ios->clock > SD_SDR50_MAX) {
12210f34084fSMarius Strobl 		if (ios->timing == bus_timing_mmc_hs400 ||
12220f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_hs400es)
12230f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_MMC_HS400;
1224aca38eabSMarius Strobl 		else
12250f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR104;
1226aca38eabSMarius Strobl 	}
12270f34084fSMarius Strobl 	else if (ios->clock > SD_SDR25_MAX)
12280f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR50;
12290f34084fSMarius Strobl 	else if (ios->clock > SD_SDR12_MAX) {
12300f34084fSMarius Strobl 		if (ios->timing == bus_timing_uhs_ddr50 ||
12310f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_ddr52)
12320f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_DDR50;
12330f34084fSMarius Strobl 		else
12340f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR25;
12350f34084fSMarius Strobl 	} else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY)
12360f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR12;
12370f34084fSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
12380f34084fSMarius Strobl 	sdhci_set_clock(slot, ios->clock);
12390f34084fSMarius Strobl }
12400f34084fSMarius Strobl 
1241d6b3aaf8SOleksandr Tymoshenko int
1242d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
1243831f5dcfSAlexander Motin {
1244831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1245831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
1246831f5dcfSAlexander Motin 
1247831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1248831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
1249831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
1250831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
1251831f5dcfSAlexander Motin 		sdhci_init(slot);
1252831f5dcfSAlexander Motin 	}
1253831f5dcfSAlexander Motin 	/* Configure the bus. */
1254831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
1255831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
12562d1731b8SIan Lepore 	if (ios->bus_width == bus_width_8) {
12572d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
1258831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
12592d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_4) {
12602d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
12612d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
12622d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_1) {
12632d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
12642d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
12652d1731b8SIan Lepore 	} else {
12662d1731b8SIan Lepore 		panic("Invalid bus width: %d", ios->bus_width);
12672d1731b8SIan Lepore 	}
12680f34084fSMarius Strobl 	if (ios->clock > SD_SDR12_MAX &&
1269bba987dcSIan Lepore 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
1270831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
1271831f5dcfSAlexander Motin 	else
1272831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
1273831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
12740f34084fSMarius Strobl 	SDHCI_SET_UHS_TIMING(brdev, slot);
1275831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
1276d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
1277831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1278831f5dcfSAlexander Motin 
1279831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1280831f5dcfSAlexander Motin 	return (0);
1281831f5dcfSAlexander Motin }
1282831f5dcfSAlexander Motin 
12830f34084fSMarius Strobl int
12840f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev)
12850f34084fSMarius Strobl {
12860f34084fSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
12870f34084fSMarius Strobl 	enum mmc_vccq vccq;
12880f34084fSMarius Strobl 	int err;
12890f34084fSMarius Strobl 	uint16_t hostctrl2;
12900f34084fSMarius Strobl 
12910f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
12920f34084fSMarius Strobl 		return (0);
12930f34084fSMarius Strobl 
12940f34084fSMarius Strobl 	err = 0;
12950f34084fSMarius Strobl 	vccq = slot->host.ios.vccq;
12960f34084fSMarius Strobl 	SDHCI_LOCK(slot);
12970f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
12980f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
12990f34084fSMarius Strobl 	switch (vccq) {
13000f34084fSMarius Strobl 	case vccq_330:
13010f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
13020f34084fSMarius Strobl 			goto done;
13030f34084fSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE;
13040f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
13050f34084fSMarius Strobl 		DELAY(5000);
13060f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
13070f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
13080f34084fSMarius Strobl 			goto done;
13090f34084fSMarius Strobl 		err = EAGAIN;
13100f34084fSMarius Strobl 		break;
13110f34084fSMarius Strobl 	case vccq_180:
13120f34084fSMarius Strobl 		if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) {
13130f34084fSMarius Strobl 			err = EINVAL;
13140f34084fSMarius Strobl 			goto done;
13150f34084fSMarius Strobl 		}
13160f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
13170f34084fSMarius Strobl 			goto done;
13180f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_S18_ENABLE;
13190f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
13200f34084fSMarius Strobl 		DELAY(5000);
13210f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
13220f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
13230f34084fSMarius Strobl 			goto done;
13240f34084fSMarius Strobl 		err = EAGAIN;
13250f34084fSMarius Strobl 		break;
13260f34084fSMarius Strobl 	default:
13270f34084fSMarius Strobl 		slot_printf(slot,
13280f34084fSMarius Strobl 		    "Attempt to set unsupported signaling voltage\n");
13290f34084fSMarius Strobl 		err = EINVAL;
13300f34084fSMarius Strobl 		break;
13310f34084fSMarius Strobl 	}
13320f34084fSMarius Strobl done:
13330f34084fSMarius Strobl 	sdhci_set_clock(slot, slot->host.ios.clock);
13340f34084fSMarius Strobl 	SDHCI_UNLOCK(slot);
13350f34084fSMarius Strobl 	return (err);
13360f34084fSMarius Strobl }
13370f34084fSMarius Strobl 
1338aca38eabSMarius Strobl int
1339aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400)
1340aca38eabSMarius Strobl {
1341aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1342ab00a509SMarius Strobl 	const struct mmc_ios *ios = &slot->host.ios;
1343aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1344aca38eabSMarius Strobl 	struct mmc_data *tune_data;
1345aca38eabSMarius Strobl 	uint32_t opcode;
1346aca38eabSMarius Strobl 	int err;
1347aca38eabSMarius Strobl 
1348aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_SUPPORTED))
1349aca38eabSMarius Strobl 		return (0);
1350aca38eabSMarius Strobl 
1351aca38eabSMarius Strobl 	slot->retune_ticks = slot->retune_count * hz;
1352aca38eabSMarius Strobl 	opcode = MMC_SEND_TUNING_BLOCK;
1353aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1354aca38eabSMarius Strobl 	switch (ios->timing) {
1355aca38eabSMarius Strobl 	case bus_timing_mmc_hs400:
1356aca38eabSMarius Strobl 		slot_printf(slot, "HS400 must be tuned in HS200 mode\n");
1357aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1358aca38eabSMarius Strobl 		return (EINVAL);
1359aca38eabSMarius Strobl 	case bus_timing_mmc_hs200:
1360aca38eabSMarius Strobl 		/*
1361aca38eabSMarius Strobl 		 * In HS400 mode, controllers use the data strobe line to
1362aca38eabSMarius Strobl 		 * latch data from the devices so periodic re-tuning isn't
1363aca38eabSMarius Strobl 		 * expected to be required.
1364aca38eabSMarius Strobl 		 */
1365aca38eabSMarius Strobl 		if (hs400)
1366aca38eabSMarius Strobl 			slot->retune_ticks = 0;
1367aca38eabSMarius Strobl 		opcode = MMC_SEND_TUNING_BLOCK_HS200;
1368aca38eabSMarius Strobl 		break;
1369aca38eabSMarius Strobl 	case bus_timing_uhs_ddr50:
1370aca38eabSMarius Strobl 	case bus_timing_uhs_sdr104:
1371aca38eabSMarius Strobl 		break;
1372aca38eabSMarius Strobl 	case bus_timing_uhs_sdr50:
1373aca38eabSMarius Strobl 		if (slot->opt & SDHCI_SDR50_NEEDS_TUNING)
1374aca38eabSMarius Strobl 			break;
1375aca38eabSMarius Strobl 		/* FALLTHROUGH */
1376aca38eabSMarius Strobl 	default:
1377aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1378aca38eabSMarius Strobl 		return (0);
1379aca38eabSMarius Strobl 	}
1380aca38eabSMarius Strobl 
1381aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1382aca38eabSMarius Strobl 	memset(tune_cmd, 0, sizeof(*tune_cmd));
1383aca38eabSMarius Strobl 	tune_cmd->opcode = opcode;
1384aca38eabSMarius Strobl 	tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1385aca38eabSMarius Strobl 	tune_data = tune_cmd->data = slot->tune_data;
1386aca38eabSMarius Strobl 	memset(tune_data, 0, sizeof(*tune_data));
1387aca38eabSMarius Strobl 	tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
1388aca38eabSMarius Strobl 	    ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 :
1389aca38eabSMarius Strobl 	    MMC_TUNING_LEN;
1390aca38eabSMarius Strobl 	tune_data->flags = MMC_DATA_READ;
1391aca38eabSMarius Strobl 	tune_data->mrq = tune_cmd->mrq = slot->tune_req;
1392aca38eabSMarius Strobl 
1393aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1394aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, true);
1395aca38eabSMarius Strobl 	if (err == 0) {
1396aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_ENABLED;
1397aca38eabSMarius Strobl 		slot->intmask |= sdhci_tuning_intmask(slot);
1398cc22204bSMarius Strobl 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
1399aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1400aca38eabSMarius Strobl 		if (slot->retune_ticks) {
1401aca38eabSMarius Strobl 			callout_reset(&slot->retune_callout, slot->retune_ticks,
1402aca38eabSMarius Strobl 			    sdhci_retune, slot);
1403aca38eabSMarius Strobl 		}
1404aca38eabSMarius Strobl 	}
1405aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1406aca38eabSMarius Strobl 	return (err);
1407aca38eabSMarius Strobl }
1408aca38eabSMarius Strobl 
1409aca38eabSMarius Strobl int
1410aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset)
1411aca38eabSMarius Strobl {
1412aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1413aca38eabSMarius Strobl 	int err;
1414aca38eabSMarius Strobl 
1415aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_ENABLED))
1416aca38eabSMarius Strobl 		return (0);
1417aca38eabSMarius Strobl 
1418aca38eabSMarius Strobl 	/* HS400 must be tuned in HS200 mode. */
1419aca38eabSMarius Strobl 	if (slot->host.ios.timing == bus_timing_mmc_hs400)
1420aca38eabSMarius Strobl 		return (EINVAL);
1421aca38eabSMarius Strobl 
1422aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1423aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, reset);
1424aca38eabSMarius Strobl 	/*
1425aca38eabSMarius Strobl 	 * There are two ways sdhci_exec_tuning() can fail:
1426aca38eabSMarius Strobl 	 * EBUSY should not actually happen when requests are only issued
1427aca38eabSMarius Strobl 	 *	 with the host properly acquired, and
1428aca38eabSMarius Strobl 	 * EIO   re-tuning failed (but it did work initially).
1429aca38eabSMarius Strobl 	 *
1430aca38eabSMarius Strobl 	 * In both cases, we should retry at later point if periodic re-tuning
1431aca38eabSMarius Strobl 	 * is enabled.  Note that due to slot->retune_req not being cleared in
1432aca38eabSMarius Strobl 	 * these failure cases, the MMC layer should trigger another attempt at
1433aca38eabSMarius Strobl 	 * re-tuning with the next request anyway, though.
1434aca38eabSMarius Strobl 	 */
1435aca38eabSMarius Strobl 	if (slot->retune_ticks) {
1436aca38eabSMarius Strobl 		callout_reset(&slot->retune_callout, slot->retune_ticks,
1437aca38eabSMarius Strobl 		    sdhci_retune, slot);
1438aca38eabSMarius Strobl 	}
1439aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1440aca38eabSMarius Strobl 	return (err);
1441aca38eabSMarius Strobl }
1442aca38eabSMarius Strobl 
1443aca38eabSMarius Strobl static int
1444aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset)
1445aca38eabSMarius Strobl {
1446aca38eabSMarius Strobl 	struct mmc_request *tune_req;
1447aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1448aca38eabSMarius Strobl 	int i;
1449aca38eabSMarius Strobl 	uint32_t intmask;
1450aca38eabSMarius Strobl 	uint16_t hostctrl2;
1451aca38eabSMarius Strobl 	u_char opt;
1452aca38eabSMarius Strobl 
1453aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
1454aca38eabSMarius Strobl 	if (slot->req != NULL)
1455aca38eabSMarius Strobl 		return (EBUSY);
1456aca38eabSMarius Strobl 
1457aca38eabSMarius Strobl 	/* Tuning doesn't work with DMA enabled. */
1458aca38eabSMarius Strobl 	opt = slot->opt;
1459aca38eabSMarius Strobl 	slot->opt = opt & ~SDHCI_HAVE_DMA;
1460aca38eabSMarius Strobl 
1461aca38eabSMarius Strobl 	/*
1462aca38eabSMarius Strobl 	 * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only
1463aca38eabSMarius Strobl 	 * kind of interrupt we receive in response to a tuning request.
1464aca38eabSMarius Strobl 	 */
1465aca38eabSMarius Strobl 	intmask = slot->intmask;
1466aca38eabSMarius Strobl 	slot->intmask = SDHCI_INT_DATA_AVAIL;
1467cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL);
1468aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL);
1469aca38eabSMarius Strobl 
1470aca38eabSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1471aca38eabSMarius Strobl 	if (reset)
1472aca38eabSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK;
1473aca38eabSMarius Strobl 	else
1474aca38eabSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK;
1475aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING);
1476aca38eabSMarius Strobl 
1477aca38eabSMarius Strobl 	tune_req = slot->tune_req;
1478aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1479aca38eabSMarius Strobl 	for (i = 0; i < MMC_TUNING_MAX; i++) {
1480aca38eabSMarius Strobl 		memset(tune_req, 0, sizeof(*tune_req));
1481aca38eabSMarius Strobl 		tune_req->cmd = tune_cmd;
1482aca38eabSMarius Strobl 		tune_req->done = sdhci_req_wakeup;
1483aca38eabSMarius Strobl 		tune_req->done_data = slot;
1484aca38eabSMarius Strobl 		slot->req = tune_req;
1485aca38eabSMarius Strobl 		slot->flags = 0;
1486aca38eabSMarius Strobl 		sdhci_start(slot);
1487aca38eabSMarius Strobl 		while (!(tune_req->flags & MMC_REQ_DONE))
1488aca38eabSMarius Strobl 			msleep(tune_req, &slot->mtx, 0, "sdhciet", 0);
1489aca38eabSMarius Strobl 		if (!(tune_req->flags & MMC_TUNE_DONE))
1490aca38eabSMarius Strobl 			break;
1491aca38eabSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1492aca38eabSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING))
1493aca38eabSMarius Strobl 			break;
1494aca38eabSMarius Strobl 		if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK)
1495aca38eabSMarius Strobl 			DELAY(1000);
1496aca38eabSMarius Strobl 	}
1497aca38eabSMarius Strobl 
149878f8baa8SMarius Strobl 	/*
149978f8baa8SMarius Strobl 	 * Restore DMA usage and interrupts.
150078f8baa8SMarius Strobl 	 * Note that the interrupt aggregation code might have cleared
150178f8baa8SMarius Strobl 	 * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask
150278f8baa8SMarius Strobl 	 * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE
150378f8baa8SMarius Strobl 	 * doesn't lose these.
150478f8baa8SMarius Strobl 	 */
1505aca38eabSMarius Strobl 	slot->opt = opt;
1506aca38eabSMarius Strobl 	slot->intmask = intmask;
150778f8baa8SMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END |
150878f8baa8SMarius Strobl 	    SDHCI_INT_RESPONSE);
1509aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, intmask);
1510aca38eabSMarius Strobl 
1511aca38eabSMarius Strobl 	if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING |
1512aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) {
1513aca38eabSMarius Strobl 		slot->retune_req = 0;
1514aca38eabSMarius Strobl 		return (0);
1515aca38eabSMarius Strobl 	}
1516aca38eabSMarius Strobl 
1517aca38eabSMarius Strobl 	slot_printf(slot, "Tuning failed, using fixed sampling clock\n");
1518aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING |
1519aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK));
1520aca38eabSMarius Strobl 	sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1521aca38eabSMarius Strobl 	return (EIO);
1522aca38eabSMarius Strobl }
1523aca38eabSMarius Strobl 
1524aca38eabSMarius Strobl static void
1525aca38eabSMarius Strobl sdhci_retune(void *arg)
1526aca38eabSMarius Strobl {
1527aca38eabSMarius Strobl 	struct sdhci_slot *slot = arg;
1528aca38eabSMarius Strobl 
1529aca38eabSMarius Strobl 	slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
1530aca38eabSMarius Strobl }
1531aca38eabSMarius Strobl 
1532a94a63f0SWarner Losh #ifdef MMCCAM
1533a94a63f0SWarner Losh static void
1534a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot)
1535a94a63f0SWarner Losh {
1536a94a63f0SWarner Losh 	union ccb *ccb;
153715c440e1SWarner Losh 
1538aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
153915c440e1SWarner Losh 		slot_printf(slot, "%s\n", __func__);
1540a94a63f0SWarner Losh 	if (slot->ccb != NULL && slot->curcmd != NULL) {
1541a94a63f0SWarner Losh 		callout_stop(&slot->timeout_callout);
1542a94a63f0SWarner Losh 		ccb = slot->ccb;
1543a94a63f0SWarner Losh 		slot->ccb = NULL;
1544a94a63f0SWarner Losh 		slot->curcmd = NULL;
1545a94a63f0SWarner Losh 
1546a94a63f0SWarner Losh 		/* Tell CAM the request is finished */
1547a94a63f0SWarner Losh 		struct ccb_mmcio *mmcio;
1548a94a63f0SWarner Losh 		mmcio = &ccb->mmcio;
1549a94a63f0SWarner Losh 
1550a94a63f0SWarner Losh 		ccb->ccb_h.status =
1551a94a63f0SWarner Losh 		    (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR);
1552a94a63f0SWarner Losh 		xpt_done(ccb);
1553a94a63f0SWarner Losh 	}
1554a94a63f0SWarner Losh }
1555a94a63f0SWarner Losh #else
1556831f5dcfSAlexander Motin static void
1557e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot)
1558e64f01a9SIan Lepore {
1559e64f01a9SIan Lepore 	struct mmc_request *req;
1560e64f01a9SIan Lepore 
1561e64f01a9SIan Lepore 	if (slot->req != NULL && slot->curcmd != NULL) {
1562e64f01a9SIan Lepore 		callout_stop(&slot->timeout_callout);
1563e64f01a9SIan Lepore 		req = slot->req;
1564e64f01a9SIan Lepore 		slot->req = NULL;
1565e64f01a9SIan Lepore 		slot->curcmd = NULL;
1566e64f01a9SIan Lepore 		req->done(req);
1567e64f01a9SIan Lepore 	}
1568e64f01a9SIan Lepore }
1569a94a63f0SWarner Losh #endif
1570e64f01a9SIan Lepore 
1571e64f01a9SIan Lepore static void
1572aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req)
1573aca38eabSMarius Strobl {
1574aca38eabSMarius Strobl 	struct sdhci_slot *slot;
1575aca38eabSMarius Strobl 
1576aca38eabSMarius Strobl 	slot = req->done_data;
1577aca38eabSMarius Strobl 	req->flags |= MMC_REQ_DONE;
1578aca38eabSMarius Strobl 	wakeup(req);
1579aca38eabSMarius Strobl }
1580aca38eabSMarius Strobl 
1581aca38eabSMarius Strobl static void
1582e64f01a9SIan Lepore sdhci_timeout(void *arg)
1583e64f01a9SIan Lepore {
1584e64f01a9SIan Lepore 	struct sdhci_slot *slot = arg;
1585e64f01a9SIan Lepore 
1586e64f01a9SIan Lepore 	if (slot->curcmd != NULL) {
15877e586643SIan Lepore 		slot_printf(slot, "Controller timeout\n");
15887e586643SIan Lepore 		sdhci_dumpregs(slot);
1589a6873fd1SIan Lepore 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1590e64f01a9SIan Lepore 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1591e64f01a9SIan Lepore 		sdhci_req_done(slot);
15927e586643SIan Lepore 	} else {
15937e586643SIan Lepore 		slot_printf(slot, "Spurious timeout - no active command\n");
1594e64f01a9SIan Lepore 	}
1595e64f01a9SIan Lepore }
1596e64f01a9SIan Lepore 
1597e64f01a9SIan Lepore static void
1598ab00a509SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data)
1599831f5dcfSAlexander Motin {
1600831f5dcfSAlexander Motin 	uint16_t mode;
1601831f5dcfSAlexander Motin 
1602831f5dcfSAlexander Motin 	if (data == NULL)
1603831f5dcfSAlexander Motin 		return;
1604831f5dcfSAlexander Motin 
1605831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
16065d5ae066SIlya Bakulin 	if (data->len > 512 || data->block_count > 1) {
1607831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
16085d5ae066SIlya Bakulin 		if (data->block_count == 0 && __predict_true(
16096dea80e6SMarius Strobl #ifdef MMCCAM
16106dea80e6SMarius Strobl 		    slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION &&
16116dea80e6SMarius Strobl #else
16120519c933SMarius Strobl 		    slot->req->stop != NULL &&
16136dea80e6SMarius Strobl #endif
16146dea80e6SMarius Strobl 		    !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)))
16156dea80e6SMarius Strobl 			mode |= SDHCI_TRNS_ACMD12;
16166dea80e6SMarius Strobl 	}
1617831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
1618831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
1619831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
1620831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
1621831f5dcfSAlexander Motin 
1622831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
1623831f5dcfSAlexander Motin }
1624831f5dcfSAlexander Motin 
1625831f5dcfSAlexander Motin static void
1626831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
1627831f5dcfSAlexander Motin {
1628831f5dcfSAlexander Motin 	int flags, timeout;
162990993663SIan Lepore 	uint32_t mask;
1630831f5dcfSAlexander Motin 
1631831f5dcfSAlexander Motin 	slot->curcmd = cmd;
1632831f5dcfSAlexander Motin 	slot->cmd_done = 0;
1633831f5dcfSAlexander Motin 
1634831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
1635831f5dcfSAlexander Motin 
1636831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
1637831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1638831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
1639831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1640e64f01a9SIan Lepore 		sdhci_req_done(slot);
1641831f5dcfSAlexander Motin 		return;
1642831f5dcfSAlexander Motin 	}
1643831f5dcfSAlexander Motin 
1644b440e965SMarius Strobl 	/*
1645b440e965SMarius Strobl 	 * Do not issue command if there is no card, clock or power.
1646b440e965SMarius Strobl 	 * Controller will not detect timeout without clock active.
1647b440e965SMarius Strobl 	 */
16486e37fb2bSIan Lepore 	if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
1649d8208d9eSAlexander Motin 	    slot->power == 0 ||
1650d8208d9eSAlexander Motin 	    slot->clock == 0) {
1651a94a63f0SWarner Losh 		slot_printf(slot,
1652a94a63f0SWarner Losh 			    "Cannot issue a command (power=%d clock=%d)",
1653a94a63f0SWarner Losh 			    slot->power, slot->clock);
1654831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1655e64f01a9SIan Lepore 		sdhci_req_done(slot);
1656831f5dcfSAlexander Motin 		return;
1657831f5dcfSAlexander Motin 	}
1658831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
1659831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
1660831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
1661a94a63f0SWarner Losh 	if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY))
1662831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
1663aca38eabSMarius Strobl 	/*
1664aca38eabSMarius Strobl 	 * We shouldn't wait for DAT for stop commands or CMD19/CMD21.  Note
1665aca38eabSMarius Strobl 	 * that these latter are also special in that SDHCI_CMD_DATA should
1666aca38eabSMarius Strobl 	 * be set below but no actual data is ever read from the controller.
1667aca38eabSMarius Strobl 	*/
1668a94a63f0SWarner Losh #ifdef MMCCAM
1669aca38eabSMarius Strobl 	if (cmd == &slot->ccb->mmcio.stop ||
1670a94a63f0SWarner Losh #else
1671aca38eabSMarius Strobl 	if (cmd == slot->req->stop ||
1672a94a63f0SWarner Losh #endif
1673aca38eabSMarius Strobl 	    __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1674aca38eabSMarius Strobl 	    cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))
1675aca38eabSMarius Strobl 		mask &= ~SDHCI_DAT_INHIBIT;
16768775ab45SIan Lepore 	/*
16778775ab45SIan Lepore 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
16788775ab45SIan Lepore 	 *  here at all, but when writing a crash dump we may be bypassing the
16798775ab45SIan Lepore 	 *  host platform's interrupt handler, and in some cases that handler
16808775ab45SIan Lepore 	 *  may be working around hardware quirks such as not respecting r1b
16818775ab45SIan Lepore 	 *  busy indications.  In those cases, this wait-loop serves the purpose
16828775ab45SIan Lepore 	 *  of waiting for the prior command and data transfers to be done, and
16838775ab45SIan Lepore 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
16848775ab45SIan Lepore 	 *  (It's usually more like 20-30ms in the real world.)
16858775ab45SIan Lepore 	 */
16868775ab45SIan Lepore 	timeout = 250;
168790993663SIan Lepore 	while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
1688831f5dcfSAlexander Motin 		if (timeout == 0) {
1689831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
1690831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
1691831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
1692831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
1693e64f01a9SIan Lepore 			sdhci_req_done(slot);
1694831f5dcfSAlexander Motin 			return;
1695831f5dcfSAlexander Motin 		}
1696831f5dcfSAlexander Motin 		timeout--;
1697831f5dcfSAlexander Motin 		DELAY(1000);
1698831f5dcfSAlexander Motin 	}
1699831f5dcfSAlexander Motin 
1700831f5dcfSAlexander Motin 	/* Prepare command flags. */
1701831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
1702831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
1703831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
1704831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
1705831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
1706831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
1707831f5dcfSAlexander Motin 	else
1708831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
1709831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
1710831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
1711831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
1712831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
1713a94a63f0SWarner Losh 	if (cmd->data != NULL)
1714831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
1715831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
1716831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
1717831f5dcfSAlexander Motin 	/* Prepare data. */
1718831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
1719831f5dcfSAlexander Motin 	/*
1720831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
1721831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
1722831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
1723831f5dcfSAlexander Motin 	 */
1724831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
1725831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1726831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
1727831f5dcfSAlexander Motin 	}
1728831f5dcfSAlexander Motin 	/* Set command argument. */
1729831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
1730831f5dcfSAlexander Motin 	/* Set data transfer mode. */
1731831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
1732aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
17338adf4202SBjoern A. Zeeb 		slot_printf(slot, "Starting command opcode %#04x flags %#04x\n",
17348adf4202SBjoern A. Zeeb 		    cmd->opcode, flags);
17358adf4202SBjoern A. Zeeb 
1736831f5dcfSAlexander Motin 	/* Start command. */
1737d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
1738a6873fd1SIan Lepore 	/* Start timeout callout. */
1739ba6fc1c7SLuiz Otavio O Souza 	callout_reset(&slot->timeout_callout, slot->timeout * hz,
1740ba6fc1c7SLuiz Otavio O Souza 	    sdhci_timeout, slot);
1741831f5dcfSAlexander Motin }
1742831f5dcfSAlexander Motin 
1743831f5dcfSAlexander Motin static void
1744831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
1745831f5dcfSAlexander Motin {
1746831f5dcfSAlexander Motin 	int i;
17471bacf3beSMarius Strobl 	uint32_t val;
17481bacf3beSMarius Strobl 	uint8_t extra;
1749831f5dcfSAlexander Motin 
1750aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
17518adf4202SBjoern A. Zeeb 		slot_printf(slot, "%s: called, err %d flags %#04x\n",
1752a94a63f0SWarner Losh 		    __func__, slot->curcmd->error, slot->curcmd->flags);
1753831f5dcfSAlexander Motin 	slot->cmd_done = 1;
175472dec079SMarius Strobl 	/*
175572dec079SMarius Strobl 	 * Interrupt aggregation: Restore command interrupt.
1756831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
175772dec079SMarius Strobl 	 * happened first.
175872dec079SMarius Strobl 	 */
1759aca38eabSMarius Strobl 	if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK &&
1760aca38eabSMarius Strobl 	    slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200))
1761aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |=
1762aca38eabSMarius Strobl 		    SDHCI_INT_RESPONSE);
1763831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
1764831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1765aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
1766aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1767831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1768831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1769831f5dcfSAlexander Motin 		sdhci_start(slot);
1770831f5dcfSAlexander Motin 		return;
1771831f5dcfSAlexander Motin 	}
1772831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
1773831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1774831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
1775831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
17761bacf3beSMarius Strobl 			extra = 0;
1777831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
17781bacf3beSMarius Strobl 				val = RD4(slot, SDHCI_RESPONSE + i * 4);
17791bacf3beSMarius Strobl 				if (slot->quirks &
17801bacf3beSMarius Strobl 				    SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1781677ee494SIan Lepore 					slot->curcmd->resp[3 - i] = val;
1782677ee494SIan Lepore 				else {
1783677ee494SIan Lepore 					slot->curcmd->resp[3 - i] =
1784677ee494SIan Lepore 					    (val << 8) | extra;
1785831f5dcfSAlexander Motin 					extra = val >> 24;
1786831f5dcfSAlexander Motin 				}
1787677ee494SIan Lepore 			}
1788831f5dcfSAlexander Motin 		} else
1789831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1790831f5dcfSAlexander Motin 	}
1791aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
17928adf4202SBjoern A. Zeeb 		slot_printf(slot, "Resp: %#04x %#04x %#04x %#04x\n",
1793a94a63f0SWarner Losh 		    slot->curcmd->resp[0], slot->curcmd->resp[1],
1794a94a63f0SWarner Losh 		    slot->curcmd->resp[2], slot->curcmd->resp[3]);
1795a94a63f0SWarner Losh 
1796831f5dcfSAlexander Motin 	/* If data ready - finish. */
1797831f5dcfSAlexander Motin 	if (slot->data_done)
1798831f5dcfSAlexander Motin 		sdhci_start(slot);
1799831f5dcfSAlexander Motin }
1800831f5dcfSAlexander Motin 
1801831f5dcfSAlexander Motin static void
1802ab00a509SMarius Strobl sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data)
1803831f5dcfSAlexander Motin {
1804ab00a509SMarius Strobl 	uint32_t blkcnt, blksz, current_timeout, sdma_bbufsz, target_timeout;
1805831f5dcfSAlexander Motin 	uint8_t div;
1806831f5dcfSAlexander Motin 
1807831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1808831f5dcfSAlexander Motin 		slot->data_done = 1;
1809831f5dcfSAlexander Motin 		return;
1810831f5dcfSAlexander Motin 	}
1811831f5dcfSAlexander Motin 
1812831f5dcfSAlexander Motin 	slot->data_done = 0;
1813831f5dcfSAlexander Motin 
1814831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
1815831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
1816ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1817ceb9e9f7SIan Lepore 		div = 0xE;
1818ceb9e9f7SIan Lepore 	} else {
1819831f5dcfSAlexander Motin 		target_timeout = 1000000;
1820831f5dcfSAlexander Motin 		div = 0;
1821831f5dcfSAlexander Motin 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1822ceb9e9f7SIan Lepore 		while (current_timeout < target_timeout && div < 0xE) {
1823ceb9e9f7SIan Lepore 			++div;
1824831f5dcfSAlexander Motin 			current_timeout <<= 1;
1825831f5dcfSAlexander Motin 		}
1826831f5dcfSAlexander Motin 		/* Compensate for an off-by-one error in the CaFe chip.*/
1827ceb9e9f7SIan Lepore 		if (div < 0xE &&
1828ceb9e9f7SIan Lepore 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1829ceb9e9f7SIan Lepore 			++div;
1830831f5dcfSAlexander Motin 		}
1831ceb9e9f7SIan Lepore 	}
1832831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1833831f5dcfSAlexander Motin 
1834831f5dcfSAlexander Motin 	if (data == NULL)
1835831f5dcfSAlexander Motin 		return;
1836831f5dcfSAlexander Motin 
1837831f5dcfSAlexander Motin 	/* Use DMA if possible. */
1838831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
1839831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
1840ab00a509SMarius Strobl 	/* If data is small, broken DMA may return zeroes instead of data. */
1841d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1842831f5dcfSAlexander Motin 	    (data->len <= 512))
1843831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1844831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
1845d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1846831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
1847831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1848831f5dcfSAlexander Motin 	/* Load DMA buffer. */
1849831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
1850ab00a509SMarius Strobl 		sdma_bbufsz = slot->sdma_bbufsz;
1851831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
1852ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1853ecc2d997SRui Paulo 			    BUS_DMASYNC_PREREAD);
1854831f5dcfSAlexander Motin 		else {
1855ab00a509SMarius Strobl 			memcpy(slot->dmamem, data->data, ulmin(data->len,
1856ab00a509SMarius Strobl 			    sdma_bbufsz));
1857ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1858ecc2d997SRui Paulo 			    BUS_DMASYNC_PREWRITE);
1859831f5dcfSAlexander Motin 		}
1860831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1861ab00a509SMarius Strobl 		/*
1862ab00a509SMarius Strobl 		 * Interrupt aggregation: Mask border interrupt for the last
1863ab00a509SMarius Strobl 		 * bounce buffer and unmask otherwise.
1864ab00a509SMarius Strobl 		 */
1865ab00a509SMarius Strobl 		if (data->len == sdma_bbufsz)
1866831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1867831f5dcfSAlexander Motin 		else
1868831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
1869831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1870831f5dcfSAlexander Motin 	}
1871831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
1872831f5dcfSAlexander Motin 	slot->offset = 0;
18735d5ae066SIlya Bakulin #ifdef MMCCAM
18745d5ae066SIlya Bakulin 	if (data->flags & MMC_DATA_BLOCK_SIZE) {
18755d5ae066SIlya Bakulin 		/* Set block size and request border interrupts on the SDMA boundary. */
18765d5ae066SIlya Bakulin 		blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, data->block_size);
18775d5ae066SIlya Bakulin 		blkcnt = data->block_count;
18785d5ae066SIlya Bakulin 		if (__predict_false(sdhci_debug > 0))
18795d5ae066SIlya Bakulin 			slot_printf(slot, "SDIO Custom block params: blksz: "
18805d5ae066SIlya Bakulin 			    "%#10x, blk cnt: %#10x\n", blksz, blkcnt);
18815d5ae066SIlya Bakulin 	} else
18825d5ae066SIlya Bakulin #endif
18835d5ae066SIlya Bakulin 	{
1884ab00a509SMarius Strobl 		/* Set block size and request border interrupts on the SDMA boundary. */
1885ab00a509SMarius Strobl 		blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, ulmin(data->len, 512));
1886ab00a509SMarius Strobl 		blkcnt = howmany(data->len, 512);
18875d5ae066SIlya Bakulin 	}
18885d5ae066SIlya Bakulin 
18895d5ae066SIlya Bakulin 	WR2(slot, SDHCI_BLOCK_SIZE, blksz);
1890ab00a509SMarius Strobl 	WR2(slot, SDHCI_BLOCK_COUNT, blkcnt);
1891aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1892ab00a509SMarius Strobl 		slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
1893ab00a509SMarius Strobl 		    blksz, blkcnt);
1894831f5dcfSAlexander Motin }
1895831f5dcfSAlexander Motin 
1896c3a0f75aSOleksandr Tymoshenko void
1897831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
1898831f5dcfSAlexander Motin {
1899831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
19007e6ccea3SMarius Strobl 	size_t left;
1901831f5dcfSAlexander Motin 
1902831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
1903ecc2d997SRui Paulo 	 * Auxiliary restore point for the case when data interrupt
1904831f5dcfSAlexander Motin 	 * happened first. */
1905831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
1906831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1907831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
1908831f5dcfSAlexander Motin 	}
1909831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
1910915780d7SLuiz Otavio O Souza 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) &&
1911915780d7SLuiz Otavio O Souza 	    slot->curcmd->data != NULL) {
1912831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
19137e6ccea3SMarius Strobl 			left = data->len - slot->offset;
1914ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1915ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTREAD);
1916831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1917ab00a509SMarius Strobl 			    ulmin(left, slot->sdma_bbufsz));
1918831f5dcfSAlexander Motin 		} else
1919ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1920ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTWRITE);
1921831f5dcfSAlexander Motin 	}
1922a98788edSIan Lepore 	slot->data_done = 1;
1923831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
1924831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1925aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
1926aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1927831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1928831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1929831f5dcfSAlexander Motin 		sdhci_start(slot);
1930831f5dcfSAlexander Motin 		return;
1931831f5dcfSAlexander Motin 	}
1932831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
1933831f5dcfSAlexander Motin 	if (slot->cmd_done)
1934831f5dcfSAlexander Motin 		sdhci_start(slot);
1935831f5dcfSAlexander Motin }
1936831f5dcfSAlexander Motin 
1937a94a63f0SWarner Losh #ifdef MMCCAM
1938a94a63f0SWarner Losh static void
1939a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot)
1940a94a63f0SWarner Losh {
1941a94a63f0SWarner Losh 	union ccb *ccb;
1942ab00a509SMarius Strobl 	struct ccb_mmcio *mmcio;
1943a94a63f0SWarner Losh 
1944a94a63f0SWarner Losh 	ccb = slot->ccb;
1945a94a63f0SWarner Losh 	if (ccb == NULL)
1946a94a63f0SWarner Losh 		return;
1947a94a63f0SWarner Losh 
1948a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
1949a94a63f0SWarner Losh 	if (!(slot->flags & CMD_STARTED)) {
1950a94a63f0SWarner Losh 		slot->flags |= CMD_STARTED;
1951a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->cmd);
1952a94a63f0SWarner Losh 		return;
1953a94a63f0SWarner Losh 	}
1954a94a63f0SWarner Losh 
1955a94a63f0SWarner Losh 	/*
1956a94a63f0SWarner Losh 	 * Old stack doesn't use this!
1957a94a63f0SWarner Losh 	 * Enabling this code causes significant performance degradation
1958a94a63f0SWarner Losh 	 * and IRQ storms on BBB, Wandboard behaves fine.
1959a94a63f0SWarner Losh 	 * Not using this code does no harm...
1960a94a63f0SWarner Losh 	if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) {
1961a94a63f0SWarner Losh 		slot->flags |= STOP_STARTED;
1962a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->stop);
1963a94a63f0SWarner Losh 		return;
1964a94a63f0SWarner Losh 	}
1965a94a63f0SWarner Losh 	*/
1966aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
1967a94a63f0SWarner Losh 		slot_printf(slot, "result: %d\n", mmcio->cmd.error);
1968a94a63f0SWarner Losh 	if (mmcio->cmd.error == 0 &&
1969a94a63f0SWarner Losh 	    (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
1970a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD);
1971a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_DATA);
1972a94a63f0SWarner Losh 	}
1973a94a63f0SWarner Losh 
1974a94a63f0SWarner Losh 	sdhci_req_done(slot);
1975a94a63f0SWarner Losh }
1976a94a63f0SWarner Losh #else
1977831f5dcfSAlexander Motin static void
1978831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
1979831f5dcfSAlexander Motin {
1980ab00a509SMarius Strobl 	const struct mmc_request *req;
1981831f5dcfSAlexander Motin 
1982831f5dcfSAlexander Motin 	req = slot->req;
1983831f5dcfSAlexander Motin 	if (req == NULL)
1984831f5dcfSAlexander Motin 		return;
1985831f5dcfSAlexander Motin 
1986831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
1987831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
1988831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
1989831f5dcfSAlexander Motin 		return;
1990831f5dcfSAlexander Motin 	}
1991915780d7SLuiz Otavio O Souza 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) &&
1992915780d7SLuiz Otavio O Souza 	    !(slot->flags & STOP_STARTED) && req->stop) {
1993831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
1994831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
1995831f5dcfSAlexander Motin 		return;
1996831f5dcfSAlexander Motin 	}
1997aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
19985b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
19995b69a497SAlexander Motin 	if (!req->cmd->error &&
2000915780d7SLuiz Otavio O Souza 	    ((slot->curcmd == req->stop &&
2001915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) ||
2002915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2003831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
2004831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
2005831f5dcfSAlexander Motin 	}
2006831f5dcfSAlexander Motin 
2007e64f01a9SIan Lepore 	sdhci_req_done(slot);
2008831f5dcfSAlexander Motin }
2009a94a63f0SWarner Losh #endif
2010831f5dcfSAlexander Motin 
2011d6b3aaf8SOleksandr Tymoshenko int
2012b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev,
2013b440e965SMarius Strobl     struct mmc_request *req)
2014831f5dcfSAlexander Motin {
2015831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2016831f5dcfSAlexander Motin 
2017831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2018831f5dcfSAlexander Motin 	if (slot->req != NULL) {
2019831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
2020831f5dcfSAlexander Motin 		return (EBUSY);
2021831f5dcfSAlexander Motin 	}
2022aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
20231bacf3beSMarius Strobl 		slot_printf(slot,
20241bacf3beSMarius Strobl 		    "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
2025831f5dcfSAlexander Motin 		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
20265b69a497SAlexander Motin 		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
20275b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
20285b69a497SAlexander Motin 	}
2029831f5dcfSAlexander Motin 	slot->req = req;
2030831f5dcfSAlexander Motin 	slot->flags = 0;
2031831f5dcfSAlexander Motin 	sdhci_start(slot);
2032831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2033bea2dca2SAlexander Motin 	if (dumping) {
2034bea2dca2SAlexander Motin 		while (slot->req != NULL) {
2035d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
2036bea2dca2SAlexander Motin 			DELAY(10);
2037bea2dca2SAlexander Motin 		}
2038bea2dca2SAlexander Motin 	}
2039831f5dcfSAlexander Motin 	return (0);
2040831f5dcfSAlexander Motin }
2041831f5dcfSAlexander Motin 
2042d6b3aaf8SOleksandr Tymoshenko int
2043b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev)
2044831f5dcfSAlexander Motin {
2045831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2046831f5dcfSAlexander Motin 	uint32_t val;
2047831f5dcfSAlexander Motin 
2048831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2049831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
2050831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2051831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
2052831f5dcfSAlexander Motin }
2053831f5dcfSAlexander Motin 
2054d6b3aaf8SOleksandr Tymoshenko int
2055b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev)
2056831f5dcfSAlexander Motin {
2057831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2058831f5dcfSAlexander Motin 	int err = 0;
2059831f5dcfSAlexander Motin 
2060831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2061831f5dcfSAlexander Motin 	while (slot->bus_busy)
2062d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
2063831f5dcfSAlexander Motin 	slot->bus_busy++;
2064831f5dcfSAlexander Motin 	/* Activate led. */
2065831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
2066831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2067831f5dcfSAlexander Motin 	return (err);
2068831f5dcfSAlexander Motin }
2069831f5dcfSAlexander Motin 
2070d6b3aaf8SOleksandr Tymoshenko int
2071b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev)
2072831f5dcfSAlexander Motin {
2073831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2074831f5dcfSAlexander Motin 
2075831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2076831f5dcfSAlexander Motin 	/* Deactivate led. */
2077831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
2078831f5dcfSAlexander Motin 	slot->bus_busy--;
2079831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2080d493985aSAlexander Motin 	wakeup(slot);
2081831f5dcfSAlexander Motin 	return (0);
2082831f5dcfSAlexander Motin }
2083831f5dcfSAlexander Motin 
2084831f5dcfSAlexander Motin static void
2085831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
2086831f5dcfSAlexander Motin {
2087831f5dcfSAlexander Motin 
2088831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2089831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
2090831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2091831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2092831f5dcfSAlexander Motin 		return;
2093831f5dcfSAlexander Motin 	}
2094831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
2095831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2096831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
2097831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2098831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
2099831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
2100831f5dcfSAlexander Motin 
2101831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
2102831f5dcfSAlexander Motin }
2103831f5dcfSAlexander Motin 
2104831f5dcfSAlexander Motin static void
2105831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
2106831f5dcfSAlexander Motin {
21071bacf3beSMarius Strobl 	struct mmc_data *data;
210815c440e1SWarner Losh 	size_t left;
2109ab00a509SMarius Strobl 	uint32_t sdma_bbufsz;
2110831f5dcfSAlexander Motin 
2111831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2112831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2113831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2114831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2115831f5dcfSAlexander Motin 		return;
2116831f5dcfSAlexander Motin 	}
2117831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2118831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
2119831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2120831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
2121831f5dcfSAlexander Motin 		    intmask);
2122831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2123831f5dcfSAlexander Motin 		return;
2124831f5dcfSAlexander Motin 	}
2125831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
2126831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2127acbaa69fSOleksandr Tymoshenko 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
2128831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2129831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2130831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
2131831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
2132831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2133831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
2134831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2135831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
2136831f5dcfSAlexander Motin 	}
2137831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
2138831f5dcfSAlexander Motin 		/* No need to continue after any error. */
2139a98788edSIan Lepore 		goto done;
2140831f5dcfSAlexander Motin 	}
2141831f5dcfSAlexander Motin 
2142aca38eabSMarius Strobl 	/* Handle tuning completion interrupt. */
2143aca38eabSMarius Strobl 	if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) &&
2144aca38eabSMarius Strobl 	    (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK ||
2145aca38eabSMarius Strobl 	    slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) {
2146aca38eabSMarius Strobl 		slot->req->flags |= MMC_TUNE_DONE;
2147aca38eabSMarius Strobl 		sdhci_finish_command(slot);
2148aca38eabSMarius Strobl 		sdhci_finish_data(slot);
2149aca38eabSMarius Strobl 		return;
2150aca38eabSMarius Strobl 	}
2151831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
2152c3a0f75aSOleksandr Tymoshenko 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
2153c3a0f75aSOleksandr Tymoshenko 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
2154c3a0f75aSOleksandr Tymoshenko 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
21551bacf3beSMarius Strobl 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot,
21561bacf3beSMarius Strobl 			    &intmask);
2157c3a0f75aSOleksandr Tymoshenko 			slot->flags |= PLATFORM_DATA_STARTED;
2158c3a0f75aSOleksandr Tymoshenko 		} else
2159831f5dcfSAlexander Motin 			sdhci_transfer_pio(slot);
2160c3a0f75aSOleksandr Tymoshenko 	}
2161831f5dcfSAlexander Motin 	/* Handle DMA border. */
2162831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
21631bacf3beSMarius Strobl 		data = slot->curcmd->data;
2164ab00a509SMarius Strobl 		sdma_bbufsz = slot->sdma_bbufsz;
2165831f5dcfSAlexander Motin 
2166831f5dcfSAlexander Motin 		/* Unload DMA buffer ... */
2167831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2168831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2169831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2170831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
2171831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
2172ab00a509SMarius Strobl 			    ulmin(left, sdma_bbufsz));
2173831f5dcfSAlexander Motin 		} else {
2174831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2175831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
2176831f5dcfSAlexander Motin 		}
2177831f5dcfSAlexander Motin 		/* ... and reload it again. */
2178ab00a509SMarius Strobl 		slot->offset += sdma_bbufsz;
2179831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2180831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2181831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2182831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREREAD);
2183831f5dcfSAlexander Motin 		} else {
2184831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
2185ab00a509SMarius Strobl 			    ulmin(left, sdma_bbufsz));
2186831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2187831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
2188831f5dcfSAlexander Motin 		}
2189ab00a509SMarius Strobl 		/*
2190ab00a509SMarius Strobl 		 * Interrupt aggregation: Mask border interrupt for the last
2191ab00a509SMarius Strobl 		 * bounce buffer.
2192ab00a509SMarius Strobl 		 */
2193ab00a509SMarius Strobl 		if (left == sdma_bbufsz) {
2194831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
2195831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2196831f5dcfSAlexander Motin 		}
2197831f5dcfSAlexander Motin 		/* Restart DMA. */
2198831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
2199831f5dcfSAlexander Motin 	}
2200831f5dcfSAlexander Motin 	/* We have got all data. */
2201c3a0f75aSOleksandr Tymoshenko 	if (intmask & SDHCI_INT_DATA_END) {
2202c3a0f75aSOleksandr Tymoshenko 		if (slot->flags & PLATFORM_DATA_STARTED) {
2203c3a0f75aSOleksandr Tymoshenko 			slot->flags &= ~PLATFORM_DATA_STARTED;
2204c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2205c3a0f75aSOleksandr Tymoshenko 		} else
2206831f5dcfSAlexander Motin 			sdhci_finish_data(slot);
2207831f5dcfSAlexander Motin 	}
2208a98788edSIan Lepore done:
2209a98788edSIan Lepore 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
2210a98788edSIan Lepore 		if (slot->flags & PLATFORM_DATA_STARTED) {
2211a98788edSIan Lepore 			slot->flags &= ~PLATFORM_DATA_STARTED;
2212a98788edSIan Lepore 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2213a98788edSIan Lepore 		} else
2214a98788edSIan Lepore 			sdhci_finish_data(slot);
2215a98788edSIan Lepore 	}
2216c3a0f75aSOleksandr Tymoshenko }
2217831f5dcfSAlexander Motin 
2218831f5dcfSAlexander Motin static void
22196dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err)
2220831f5dcfSAlexander Motin {
2221831f5dcfSAlexander Motin 
2222831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2223831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
22246dea80e6SMarius Strobl 		    "there is no active command.\n", acmd_err);
2225831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2226831f5dcfSAlexander Motin 		return;
2227831f5dcfSAlexander Motin 	}
22286dea80e6SMarius Strobl 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err);
2229831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_CMD);
2230831f5dcfSAlexander Motin }
2231831f5dcfSAlexander Motin 
2232d6b3aaf8SOleksandr Tymoshenko void
2233d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
2234831f5dcfSAlexander Motin {
22352b96b955SJustin Hibbits 	uint32_t intmask, present;
22366dea80e6SMarius Strobl 	uint16_t val16;
2237831f5dcfSAlexander Motin 
2238831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2239831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
2240831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
2241831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
2242831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
2243d6b3aaf8SOleksandr Tymoshenko 		return;
2244831f5dcfSAlexander Motin 	}
2245aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 2))
22465b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
22475b69a497SAlexander Motin 
2248aca38eabSMarius Strobl 	/* Handle tuning error interrupt. */
2249aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_TUNEERR)) {
22506dea80e6SMarius Strobl 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR);
2251aca38eabSMarius Strobl 		slot_printf(slot, "Tuning error indicated\n");
2252aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
2253aca38eabSMarius Strobl 		if (slot->curcmd) {
2254aca38eabSMarius Strobl 			slot->curcmd->error = MMC_ERR_BADCRC;
2255aca38eabSMarius Strobl 			sdhci_finish_command(slot);
2256aca38eabSMarius Strobl 		}
2257aca38eabSMarius Strobl 	}
2258aca38eabSMarius Strobl 	/* Handle re-tuning interrupt. */
2259aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_RETUNE))
2260aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
2261831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
2262831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2263639f59f0SIan Lepore 		present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
22642b96b955SJustin Hibbits 		slot->intmask &=
22652b96b955SJustin Hibbits 		    ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
22662b96b955SJustin Hibbits 		slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
22672b96b955SJustin Hibbits 		    SDHCI_INT_CARD_INSERT;
22682b96b955SJustin Hibbits 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
22692b96b955SJustin Hibbits 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2270831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
2271831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
2272b8bf08b1SIan Lepore 		sdhci_handle_card_present_locked(slot, present);
2273831f5dcfSAlexander Motin 	}
2274831f5dcfSAlexander Motin 	/* Handle command interrupts. */
2275831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
2276831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
2277831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
2278831f5dcfSAlexander Motin 	}
2279831f5dcfSAlexander Motin 	/* Handle data interrupts. */
2280831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
2281831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
22827e6ccea3SMarius Strobl 		/* Don't call data_irq in case of errored command. */
22837e586643SIan Lepore 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
2284831f5dcfSAlexander Motin 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
2285831f5dcfSAlexander Motin 	}
2286831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
2287831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
22886dea80e6SMarius Strobl 		/* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */
22896dea80e6SMarius Strobl 		val16 = RD2(slot, SDHCI_ACMD12_ERR);
2290831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
22916dea80e6SMarius Strobl 		sdhci_acmd_irq(slot, val16);
2292831f5dcfSAlexander Motin 	}
2293831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
2294831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
2295831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
2296aca38eabSMarius Strobl 		slot_printf(slot, "Card is consuming too much power!\n");
2297831f5dcfSAlexander Motin 	}
2298aca38eabSMarius Strobl 	intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE |
2299aca38eabSMarius Strobl 	    SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK |
2300aca38eabSMarius Strobl 	    SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER);
2301831f5dcfSAlexander Motin 	/* The rest is unknown. */
2302831f5dcfSAlexander Motin 	if (intmask) {
2303831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
2304831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
2305831f5dcfSAlexander Motin 		    intmask);
2306831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2307831f5dcfSAlexander Motin 	}
2308831f5dcfSAlexander Motin 
2309831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2310831f5dcfSAlexander Motin }
2311831f5dcfSAlexander Motin 
2312d6b3aaf8SOleksandr Tymoshenko int
23131bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which,
23141bacf3beSMarius Strobl     uintptr_t *result)
2315831f5dcfSAlexander Motin {
2316ab00a509SMarius Strobl 	const struct sdhci_slot *slot = device_get_ivars(child);
2317831f5dcfSAlexander Motin 
2318831f5dcfSAlexander Motin 	switch (which) {
2319831f5dcfSAlexander Motin 	default:
2320831f5dcfSAlexander Motin 		return (EINVAL);
2321831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2322bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
2323831f5dcfSAlexander Motin 		break;
2324831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2325bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
2326831f5dcfSAlexander Motin 		break;
2327831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2328bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
2329831f5dcfSAlexander Motin 		break;
2330831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2331bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
2332831f5dcfSAlexander Motin 		break;
2333831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2334bcd91d25SJayachandran C. 		*result = slot->host.f_min;
2335831f5dcfSAlexander Motin 		break;
2336831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
2337bcd91d25SJayachandran C. 		*result = slot->host.f_max;
2338831f5dcfSAlexander Motin 		break;
2339831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2340bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
2341831f5dcfSAlexander Motin 		break;
2342831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2343bcd91d25SJayachandran C. 		*result = slot->host.mode;
2344831f5dcfSAlexander Motin 		break;
2345831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2346bcd91d25SJayachandran C. 		*result = slot->host.ocr;
2347831f5dcfSAlexander Motin 		break;
2348831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2349bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
2350831f5dcfSAlexander Motin 		break;
2351831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2352bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
2353831f5dcfSAlexander Motin 		break;
2354aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2355aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED) {
2356aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) {
2357aca38eabSMarius Strobl 				*result = retune_req_reset;
2358aca38eabSMarius Strobl 				break;
2359aca38eabSMarius Strobl 			}
2360aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) {
2361aca38eabSMarius Strobl 				*result = retune_req_normal;
2362aca38eabSMarius Strobl 				break;
2363aca38eabSMarius Strobl 			}
2364aca38eabSMarius Strobl 		}
2365aca38eabSMarius Strobl 		*result = retune_req_none;
2366aca38eabSMarius Strobl 		break;
23670f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
23680f34084fSMarius Strobl 		*result = slot->host.ios.vccq;
23690f34084fSMarius Strobl 		break;
2370831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2371bcd91d25SJayachandran C. 		*result = slot->host.caps;
2372831f5dcfSAlexander Motin 		break;
2373831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2374bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
2375831f5dcfSAlexander Motin 		break;
23763a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2377aca38eabSMarius Strobl 		/*
2378aca38eabSMarius Strobl 		 * Re-tuning modes 1 and 2 restrict the maximum data length
2379aca38eabSMarius Strobl 		 * per read/write command to 4 MiB.
2380aca38eabSMarius Strobl 		 */
2381aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED &&
2382aca38eabSMarius Strobl 		    (slot->retune_mode == SDHCI_RETUNE_MODE_1 ||
2383aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_2)) {
2384aca38eabSMarius Strobl 			*result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE;
2385aca38eabSMarius Strobl 			break;
2386aca38eabSMarius Strobl 		}
2387bcd91d25SJayachandran C. 		*result = 65535;
23883a4a2557SAlexander Motin 		break;
238972dec079SMarius Strobl 	case MMCBR_IVAR_MAX_BUSY_TIMEOUT:
239072dec079SMarius Strobl 		/*
239172dec079SMarius Strobl 		 * Currently, sdhci_start_data() hardcodes 1 s for all CMDs.
239272dec079SMarius Strobl 		 */
239372dec079SMarius Strobl 		*result = 1000000;
239472dec079SMarius Strobl 		break;
2395831f5dcfSAlexander Motin 	}
2396831f5dcfSAlexander Motin 	return (0);
2397831f5dcfSAlexander Motin }
2398831f5dcfSAlexander Motin 
2399d6b3aaf8SOleksandr Tymoshenko int
24001bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which,
24011bacf3beSMarius Strobl     uintptr_t value)
2402831f5dcfSAlexander Motin {
2403831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
2404b440e965SMarius Strobl 	uint32_t clock, max_clock;
2405b440e965SMarius Strobl 	int i;
2406831f5dcfSAlexander Motin 
240715c440e1SWarner Losh 	if (sdhci_debug > 1)
240815c440e1SWarner Losh 		slot_printf(slot, "%s: var=%d\n", __func__, which);
2409831f5dcfSAlexander Motin 	switch (which) {
2410831f5dcfSAlexander Motin 	default:
2411831f5dcfSAlexander Motin 		return (EINVAL);
2412831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2413831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
2414831f5dcfSAlexander Motin 		break;
2415831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2416831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
2417831f5dcfSAlexander Motin 		break;
2418831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2419831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
2420831f5dcfSAlexander Motin 		break;
2421831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2422831f5dcfSAlexander Motin 		if (value > 0) {
242357677a3aSOleksandr Tymoshenko 			max_clock = slot->max_clk;
242457677a3aSOleksandr Tymoshenko 			clock = max_clock;
242557677a3aSOleksandr Tymoshenko 
242657677a3aSOleksandr Tymoshenko 			if (slot->version < SDHCI_SPEC_300) {
242757677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
242857677a3aSOleksandr Tymoshenko 				    i <<= 1) {
2429831f5dcfSAlexander Motin 					if (clock <= value)
2430831f5dcfSAlexander Motin 						break;
2431831f5dcfSAlexander Motin 					clock >>= 1;
2432831f5dcfSAlexander Motin 				}
2433b440e965SMarius Strobl 			} else {
243457677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
243557677a3aSOleksandr Tymoshenko 				    i += 2) {
243657677a3aSOleksandr Tymoshenko 					if (clock <= value)
243757677a3aSOleksandr Tymoshenko 						break;
243857677a3aSOleksandr Tymoshenko 					clock = max_clock / (i + 2);
243957677a3aSOleksandr Tymoshenko 				}
244057677a3aSOleksandr Tymoshenko 			}
244157677a3aSOleksandr Tymoshenko 
2442831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
2443831f5dcfSAlexander Motin 		} else
2444831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
2445831f5dcfSAlexander Motin 		break;
2446831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2447831f5dcfSAlexander Motin 		slot->host.mode = value;
2448831f5dcfSAlexander Motin 		break;
2449831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2450831f5dcfSAlexander Motin 		slot->host.ocr = value;
2451831f5dcfSAlexander Motin 		break;
2452831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2453831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
2454831f5dcfSAlexander Motin 		break;
2455831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2456831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
2457831f5dcfSAlexander Motin 		break;
24580f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
24590f34084fSMarius Strobl 		slot->host.ios.vccq = value;
24600f34084fSMarius Strobl 		break;
2461831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2462831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
2463831f5dcfSAlexander Motin 		break;
2464831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2465831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2466831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2467831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
24683a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2469aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2470831f5dcfSAlexander Motin 		return (EINVAL);
2471831f5dcfSAlexander Motin 	}
2472831f5dcfSAlexander Motin 	return (0);
2473831f5dcfSAlexander Motin }
2474831f5dcfSAlexander Motin 
247515c440e1SWarner Losh #ifdef MMCCAM
2476a94a63f0SWarner Losh void
2477d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot)
2478a94a63f0SWarner Losh {
2479ab00a509SMarius Strobl 
2480505f6a0cSBjoern A. Zeeb 	if ((slot->devq = cam_simq_alloc(1)) == NULL)
2481a94a63f0SWarner Losh 		goto fail;
2482a94a63f0SWarner Losh 
2483a94a63f0SWarner Losh 	mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF);
24846e40542aSBjoern A. Zeeb 	slot->sim = cam_sim_alloc_dev(sdhci_cam_action, sdhci_cam_poll,
24856e40542aSBjoern A. Zeeb 	    "sdhci_slot", slot, slot->bus,
2486a94a63f0SWarner Losh 	    &slot->sim_mtx, 1, 1, slot->devq);
2487a94a63f0SWarner Losh 
2488a94a63f0SWarner Losh 	if (slot->sim == NULL) {
2489a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2490a94a63f0SWarner Losh 		slot_printf(slot, "cannot allocate CAM SIM\n");
2491a94a63f0SWarner Losh 		goto fail;
2492a94a63f0SWarner Losh 	}
2493a94a63f0SWarner Losh 
2494a94a63f0SWarner Losh 	mtx_lock(&slot->sim_mtx);
2495a94a63f0SWarner Losh 	if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) {
2496505f6a0cSBjoern A. Zeeb 		slot_printf(slot, "cannot register SCSI pass-through bus\n");
2497a94a63f0SWarner Losh 		cam_sim_free(slot->sim, FALSE);
2498a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2499a94a63f0SWarner Losh 		mtx_unlock(&slot->sim_mtx);
2500a94a63f0SWarner Losh 		goto fail;
2501a94a63f0SWarner Losh 	}
2502a94a63f0SWarner Losh 	mtx_unlock(&slot->sim_mtx);
2503505f6a0cSBjoern A. Zeeb 
2504a94a63f0SWarner Losh 	/* End CAM-specific init */
2505a94a63f0SWarner Losh 	slot->card_present = 0;
2506a94a63f0SWarner Losh 	sdhci_card_task(slot, 0);
2507a94a63f0SWarner Losh 	return;
2508a94a63f0SWarner Losh 
2509a94a63f0SWarner Losh fail:
2510a94a63f0SWarner Losh 	if (slot->sim != NULL) {
2511a94a63f0SWarner Losh 		mtx_lock(&slot->sim_mtx);
2512a94a63f0SWarner Losh 		xpt_bus_deregister(cam_sim_path(slot->sim));
2513a94a63f0SWarner Losh 		cam_sim_free(slot->sim, FALSE);
2514a94a63f0SWarner Losh 		mtx_unlock(&slot->sim_mtx);
2515a94a63f0SWarner Losh 	}
2516a94a63f0SWarner Losh 
2517a94a63f0SWarner Losh 	if (slot->devq != NULL)
2518a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2519a94a63f0SWarner Losh }
2520a94a63f0SWarner Losh 
2521a94a63f0SWarner Losh void
2522a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb)
2523a94a63f0SWarner Losh {
2524a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2525a94a63f0SWarner Losh 
2526a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2527a94a63f0SWarner Losh 	if (slot == NULL) {
2528a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2529a94a63f0SWarner Losh 		xpt_done(ccb);
2530a94a63f0SWarner Losh 		return;
2531a94a63f0SWarner Losh 	}
2532a94a63f0SWarner Losh 
2533a94a63f0SWarner Losh 	mtx_assert(&slot->sim_mtx, MA_OWNED);
2534a94a63f0SWarner Losh 
2535a94a63f0SWarner Losh 	switch (ccb->ccb_h.func_code) {
2536a94a63f0SWarner Losh 	case XPT_PATH_INQ:
25378c7cd14aSWarner Losh 		mmc_path_inq(&ccb->cpi, "Deglitch Networks", sim, MAXPHYS);
2538a94a63f0SWarner Losh 		break;
25398c7cd14aSWarner Losh 
2540a94a63f0SWarner Losh 	case XPT_GET_TRAN_SETTINGS:
2541a94a63f0SWarner Losh 	{
2542a94a63f0SWarner Losh 		struct ccb_trans_settings *cts = &ccb->cts;
25435d20e651SIlya Bakulin 		uint32_t max_data;
2544a94a63f0SWarner Losh 
2545a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2546a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n");
2547a94a63f0SWarner Losh 
2548a94a63f0SWarner Losh 		cts->protocol = PROTO_MMCSD;
2549a94a63f0SWarner Losh 		cts->protocol_version = 1;
2550a94a63f0SWarner Losh 		cts->transport = XPORT_MMCSD;
2551a94a63f0SWarner Losh 		cts->transport_version = 1;
2552a94a63f0SWarner Losh 		cts->xport_specific.valid = 0;
2553a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_ocr = slot->host.host_ocr;
2554a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_min = slot->host.f_min;
2555a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_max = slot->host.f_max;
2556a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_caps = slot->host.caps;
25575d20e651SIlya Bakulin 		/*
25585d20e651SIlya Bakulin 		 * Re-tuning modes 1 and 2 restrict the maximum data length
25595d20e651SIlya Bakulin 		 * per read/write command to 4 MiB.
25605d20e651SIlya Bakulin 		 */
25615d20e651SIlya Bakulin 		if (slot->opt & SDHCI_TUNING_ENABLED &&
25625d20e651SIlya Bakulin 		    (slot->retune_mode == SDHCI_RETUNE_MODE_1 ||
25635d20e651SIlya Bakulin 		    slot->retune_mode == SDHCI_RETUNE_MODE_2)) {
25645d20e651SIlya Bakulin 			max_data = 4 * 1024 * 1024 / MMC_SECTOR_SIZE;
25655d20e651SIlya Bakulin 		} else {
25665d20e651SIlya Bakulin 			max_data = 65535;
25675d20e651SIlya Bakulin 		}
25685d20e651SIlya Bakulin 		cts->proto_specific.mmc.host_max_data = max_data;
25695d20e651SIlya Bakulin 
2570a94a63f0SWarner Losh 		memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios));
2571a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2572a94a63f0SWarner Losh 		break;
2573a94a63f0SWarner Losh 	}
2574a94a63f0SWarner Losh 	case XPT_SET_TRAN_SETTINGS:
2575a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2576a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n");
2577a94a63f0SWarner Losh 		sdhci_cam_settran_settings(slot, ccb);
2578a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2579a94a63f0SWarner Losh 		break;
2580a94a63f0SWarner Losh 	case XPT_RESET_BUS:
2581a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2582a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n");
2583a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2584a94a63f0SWarner Losh 		break;
2585a94a63f0SWarner Losh 	case XPT_MMC_IO:
2586a94a63f0SWarner Losh 		/*
2587a94a63f0SWarner Losh 		 * Here is the HW-dependent part of
2588a94a63f0SWarner Losh 		 * sending the command to the underlying h/w
2589a94a63f0SWarner Losh 		 * At some point in the future an interrupt comes.
2590a94a63f0SWarner Losh 		 * Then the request will be marked as completed.
2591a94a63f0SWarner Losh 		 */
2592aca38eabSMarius Strobl 		if (__predict_false(sdhci_debug > 1))
2593a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_MMC_IO\n");
2594a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INPROG;
2595a94a63f0SWarner Losh 
2596160799c6SWarner Losh 		sdhci_cam_request(cam_sim_softc(sim), ccb);
2597a94a63f0SWarner Losh 		return;
2598a94a63f0SWarner Losh 	default:
2599a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INVALID;
2600a94a63f0SWarner Losh 		break;
2601a94a63f0SWarner Losh 	}
2602a94a63f0SWarner Losh 	xpt_done(ccb);
2603a94a63f0SWarner Losh 	return;
2604a94a63f0SWarner Losh }
2605a94a63f0SWarner Losh 
2606a94a63f0SWarner Losh void
2607a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim)
2608a94a63f0SWarner Losh {
2609a94a63f0SWarner Losh 	return;
2610a94a63f0SWarner Losh }
2611a94a63f0SWarner Losh 
26126dea80e6SMarius Strobl static int
2613ab00a509SMarius Strobl sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot,
2614ab00a509SMarius Strobl     int proposed_clock)
26156dea80e6SMarius Strobl {
2616a94a63f0SWarner Losh 	int max_clock, clock, i;
2617a94a63f0SWarner Losh 
2618a94a63f0SWarner Losh 	if (proposed_clock == 0)
2619a94a63f0SWarner Losh 		return 0;
2620a94a63f0SWarner Losh 	max_clock = slot->max_clk;
2621a94a63f0SWarner Losh 	clock = max_clock;
2622a94a63f0SWarner Losh 
2623a94a63f0SWarner Losh 	if (slot->version < SDHCI_SPEC_300) {
2624505f6a0cSBjoern A. Zeeb 		for (i = 0; i < SDHCI_200_MAX_DIVIDER; i <<= 1) {
2625a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2626a94a63f0SWarner Losh 				break;
2627a94a63f0SWarner Losh 			clock >>= 1;
2628a94a63f0SWarner Losh 		}
2629a94a63f0SWarner Losh 	} else {
2630505f6a0cSBjoern A. Zeeb 		for (i = 0; i < SDHCI_300_MAX_DIVIDER; i += 2) {
2631a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2632a94a63f0SWarner Losh 				break;
2633a94a63f0SWarner Losh 			clock = max_clock / (i + 2);
2634a94a63f0SWarner Losh 		}
2635a94a63f0SWarner Losh 	}
2636a94a63f0SWarner Losh 	return clock;
2637a94a63f0SWarner Losh }
2638a94a63f0SWarner Losh 
2639ab00a509SMarius Strobl static int
2640a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb)
2641a94a63f0SWarner Losh {
2642a94a63f0SWarner Losh 	struct mmc_ios *ios;
2643ab00a509SMarius Strobl 	const struct mmc_ios *new_ios;
2644ab00a509SMarius Strobl 	const struct ccb_trans_settings_mmc *cts;
2645a94a63f0SWarner Losh 
2646a94a63f0SWarner Losh 	ios = &slot->host.ios;
2647a94a63f0SWarner Losh 	cts = &ccb->cts.proto_specific.mmc;
2648a94a63f0SWarner Losh 	new_ios = &cts->ios;
2649a94a63f0SWarner Losh 
2650a94a63f0SWarner Losh 	/* Update only requested fields */
2651a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CLK) {
2652a94a63f0SWarner Losh 		ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock);
2653*b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2654a94a63f0SWarner Losh 			slot_printf(slot, "Clock => %d\n", ios->clock);
2655a94a63f0SWarner Losh 	}
2656a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_VDD) {
2657a94a63f0SWarner Losh 		ios->vdd = new_ios->vdd;
2658*b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2659a94a63f0SWarner Losh 			slot_printf(slot, "VDD => %d\n", ios->vdd);
2660a94a63f0SWarner Losh 	}
2661a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CS) {
2662a94a63f0SWarner Losh 		ios->chip_select = new_ios->chip_select;
2663*b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2664a94a63f0SWarner Losh 			slot_printf(slot, "CS => %d\n", ios->chip_select);
2665a94a63f0SWarner Losh 	}
2666a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BW) {
2667a94a63f0SWarner Losh 		ios->bus_width = new_ios->bus_width;
2668*b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2669a94a63f0SWarner Losh 			slot_printf(slot, "Bus width => %d\n", ios->bus_width);
2670a94a63f0SWarner Losh 	}
2671a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_PM) {
2672a94a63f0SWarner Losh 		ios->power_mode = new_ios->power_mode;
2673*b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2674a94a63f0SWarner Losh 			slot_printf(slot, "Power mode => %d\n", ios->power_mode);
2675a94a63f0SWarner Losh 	}
2676a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BT) {
2677a94a63f0SWarner Losh 		ios->timing = new_ios->timing;
2678*b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2679a94a63f0SWarner Losh 			slot_printf(slot, "Timing => %d\n", ios->timing);
2680a94a63f0SWarner Losh 	}
2681a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BM) {
2682a94a63f0SWarner Losh 		ios->bus_mode = new_ios->bus_mode;
2683*b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2684a94a63f0SWarner Losh 			slot_printf(slot, "Bus mode => %d\n", ios->bus_mode);
2685a94a63f0SWarner Losh 	}
2686a94a63f0SWarner Losh 
2687a94a63f0SWarner Losh 	/* XXX Provide a way to call a chip-specific IOS update, required for TI */
2688a94a63f0SWarner Losh 	return (sdhci_cam_update_ios(slot));
2689a94a63f0SWarner Losh }
2690a94a63f0SWarner Losh 
2691ab00a509SMarius Strobl static int
2692a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot)
2693a94a63f0SWarner Losh {
2694a94a63f0SWarner Losh 	struct mmc_ios *ios = &slot->host.ios;
2695a94a63f0SWarner Losh 
2696*b18f2ef4SEmmanuel Vadot 	if (sdhci_debug > 1)
2697a94a63f0SWarner Losh 		slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n",
2698a94a63f0SWarner Losh 		    __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing);
2699a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2700a94a63f0SWarner Losh 	/* Do full reset on bus power down to clear from any state. */
2701a94a63f0SWarner Losh 	if (ios->power_mode == power_off) {
2702a94a63f0SWarner Losh 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
2703a94a63f0SWarner Losh 		sdhci_init(slot);
2704a94a63f0SWarner Losh 	}
2705a94a63f0SWarner Losh 	/* Configure the bus. */
2706a94a63f0SWarner Losh 	sdhci_set_clock(slot, ios->clock);
2707a94a63f0SWarner Losh 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
2708a94a63f0SWarner Losh 	if (ios->bus_width == bus_width_8) {
2709a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
2710a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2711a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_4) {
2712a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2713a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
2714a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_1) {
2715a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2716a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2717a94a63f0SWarner Losh 	} else {
2718a94a63f0SWarner Losh 		panic("Invalid bus width: %d", ios->bus_width);
2719a94a63f0SWarner Losh 	}
2720a94a63f0SWarner Losh 	if (ios->timing == bus_timing_hs &&
2721a94a63f0SWarner Losh 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
2722a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_HISPD;
2723a94a63f0SWarner Losh 	else
2724a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
2725a94a63f0SWarner Losh 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
2726a94a63f0SWarner Losh 	/* Some controllers like reset after bus changes. */
2727a94a63f0SWarner Losh 	if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
2728a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2729a94a63f0SWarner Losh 
2730a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2731a94a63f0SWarner Losh 	return (0);
2732a94a63f0SWarner Losh }
2733a94a63f0SWarner Losh 
2734ab00a509SMarius Strobl static int
2735a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb)
2736a94a63f0SWarner Losh {
2737ab00a509SMarius Strobl 	const struct ccb_mmcio *mmcio;
2738a94a63f0SWarner Losh 
2739a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
2740a94a63f0SWarner Losh 
2741a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2742a94a63f0SWarner Losh /*	if (slot->req != NULL) {
2743a94a63f0SWarner Losh 		SDHCI_UNLOCK(slot);
2744a94a63f0SWarner Losh 		return (EBUSY);
2745a94a63f0SWarner Losh 	}
2746a94a63f0SWarner Losh */
2747aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
27485d5ae066SIlya Bakulin 		slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x "
27495d5ae066SIlya Bakulin 		    "blksz=%zu blkcnt=%zu\n",
2750a94a63f0SWarner Losh 		    mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags,
2751a94a63f0SWarner Losh 		    mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0,
27525d5ae066SIlya Bakulin 		    mmcio->cmd.data != NULL ? mmcio->cmd.data->flags : 0,
27535d5ae066SIlya Bakulin 		    mmcio->cmd.data != NULL ? mmcio->cmd.data->block_size : 0,
27545d5ae066SIlya Bakulin 		    mmcio->cmd.data != NULL ? mmcio->cmd.data->block_count : 0);
2755a94a63f0SWarner Losh 	}
2756a94a63f0SWarner Losh 	if (mmcio->cmd.data != NULL) {
2757a94a63f0SWarner Losh 		if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0)
2758a94a63f0SWarner Losh 			panic("data->len = %d, data->flags = %d -- something is b0rked",
2759a94a63f0SWarner Losh 			    (int)mmcio->cmd.data->len, mmcio->cmd.data->flags);
2760a94a63f0SWarner Losh 	}
2761a94a63f0SWarner Losh 	slot->ccb = ccb;
2762a94a63f0SWarner Losh 	slot->flags = 0;
2763a94a63f0SWarner Losh 	sdhci_start(slot);
2764a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2765a94a63f0SWarner Losh 	if (dumping) {
2766a94a63f0SWarner Losh 		while (slot->ccb != NULL) {
2767a94a63f0SWarner Losh 			sdhci_generic_intr(slot);
2768a94a63f0SWarner Losh 			DELAY(10);
2769a94a63f0SWarner Losh 		}
2770a94a63f0SWarner Losh 	}
2771a94a63f0SWarner Losh 	return (0);
2772a94a63f0SWarner Losh }
277315c440e1SWarner Losh #endif /* MMCCAM */
2774a94a63f0SWarner Losh 
2775ab00a509SMarius Strobl MODULE_VERSION(sdhci, SDHCI_VERSION);
2776