xref: /freebsd/sys/dev/sdhci/sdhci.c (revision 639f59f02a03127173337ecf5b015d167af11bca)
1831f5dcfSAlexander Motin /*-
2831f5dcfSAlexander Motin  * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
3831f5dcfSAlexander Motin  * All rights reserved.
4831f5dcfSAlexander Motin  *
5831f5dcfSAlexander Motin  * Redistribution and use in source and binary forms, with or without
6831f5dcfSAlexander Motin  * modification, are permitted provided that the following conditions
7831f5dcfSAlexander Motin  * are met:
8831f5dcfSAlexander Motin  * 1. Redistributions of source code must retain the above copyright
9831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer.
10831f5dcfSAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
11831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
12831f5dcfSAlexander Motin  *    documentation and/or other materials provided with the distribution.
13831f5dcfSAlexander Motin  *
14831f5dcfSAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15831f5dcfSAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16831f5dcfSAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17831f5dcfSAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18831f5dcfSAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19831f5dcfSAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20831f5dcfSAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21831f5dcfSAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22831f5dcfSAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23831f5dcfSAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24831f5dcfSAlexander Motin  */
25831f5dcfSAlexander Motin 
26831f5dcfSAlexander Motin #include <sys/cdefs.h>
27831f5dcfSAlexander Motin __FBSDID("$FreeBSD$");
28831f5dcfSAlexander Motin 
29831f5dcfSAlexander Motin #include <sys/param.h>
30831f5dcfSAlexander Motin #include <sys/systm.h>
31831f5dcfSAlexander Motin #include <sys/bus.h>
32e64f01a9SIan Lepore #include <sys/callout.h>
33831f5dcfSAlexander Motin #include <sys/conf.h>
34831f5dcfSAlexander Motin #include <sys/kernel.h>
35831f5dcfSAlexander Motin #include <sys/lock.h>
36831f5dcfSAlexander Motin #include <sys/module.h>
37831f5dcfSAlexander Motin #include <sys/mutex.h>
38831f5dcfSAlexander Motin #include <sys/resource.h>
39831f5dcfSAlexander Motin #include <sys/rman.h>
405b69a497SAlexander Motin #include <sys/sysctl.h>
41831f5dcfSAlexander Motin #include <sys/taskqueue.h>
42831f5dcfSAlexander Motin 
43831f5dcfSAlexander Motin #include <machine/bus.h>
44831f5dcfSAlexander Motin #include <machine/resource.h>
45831f5dcfSAlexander Motin #include <machine/stdarg.h>
46831f5dcfSAlexander Motin 
47831f5dcfSAlexander Motin #include <dev/mmc/bridge.h>
48831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h>
49831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h>
50831f5dcfSAlexander Motin 
51831f5dcfSAlexander Motin #include "mmcbr_if.h"
52831f5dcfSAlexander Motin #include "sdhci.h"
53d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h"
54831f5dcfSAlexander Motin 
55f0d2731dSMarius Strobl SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver");
56831f5dcfSAlexander Motin 
57f0d2731dSMarius Strobl static int sdhci_debug;
58af3b2549SHans Petter Selasky SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, "Debug level");
595b69a497SAlexander Motin 
60d6b3aaf8SOleksandr Tymoshenko #define RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
61d6b3aaf8SOleksandr Tymoshenko #define RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
62d6b3aaf8SOleksandr Tymoshenko #define RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
63d6b3aaf8SOleksandr Tymoshenko #define RD_MULTI_4(slot, off, ptr, count)	\
64d6b3aaf8SOleksandr Tymoshenko     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
65831f5dcfSAlexander Motin 
66d6b3aaf8SOleksandr Tymoshenko #define WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
67d6b3aaf8SOleksandr Tymoshenko #define WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
68d6b3aaf8SOleksandr Tymoshenko #define WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
69d6b3aaf8SOleksandr Tymoshenko #define WR_MULTI_4(slot, off, ptr, count)	\
70d6b3aaf8SOleksandr Tymoshenko     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
71831f5dcfSAlexander Motin 
72831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
73831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot);
74831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data);
75831f5dcfSAlexander Motin 
76*639f59f0SIan Lepore static void sdhci_card_poll(void *);
77831f5dcfSAlexander Motin static void sdhci_card_task(void *, int);
78831f5dcfSAlexander Motin 
79831f5dcfSAlexander Motin /* helper routines */
80831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot)		mtx_lock(&(_slot)->mtx)
81831f5dcfSAlexander Motin #define	SDHCI_UNLOCK(_slot)		mtx_unlock(&(_slot)->mtx)
82831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \
83831f5dcfSAlexander Motin 	mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
84831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot)	mtx_destroy(&_slot->mtx);
85831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot)	mtx_assert(&_slot->mtx, MA_OWNED);
86831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot)	mtx_assert(&_slot->mtx, MA_NOTOWNED);
87831f5dcfSAlexander Motin 
8833aad34dSOleksandr Tymoshenko #define	SDHCI_DEFAULT_MAX_FREQ	50
8933aad34dSOleksandr Tymoshenko 
9057677a3aSOleksandr Tymoshenko #define	SDHCI_200_MAX_DIVIDER	256
9157677a3aSOleksandr Tymoshenko #define	SDHCI_300_MAX_DIVIDER	2046
9257677a3aSOleksandr Tymoshenko 
93*639f59f0SIan Lepore #define	SDHCI_CARD_PRESENT_TICKS	(hz / 5)
94*639f59f0SIan Lepore #define	SDHCI_INSERT_DELAY_TICKS	(hz / 2)
95*639f59f0SIan Lepore 
9693efdc63SAdrian Chadd /*
9793efdc63SAdrian Chadd  * Broadcom BCM577xx Controller Constants
9893efdc63SAdrian Chadd  */
9993efdc63SAdrian Chadd #define BCM577XX_DEFAULT_MAX_DIVIDER	256		/* Maximum divider supported by the default clock source. */
10093efdc63SAdrian Chadd #define BCM577XX_ALT_CLOCK_BASE		63000000	/* Alternative clock's base frequency. */
10193efdc63SAdrian Chadd 
10293efdc63SAdrian Chadd #define BCM577XX_HOST_CONTROL		0x198
10393efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_MASK	0xFFFFCFFF
10493efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_SHIFT	12
10593efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_DEFAULT	0x0
10693efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_64MHZ	0x3
10793efdc63SAdrian Chadd 
10893efdc63SAdrian Chadd 
109831f5dcfSAlexander Motin static void
110831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
111831f5dcfSAlexander Motin {
112831f5dcfSAlexander Motin 	if (error != 0) {
113831f5dcfSAlexander Motin 		printf("getaddr: error %d\n", error);
114831f5dcfSAlexander Motin 		return;
115831f5dcfSAlexander Motin 	}
116831f5dcfSAlexander Motin 	*(bus_addr_t *)arg = segs[0].ds_addr;
117831f5dcfSAlexander Motin }
118831f5dcfSAlexander Motin 
119d6b3aaf8SOleksandr Tymoshenko static int
120d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
121d6b3aaf8SOleksandr Tymoshenko {
122d6b3aaf8SOleksandr Tymoshenko 	va_list ap;
123d6b3aaf8SOleksandr Tymoshenko 	int retval;
124d6b3aaf8SOleksandr Tymoshenko 
125d6b3aaf8SOleksandr Tymoshenko     	retval = printf("%s-slot%d: ",
126d6b3aaf8SOleksandr Tymoshenko 	    device_get_nameunit(slot->bus), slot->num);
127d6b3aaf8SOleksandr Tymoshenko 
128d6b3aaf8SOleksandr Tymoshenko 	va_start(ap, fmt);
129d6b3aaf8SOleksandr Tymoshenko 	retval += vprintf(fmt, ap);
130d6b3aaf8SOleksandr Tymoshenko 	va_end(ap);
131d6b3aaf8SOleksandr Tymoshenko 	return (retval);
132d6b3aaf8SOleksandr Tymoshenko }
133d6b3aaf8SOleksandr Tymoshenko 
134831f5dcfSAlexander Motin static void
135831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot)
136831f5dcfSAlexander Motin {
137831f5dcfSAlexander Motin 	slot_printf(slot,
138831f5dcfSAlexander Motin 	    "============== REGISTER DUMP ==============\n");
139831f5dcfSAlexander Motin 
140831f5dcfSAlexander Motin 	slot_printf(slot, "Sys addr: 0x%08x | Version:  0x%08x\n",
141831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
142831f5dcfSAlexander Motin 	slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
143831f5dcfSAlexander Motin 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
144831f5dcfSAlexander Motin 	slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
145831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
146831f5dcfSAlexander Motin 	slot_printf(slot, "Present:  0x%08x | Host ctl: 0x%08x\n",
147831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
148831f5dcfSAlexander Motin 	slot_printf(slot, "Power:    0x%08x | Blk gap:  0x%08x\n",
149831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
150831f5dcfSAlexander Motin 	slot_printf(slot, "Wake-up:  0x%08x | Clock:    0x%08x\n",
151831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
152831f5dcfSAlexander Motin 	slot_printf(slot, "Timeout:  0x%08x | Int stat: 0x%08x\n",
153831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
154831f5dcfSAlexander Motin 	slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
155831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
156831f5dcfSAlexander Motin 	slot_printf(slot, "AC12 err: 0x%08x | Slot int: 0x%08x\n",
157831f5dcfSAlexander Motin 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_SLOT_INT_STATUS));
158831f5dcfSAlexander Motin 	slot_printf(slot, "Caps:     0x%08x | Max curr: 0x%08x\n",
159831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_MAX_CURRENT));
160831f5dcfSAlexander Motin 
161831f5dcfSAlexander Motin 	slot_printf(slot,
162831f5dcfSAlexander Motin 	    "===========================================\n");
163831f5dcfSAlexander Motin }
164831f5dcfSAlexander Motin 
165831f5dcfSAlexander Motin static void
166831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
167831f5dcfSAlexander Motin {
168831f5dcfSAlexander Motin 	int timeout;
169831f5dcfSAlexander Motin 
170d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
1716e37fb2bSIan Lepore 		if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
172831f5dcfSAlexander Motin 			return;
173831f5dcfSAlexander Motin 	}
174831f5dcfSAlexander Motin 
175831f5dcfSAlexander Motin 	/* Some controllers need this kick or reset won't work. */
176831f5dcfSAlexander Motin 	if ((mask & SDHCI_RESET_ALL) == 0 &&
177d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
178831f5dcfSAlexander Motin 		uint32_t clock;
179831f5dcfSAlexander Motin 
180831f5dcfSAlexander Motin 		/* This is to force an update */
181831f5dcfSAlexander Motin 		clock = slot->clock;
182831f5dcfSAlexander Motin 		slot->clock = 0;
183831f5dcfSAlexander Motin 		sdhci_set_clock(slot, clock);
184831f5dcfSAlexander Motin 	}
185831f5dcfSAlexander Motin 
186d8208d9eSAlexander Motin 	if (mask & SDHCI_RESET_ALL) {
187831f5dcfSAlexander Motin 		slot->clock = 0;
188d8208d9eSAlexander Motin 		slot->power = 0;
189d8208d9eSAlexander Motin 	}
190831f5dcfSAlexander Motin 
19161bc42f7SIan Lepore 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
19261bc42f7SIan Lepore 
19361bc42f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
19461bc42f7SIan Lepore 		/*
19561bc42f7SIan Lepore 		 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
19661bc42f7SIan Lepore 		 * specification.  The reset bit has internal propagation delay,
19761bc42f7SIan Lepore 		 * so a fast read after write returns 0 even if reset process is
19861bc42f7SIan Lepore 		 * in progress. The workaround is to poll for 1 before polling
19961bc42f7SIan Lepore 		 * for 0.  In the worst case, if we miss seeing it asserted the
20061bc42f7SIan Lepore 		 * time we spent waiting is enough to ensure the reset finishes.
20161bc42f7SIan Lepore 		 */
20261bc42f7SIan Lepore 		timeout = 10000;
20361bc42f7SIan Lepore 		while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
20461bc42f7SIan Lepore 			if (timeout <= 0)
20561bc42f7SIan Lepore 				break;
20661bc42f7SIan Lepore 			timeout--;
20761bc42f7SIan Lepore 			DELAY(1);
20861bc42f7SIan Lepore 		}
20961bc42f7SIan Lepore 	}
21061bc42f7SIan Lepore 
211831f5dcfSAlexander Motin 	/* Wait max 100 ms */
21261bc42f7SIan Lepore 	timeout = 10000;
213831f5dcfSAlexander Motin 	/* Controller clears the bits when it's done */
21461bc42f7SIan Lepore 	while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
21561bc42f7SIan Lepore 		if (timeout <= 0) {
21661bc42f7SIan Lepore 			slot_printf(slot, "Reset 0x%x never completed.\n",
21761bc42f7SIan Lepore 			    mask);
218831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
219831f5dcfSAlexander Motin 			return;
220831f5dcfSAlexander Motin 		}
221831f5dcfSAlexander Motin 		timeout--;
22261bc42f7SIan Lepore 		DELAY(10);
223831f5dcfSAlexander Motin 	}
224831f5dcfSAlexander Motin }
225831f5dcfSAlexander Motin 
226831f5dcfSAlexander Motin static void
227831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot)
228831f5dcfSAlexander Motin {
229831f5dcfSAlexander Motin 
230831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
231831f5dcfSAlexander Motin 
232831f5dcfSAlexander Motin 	/* Enable interrupts. */
233831f5dcfSAlexander Motin 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
234831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
235831f5dcfSAlexander Motin 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
236831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
237831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
238831f5dcfSAlexander Motin 	    SDHCI_INT_ACMD12ERR;
239*639f59f0SIan Lepore 
240*639f59f0SIan Lepore 	if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
241*639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
242*639f59f0SIan Lepore 		slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
243*639f59f0SIan Lepore 	}
244*639f59f0SIan Lepore 
245831f5dcfSAlexander Motin 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
246831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
247831f5dcfSAlexander Motin }
248831f5dcfSAlexander Motin 
249831f5dcfSAlexander Motin static void
250831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
251831f5dcfSAlexander Motin {
25293efdc63SAdrian Chadd 	uint32_t clk_base;
25393efdc63SAdrian Chadd 	uint32_t clk_sel;
254831f5dcfSAlexander Motin 	uint32_t res;
255831f5dcfSAlexander Motin 	uint16_t clk;
2568f3b7d56SOleksandr Tymoshenko 	uint16_t div;
257831f5dcfSAlexander Motin 	int timeout;
258831f5dcfSAlexander Motin 
259831f5dcfSAlexander Motin 	if (clock == slot->clock)
260831f5dcfSAlexander Motin 		return;
261831f5dcfSAlexander Motin 	slot->clock = clock;
262831f5dcfSAlexander Motin 
263831f5dcfSAlexander Motin 	/* Turn off the clock. */
2644ddc0172SIan Lepore 	clk = RD2(slot, SDHCI_CLOCK_CONTROL);
2654ddc0172SIan Lepore 	WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
266831f5dcfSAlexander Motin 	/* If no clock requested - left it so. */
267831f5dcfSAlexander Motin 	if (clock == 0)
268831f5dcfSAlexander Motin 		return;
269ceb9e9f7SIan Lepore 
27093efdc63SAdrian Chadd 	/* Determine the clock base frequency */
27193efdc63SAdrian Chadd 	clk_base = slot->max_clk;
27293efdc63SAdrian Chadd 	if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
27393efdc63SAdrian Chadd 		clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & BCM577XX_CTRL_CLKSEL_MASK;
27493efdc63SAdrian Chadd 
27593efdc63SAdrian Chadd 		/* Select clock source appropriate for the requested frequency. */
27693efdc63SAdrian Chadd 		if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
27793efdc63SAdrian Chadd 			clk_base = BCM577XX_ALT_CLOCK_BASE;
27893efdc63SAdrian Chadd 			clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << BCM577XX_CTRL_CLKSEL_SHIFT);
27993efdc63SAdrian Chadd 		} else {
28093efdc63SAdrian Chadd 			clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << BCM577XX_CTRL_CLKSEL_SHIFT);
28193efdc63SAdrian Chadd 		}
28293efdc63SAdrian Chadd 
28393efdc63SAdrian Chadd 		WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
28493efdc63SAdrian Chadd 	}
28593efdc63SAdrian Chadd 
286ceb9e9f7SIan Lepore 	/* Recalculate timeout clock frequency based on the new sd clock. */
287ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
288ceb9e9f7SIan Lepore 		slot->timeout_clk = slot->clock / 1000;
289ceb9e9f7SIan Lepore 
2908f3b7d56SOleksandr Tymoshenko 	if (slot->version < SDHCI_SPEC_300) {
291831f5dcfSAlexander Motin 		/* Looking for highest freq <= clock. */
29293efdc63SAdrian Chadd 		res = clk_base;
29357677a3aSOleksandr Tymoshenko 		for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
294831f5dcfSAlexander Motin 			if (res <= clock)
295831f5dcfSAlexander Motin 				break;
296831f5dcfSAlexander Motin 			res >>= 1;
297831f5dcfSAlexander Motin 		}
298831f5dcfSAlexander Motin 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
2998f3b7d56SOleksandr Tymoshenko 		div >>= 1;
3008f3b7d56SOleksandr Tymoshenko 	}
3018f3b7d56SOleksandr Tymoshenko 	else {
3028f3b7d56SOleksandr Tymoshenko 		/* Version 3.0 divisors are multiples of two up to 1023*2 */
30393efdc63SAdrian Chadd 		if (clock >= clk_base)
30457677a3aSOleksandr Tymoshenko 			div = 0;
3058f3b7d56SOleksandr Tymoshenko 		else {
30657677a3aSOleksandr Tymoshenko 			for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
30793efdc63SAdrian Chadd 				if ((clk_base / div) <= clock)
3088f3b7d56SOleksandr Tymoshenko 					break;
3098f3b7d56SOleksandr Tymoshenko 			}
3108f3b7d56SOleksandr Tymoshenko 		}
3118f3b7d56SOleksandr Tymoshenko 		div >>= 1;
3128f3b7d56SOleksandr Tymoshenko 	}
3138f3b7d56SOleksandr Tymoshenko 
3148f3b7d56SOleksandr Tymoshenko 	if (bootverbose || sdhci_debug)
31593efdc63SAdrian Chadd 		slot_printf(slot, "Divider %d for freq %d (base %d)\n",
31693efdc63SAdrian Chadd 			div, clock, clk_base);
3178f3b7d56SOleksandr Tymoshenko 
318831f5dcfSAlexander Motin 	/* Now we have got divider, set it. */
3198f3b7d56SOleksandr Tymoshenko 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
3208f3b7d56SOleksandr Tymoshenko 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
3218f3b7d56SOleksandr Tymoshenko 		<< SDHCI_DIVIDER_HI_SHIFT;
3228f3b7d56SOleksandr Tymoshenko 
323831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
324831f5dcfSAlexander Motin 	/* Enable clock. */
325831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_INT_EN;
326831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
327831f5dcfSAlexander Motin 	/* Wait up to 10 ms until it stabilize. */
328831f5dcfSAlexander Motin 	timeout = 10;
329831f5dcfSAlexander Motin 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
330831f5dcfSAlexander Motin 		& SDHCI_CLOCK_INT_STABLE)) {
331831f5dcfSAlexander Motin 		if (timeout == 0) {
332831f5dcfSAlexander Motin 			slot_printf(slot,
333831f5dcfSAlexander Motin 			    "Internal clock never stabilised.\n");
334831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
335831f5dcfSAlexander Motin 			return;
336831f5dcfSAlexander Motin 		}
337831f5dcfSAlexander Motin 		timeout--;
338831f5dcfSAlexander Motin 		DELAY(1000);
339831f5dcfSAlexander Motin 	}
340831f5dcfSAlexander Motin 	/* Pass clock signal to the bus. */
341831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_CARD_EN;
342831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
343831f5dcfSAlexander Motin }
344831f5dcfSAlexander Motin 
345831f5dcfSAlexander Motin static void
346831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power)
347831f5dcfSAlexander Motin {
348831f5dcfSAlexander Motin 	uint8_t pwr;
349831f5dcfSAlexander Motin 
350831f5dcfSAlexander Motin 	if (slot->power == power)
351831f5dcfSAlexander Motin 		return;
352d6b3aaf8SOleksandr Tymoshenko 
353831f5dcfSAlexander Motin 	slot->power = power;
354831f5dcfSAlexander Motin 
355831f5dcfSAlexander Motin 	/* Turn off the power. */
356831f5dcfSAlexander Motin 	pwr = 0;
357831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
358831f5dcfSAlexander Motin 	/* If power down requested - left it so. */
359831f5dcfSAlexander Motin 	if (power == 0)
360831f5dcfSAlexander Motin 		return;
361831f5dcfSAlexander Motin 	/* Set voltage. */
362831f5dcfSAlexander Motin 	switch (1 << power) {
363831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
364831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
365831f5dcfSAlexander Motin 		break;
366831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
367831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
368831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
369831f5dcfSAlexander Motin 		break;
370831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
371831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
372831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
373831f5dcfSAlexander Motin 		break;
374831f5dcfSAlexander Motin 	}
375831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
376831f5dcfSAlexander Motin 	/* Turn on the power. */
377831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
378831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
379831f5dcfSAlexander Motin }
380831f5dcfSAlexander Motin 
381831f5dcfSAlexander Motin static void
382831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
383831f5dcfSAlexander Motin {
384831f5dcfSAlexander Motin 	uint32_t data;
385831f5dcfSAlexander Motin 	char *buffer;
386831f5dcfSAlexander Motin 	size_t left;
387831f5dcfSAlexander Motin 
388831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
389831f5dcfSAlexander Motin 	buffer += slot->offset;
390831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
391831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
392831f5dcfSAlexander Motin 	slot->offset += left;
393831f5dcfSAlexander Motin 
394831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
395d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
396831f5dcfSAlexander Motin 		DELAY(10);
397ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
398831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
399831f5dcfSAlexander Motin 		while (left > 3) {
400831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
401831f5dcfSAlexander Motin 			buffer[0] = data;
402831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
403831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
404831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
405831f5dcfSAlexander Motin 			buffer += 4;
406831f5dcfSAlexander Motin 			left -= 4;
407831f5dcfSAlexander Motin 		}
408831f5dcfSAlexander Motin 	} else {
409d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
410831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
411831f5dcfSAlexander Motin 		left &= 3;
412831f5dcfSAlexander Motin 	}
413831f5dcfSAlexander Motin 	/* Handle uneven size case. */
414831f5dcfSAlexander Motin 	if (left > 0) {
415831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
416831f5dcfSAlexander Motin 		while (left > 0) {
417831f5dcfSAlexander Motin 			*(buffer++) = data;
418831f5dcfSAlexander Motin 			data >>= 8;
419831f5dcfSAlexander Motin 			left--;
420831f5dcfSAlexander Motin 		}
421831f5dcfSAlexander Motin 	}
422831f5dcfSAlexander Motin }
423831f5dcfSAlexander Motin 
424831f5dcfSAlexander Motin static void
425831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
426831f5dcfSAlexander Motin {
427831f5dcfSAlexander Motin 	uint32_t data = 0;
428831f5dcfSAlexander Motin 	char *buffer;
429831f5dcfSAlexander Motin 	size_t left;
430831f5dcfSAlexander Motin 
431831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
432831f5dcfSAlexander Motin 	buffer += slot->offset;
433831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
434831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
435831f5dcfSAlexander Motin 	slot->offset += left;
436831f5dcfSAlexander Motin 
437ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
438831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
439831f5dcfSAlexander Motin 		while (left > 3) {
440831f5dcfSAlexander Motin 			data = buffer[0] +
441831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
442831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
443831f5dcfSAlexander Motin 			    (buffer[3] << 24);
444831f5dcfSAlexander Motin 			left -= 4;
445831f5dcfSAlexander Motin 			buffer += 4;
446831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
447831f5dcfSAlexander Motin 		}
448831f5dcfSAlexander Motin 	} else {
449d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
450831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
451831f5dcfSAlexander Motin 		left &= 3;
452831f5dcfSAlexander Motin 	}
453831f5dcfSAlexander Motin 	/* Handle uneven size case. */
454831f5dcfSAlexander Motin 	if (left > 0) {
455831f5dcfSAlexander Motin 		while (left > 0) {
456831f5dcfSAlexander Motin 			data <<= 8;
457831f5dcfSAlexander Motin 			data += *(buffer++);
458831f5dcfSAlexander Motin 			left--;
459831f5dcfSAlexander Motin 		}
460831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
461831f5dcfSAlexander Motin 	}
462831f5dcfSAlexander Motin }
463831f5dcfSAlexander Motin 
464831f5dcfSAlexander Motin static void
465831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
466831f5dcfSAlexander Motin {
467831f5dcfSAlexander Motin 
468831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
469831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
470831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
471831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
472831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
473831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
474831f5dcfSAlexander Motin 				break;
475831f5dcfSAlexander Motin 		}
476831f5dcfSAlexander Motin 	} else {
477831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
478831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
479831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
480831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
481831f5dcfSAlexander Motin 				break;
482831f5dcfSAlexander Motin 		}
483831f5dcfSAlexander Motin 	}
484831f5dcfSAlexander Motin }
485831f5dcfSAlexander Motin 
486831f5dcfSAlexander Motin static void
487831f5dcfSAlexander Motin sdhci_card_task(void *arg, int pending)
488831f5dcfSAlexander Motin {
489831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
490831f5dcfSAlexander Motin 
491831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
4926e37fb2bSIan Lepore 	if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
493831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
494831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
495*639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
496*639f59f0SIan Lepore 				slot_printf(slot, "Card inserted\n");
497d6b3aaf8SOleksandr Tymoshenko 			slot->dev = device_add_child(slot->bus, "mmc", -1);
498831f5dcfSAlexander Motin 			device_set_ivars(slot->dev, slot);
499831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
500831f5dcfSAlexander Motin 			device_probe_and_attach(slot->dev);
501831f5dcfSAlexander Motin 		} else
502831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
503831f5dcfSAlexander Motin 	} else {
504831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
505831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
506*639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
507*639f59f0SIan Lepore 				slot_printf(slot, "Card removed\n");
508831f5dcfSAlexander Motin 			device_t d = slot->dev;
509831f5dcfSAlexander Motin 			slot->dev = NULL;
510831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
511d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
512831f5dcfSAlexander Motin 		} else
513831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
514831f5dcfSAlexander Motin 	}
515831f5dcfSAlexander Motin }
516831f5dcfSAlexander Motin 
517*639f59f0SIan Lepore void
518*639f59f0SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
519*639f59f0SIan Lepore {
520*639f59f0SIan Lepore 	bool was_present;
521*639f59f0SIan Lepore 
522*639f59f0SIan Lepore 	/*
523*639f59f0SIan Lepore 	 * If there was no card and now there is one, schedule the task to
524*639f59f0SIan Lepore 	 * create the child device after a short delay.  The delay is to
525*639f59f0SIan Lepore 	 * debounce the card insert (sometimes the card detect pin stabilizes
526*639f59f0SIan Lepore 	 * before the other pins have made good contact).
527*639f59f0SIan Lepore 	 *
528*639f59f0SIan Lepore 	 * If there was a card present and now it's gone, immediately schedule
529*639f59f0SIan Lepore 	 * the task to delete the child device.  No debouncing -- gone is gone,
530*639f59f0SIan Lepore 	 * because once power is removed, a full card re-init is needed, and
531*639f59f0SIan Lepore 	 * that happens by deleting and recreating the child device.
532*639f59f0SIan Lepore 	 */
533*639f59f0SIan Lepore 	SDHCI_LOCK(slot);
534*639f59f0SIan Lepore 	was_present = slot->dev != NULL;
535*639f59f0SIan Lepore 	if (!was_present && is_present) {
536*639f59f0SIan Lepore 		taskqueue_enqueue_timeout(taskqueue_swi_giant,
537*639f59f0SIan Lepore 		    &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
538*639f59f0SIan Lepore 	} else if (was_present && !is_present) {
539*639f59f0SIan Lepore 		taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
540*639f59f0SIan Lepore 	}
541*639f59f0SIan Lepore 	SDHCI_UNLOCK(slot);
542*639f59f0SIan Lepore }
543*639f59f0SIan Lepore 
544*639f59f0SIan Lepore static void
545*639f59f0SIan Lepore sdhci_card_poll(void *arg)
546*639f59f0SIan Lepore {
547*639f59f0SIan Lepore 	struct sdhci_slot *slot = arg;
548*639f59f0SIan Lepore 
549*639f59f0SIan Lepore 	sdhci_handle_card_present(slot,
550*639f59f0SIan Lepore 	    SDHCI_GET_CARD_PRESENT(slot->bus, slot));
551*639f59f0SIan Lepore 	callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
552*639f59f0SIan Lepore 	    sdhci_card_poll, slot);
553*639f59f0SIan Lepore }
554*639f59f0SIan Lepore 
555d6b3aaf8SOleksandr Tymoshenko int
556d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
557831f5dcfSAlexander Motin {
55887a6a871SIan Lepore 	uint32_t caps, freq;
559d6b3aaf8SOleksandr Tymoshenko 	int err;
560831f5dcfSAlexander Motin 
561831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
562d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
563d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
564d6b3aaf8SOleksandr Tymoshenko 
565831f5dcfSAlexander Motin 	/* Allocate DMA tag. */
566831f5dcfSAlexander Motin 	err = bus_dma_tag_create(bus_get_dma_tag(dev),
567831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
568831f5dcfSAlexander Motin 	    BUS_SPACE_MAXADDR, NULL, NULL,
569831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE,
570831f5dcfSAlexander Motin 	    BUS_DMA_ALLOCNOW, NULL, NULL,
571831f5dcfSAlexander Motin 	    &slot->dmatag);
572831f5dcfSAlexander Motin 	if (err != 0) {
573831f5dcfSAlexander Motin 		device_printf(dev, "Can't create DMA tag\n");
574831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
575d6b3aaf8SOleksandr Tymoshenko 		return (err);
576831f5dcfSAlexander Motin 	}
577831f5dcfSAlexander Motin 	/* Allocate DMA memory. */
578831f5dcfSAlexander Motin 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
579831f5dcfSAlexander Motin 	    BUS_DMA_NOWAIT, &slot->dmamap);
580831f5dcfSAlexander Motin 	if (err != 0) {
581831f5dcfSAlexander Motin 		device_printf(dev, "Can't alloc DMA memory\n");
582831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
583d6b3aaf8SOleksandr Tymoshenko 		return (err);
584831f5dcfSAlexander Motin 	}
585831f5dcfSAlexander Motin 	/* Map the memory. */
586831f5dcfSAlexander Motin 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
587831f5dcfSAlexander Motin 	    (void *)slot->dmamem, DMA_BLOCK_SIZE,
588831f5dcfSAlexander Motin 	    sdhci_getaddr, &slot->paddr, 0);
589831f5dcfSAlexander Motin 	if (err != 0 || slot->paddr == 0) {
590831f5dcfSAlexander Motin 		device_printf(dev, "Can't load DMA memory\n");
591831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
592d6b3aaf8SOleksandr Tymoshenko 		if(err)
593d6b3aaf8SOleksandr Tymoshenko 			return (err);
594d6b3aaf8SOleksandr Tymoshenko 		else
595d6b3aaf8SOleksandr Tymoshenko 			return (EFAULT);
596831f5dcfSAlexander Motin 	}
597d6b3aaf8SOleksandr Tymoshenko 
598831f5dcfSAlexander Motin 	/* Initialize slot. */
599831f5dcfSAlexander Motin 	sdhci_init(slot);
600d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
601d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
6028f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS)
6038f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
6048f3b7d56SOleksandr Tymoshenko 	else
605831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
606831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
60733aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
60887a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
60987a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
61033aad34dSOleksandr Tymoshenko 	else
61187a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
61287a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
61387a6a871SIan Lepore 	if (freq != 0)
61487a6a871SIan Lepore 		slot->max_clk = freq * 1000000;
61587a6a871SIan Lepore 	/*
61687a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
61787a6a871SIan Lepore 	 * hasn't already set max_clk we're probably not going to work right
61887a6a871SIan Lepore 	 * with an assumption, so complain about it.
61987a6a871SIan Lepore 	 */
620831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
62187a6a871SIan Lepore 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
622831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify base clock "
62333aad34dSOleksandr Tymoshenko 		    "frequency, using %dMHz as default.\n", SDHCI_DEFAULT_MAX_FREQ);
624831f5dcfSAlexander Motin 	}
625831f5dcfSAlexander Motin 	/* Calculate timeout clock frequency. */
6268f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
6278f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
6288f3b7d56SOleksandr Tymoshenko 	} else {
629831f5dcfSAlexander Motin 		slot->timeout_clk =
630831f5dcfSAlexander Motin 			(caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
6318f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
6328f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
6338f3b7d56SOleksandr Tymoshenko 	}
63487a6a871SIan Lepore 	/*
63587a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
63687a6a871SIan Lepore 	 * hasn't already set timeout_clk we'll probably work okay using the
63787a6a871SIan Lepore 	 * max timeout, but still mention it.
63887a6a871SIan Lepore 	 */
639831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
640831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify timeout clock "
641ceb9e9f7SIan Lepore 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
642ceb9e9f7SIan Lepore 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
643831f5dcfSAlexander Motin 	}
644831f5dcfSAlexander Motin 
64557677a3aSOleksandr Tymoshenko 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
646831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
647831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
648831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
649831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
650831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
651831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
652831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_180)
653831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
654831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
655831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't report any "
656831f5dcfSAlexander Motin 		    "support voltages.\n");
657831f5dcfSAlexander Motin 	}
658831f5dcfSAlexander Motin 	slot->host.caps = MMC_CAP_4_BIT_DATA;
6592d1731b8SIan Lepore 	if (caps & SDHCI_CAN_DO_8BITBUS)
6602d1731b8SIan Lepore 		slot->host.caps |= MMC_CAP_8_BIT_DATA;
661831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
662831f5dcfSAlexander Motin 		slot->host.caps |= MMC_CAP_HSPEED;
663831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
664831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
665831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
666d6b3aaf8SOleksandr Tymoshenko 
667d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
668831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
669d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
670831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
671831f5dcfSAlexander Motin 
672c3a0f75aSOleksandr Tymoshenko 	/*
673c3a0f75aSOleksandr Tymoshenko 	 * Use platform-provided transfer backend
674c3a0f75aSOleksandr Tymoshenko 	 * with PIO as a fallback mechanism
675c3a0f75aSOleksandr Tymoshenko 	 */
676c3a0f75aSOleksandr Tymoshenko 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
677c3a0f75aSOleksandr Tymoshenko 		slot->opt &= ~SDHCI_HAVE_DMA;
678c3a0f75aSOleksandr Tymoshenko 
6795b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
6802d1731b8SIan Lepore 		slot_printf(slot, "%uMHz%s %s%s%s%s %s\n",
681831f5dcfSAlexander Motin 		    slot->max_clk / 1000000,
682831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
6832d1731b8SIan Lepore 		    (caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
6842d1731b8SIan Lepore 			((caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
685831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
686831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
687831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "",
688831f5dcfSAlexander Motin 		    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO");
689831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
690831f5dcfSAlexander Motin 	}
691831f5dcfSAlexander Motin 
692ba6fc1c7SLuiz Otavio O Souza 	slot->timeout = 10;
693ba6fc1c7SLuiz Otavio O Souza 	SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
694ba6fc1c7SLuiz Otavio O Souza 	    SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
695ba6fc1c7SLuiz Otavio O Souza 	    "timeout", CTLFLAG_RW, &slot->timeout, 0,
696ba6fc1c7SLuiz Otavio O Souza 	    "Maximum timeout for SDHCI transfers (in secs)");
697831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
698*639f59f0SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
699*639f59f0SIan Lepore 		sdhci_card_task, slot);
700*639f59f0SIan Lepore 	callout_init(&slot->card_poll_callout, 1);
701e64f01a9SIan Lepore 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
702ba6fc1c7SLuiz Otavio O Souza 
703*639f59f0SIan Lepore 	if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
704*639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
705*639f59f0SIan Lepore 		callout_reset(&slot->card_poll_callout,
706*639f59f0SIan Lepore 		    SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
707*639f59f0SIan Lepore 	}
708*639f59f0SIan Lepore 
709831f5dcfSAlexander Motin 	return (0);
710831f5dcfSAlexander Motin }
711831f5dcfSAlexander Motin 
712d6b3aaf8SOleksandr Tymoshenko void
713d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
714831f5dcfSAlexander Motin {
715d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
716d6b3aaf8SOleksandr Tymoshenko }
717831f5dcfSAlexander Motin 
718d6b3aaf8SOleksandr Tymoshenko int
719d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
720d6b3aaf8SOleksandr Tymoshenko {
721831f5dcfSAlexander Motin 	device_t d;
722831f5dcfSAlexander Motin 
723e64f01a9SIan Lepore 	callout_drain(&slot->timeout_callout);
724*639f59f0SIan Lepore 	callout_drain(&slot->card_poll_callout);
725831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
726*639f59f0SIan Lepore 	taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
727831f5dcfSAlexander Motin 
728831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
729831f5dcfSAlexander Motin 	d = slot->dev;
730831f5dcfSAlexander Motin 	slot->dev = NULL;
731831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
732831f5dcfSAlexander Motin 	if (d != NULL)
733d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
734831f5dcfSAlexander Motin 
735831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
736831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
737831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
738831f5dcfSAlexander Motin 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
739831f5dcfSAlexander Motin 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
740831f5dcfSAlexander Motin 	bus_dma_tag_destroy(slot->dmatag);
741d6b3aaf8SOleksandr Tymoshenko 
742831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
743d6b3aaf8SOleksandr Tymoshenko 
744831f5dcfSAlexander Motin 	return (0);
745831f5dcfSAlexander Motin }
746831f5dcfSAlexander Motin 
747d6b3aaf8SOleksandr Tymoshenko int
748d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
74992bf0e27SAlexander Motin {
750d6b3aaf8SOleksandr Tymoshenko 	sdhci_reset(slot, SDHCI_RESET_ALL);
75192bf0e27SAlexander Motin 
75292bf0e27SAlexander Motin 	return (0);
75392bf0e27SAlexander Motin }
75492bf0e27SAlexander Motin 
755d6b3aaf8SOleksandr Tymoshenko int
756d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
75792bf0e27SAlexander Motin {
758d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
75992bf0e27SAlexander Motin 
760d6b3aaf8SOleksandr Tymoshenko 	return (0);
76192bf0e27SAlexander Motin }
76292bf0e27SAlexander Motin 
76357677a3aSOleksandr Tymoshenko uint32_t
76457677a3aSOleksandr Tymoshenko sdhci_generic_min_freq(device_t brdev, struct sdhci_slot *slot)
76557677a3aSOleksandr Tymoshenko {
76657677a3aSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
76757677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
76857677a3aSOleksandr Tymoshenko 	else
76957677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
77057677a3aSOleksandr Tymoshenko }
77157677a3aSOleksandr Tymoshenko 
7726e37fb2bSIan Lepore bool
7736e37fb2bSIan Lepore sdhci_generic_get_card_present(device_t brdev, struct sdhci_slot *slot)
7746e37fb2bSIan Lepore {
7756e37fb2bSIan Lepore 
776*639f59f0SIan Lepore 	if (slot->opt & SDHCI_NON_REMOVABLE)
777*639f59f0SIan Lepore 		return true;
778*639f59f0SIan Lepore 
7796e37fb2bSIan Lepore 	return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
7806e37fb2bSIan Lepore }
7816e37fb2bSIan Lepore 
782d6b3aaf8SOleksandr Tymoshenko int
783d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
784831f5dcfSAlexander Motin {
785831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
786831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
787831f5dcfSAlexander Motin 
788831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
789831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
790831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
791831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
792831f5dcfSAlexander Motin 		sdhci_init(slot);
793831f5dcfSAlexander Motin 	}
794831f5dcfSAlexander Motin 	/* Configure the bus. */
795831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
796831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
7972d1731b8SIan Lepore 	if (ios->bus_width == bus_width_8) {
7982d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
799831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
8002d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_4) {
8012d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
8022d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
8032d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_1) {
8042d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
8052d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
8062d1731b8SIan Lepore 	} else {
8072d1731b8SIan Lepore 		panic("Invalid bus width: %d", ios->bus_width);
8082d1731b8SIan Lepore 	}
809bba987dcSIan Lepore 	if (ios->timing == bus_timing_hs &&
810bba987dcSIan Lepore 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
811831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
812831f5dcfSAlexander Motin 	else
813831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
814831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
815831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
816d6b3aaf8SOleksandr Tymoshenko 	if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
817831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
818831f5dcfSAlexander Motin 
819831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
820831f5dcfSAlexander Motin 	return (0);
821831f5dcfSAlexander Motin }
822831f5dcfSAlexander Motin 
823831f5dcfSAlexander Motin static void
824e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot)
825e64f01a9SIan Lepore {
826e64f01a9SIan Lepore 	struct mmc_request *req;
827e64f01a9SIan Lepore 
828e64f01a9SIan Lepore 	if (slot->req != NULL && slot->curcmd != NULL) {
829e64f01a9SIan Lepore 		callout_stop(&slot->timeout_callout);
830e64f01a9SIan Lepore 		req = slot->req;
831e64f01a9SIan Lepore 		slot->req = NULL;
832e64f01a9SIan Lepore 		slot->curcmd = NULL;
833e64f01a9SIan Lepore 		req->done(req);
834e64f01a9SIan Lepore 	}
835e64f01a9SIan Lepore }
836e64f01a9SIan Lepore 
837e64f01a9SIan Lepore static void
838e64f01a9SIan Lepore sdhci_timeout(void *arg)
839e64f01a9SIan Lepore {
840e64f01a9SIan Lepore 	struct sdhci_slot *slot = arg;
841e64f01a9SIan Lepore 
842e64f01a9SIan Lepore 	if (slot->curcmd != NULL) {
8437e586643SIan Lepore 		slot_printf(slot, " Controller timeout\n");
8447e586643SIan Lepore 		sdhci_dumpregs(slot);
845a6873fd1SIan Lepore 		sdhci_reset(slot, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
846e64f01a9SIan Lepore 		slot->curcmd->error = MMC_ERR_TIMEOUT;
847e64f01a9SIan Lepore 		sdhci_req_done(slot);
8487e586643SIan Lepore 	} else {
8497e586643SIan Lepore 		slot_printf(slot, " Spurious timeout - no active command\n");
850e64f01a9SIan Lepore 	}
851e64f01a9SIan Lepore }
852e64f01a9SIan Lepore 
853e64f01a9SIan Lepore static void
854831f5dcfSAlexander Motin sdhci_set_transfer_mode(struct sdhci_slot *slot,
855831f5dcfSAlexander Motin 	struct mmc_data *data)
856831f5dcfSAlexander Motin {
857831f5dcfSAlexander Motin 	uint16_t mode;
858831f5dcfSAlexander Motin 
859831f5dcfSAlexander Motin 	if (data == NULL)
860831f5dcfSAlexander Motin 		return;
861831f5dcfSAlexander Motin 
862831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
863831f5dcfSAlexander Motin 	if (data->len > 512)
864831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
865831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
866831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
867831f5dcfSAlexander Motin 	if (slot->req->stop)
868831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_ACMD12;
869831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
870831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
871831f5dcfSAlexander Motin 
872831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
873831f5dcfSAlexander Motin }
874831f5dcfSAlexander Motin 
875831f5dcfSAlexander Motin static void
876831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
877831f5dcfSAlexander Motin {
878831f5dcfSAlexander Motin 	int flags, timeout;
87990993663SIan Lepore 	uint32_t mask;
880831f5dcfSAlexander Motin 
881831f5dcfSAlexander Motin 	slot->curcmd = cmd;
882831f5dcfSAlexander Motin 	slot->cmd_done = 0;
883831f5dcfSAlexander Motin 
884831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
885831f5dcfSAlexander Motin 
886831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
887831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
888831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
889831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
890e64f01a9SIan Lepore 		sdhci_req_done(slot);
891831f5dcfSAlexander Motin 		return;
892831f5dcfSAlexander Motin 	}
893831f5dcfSAlexander Motin 
894d8208d9eSAlexander Motin 	/* Do not issue command if there is no card, clock or power.
895d8208d9eSAlexander Motin 	 * Controller will not detect timeout without clock active. */
8966e37fb2bSIan Lepore 	if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
897d8208d9eSAlexander Motin 	    slot->power == 0 ||
898d8208d9eSAlexander Motin 	    slot->clock == 0) {
899831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
900e64f01a9SIan Lepore 		sdhci_req_done(slot);
901831f5dcfSAlexander Motin 		return;
902831f5dcfSAlexander Motin 	}
903831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
904831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
905831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
906831f5dcfSAlexander Motin 	if (cmd->data || (cmd->flags & MMC_RSP_BUSY))
907831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
908831f5dcfSAlexander Motin 	/* We shouldn't wait for DAT for stop commands. */
909831f5dcfSAlexander Motin 	if (cmd == slot->req->stop)
910831f5dcfSAlexander Motin 		mask &= ~SDHCI_DAT_INHIBIT;
9118775ab45SIan Lepore 	/*
9128775ab45SIan Lepore 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
9138775ab45SIan Lepore 	 *  here at all, but when writing a crash dump we may be bypassing the
9148775ab45SIan Lepore 	 *  host platform's interrupt handler, and in some cases that handler
9158775ab45SIan Lepore 	 *  may be working around hardware quirks such as not respecting r1b
9168775ab45SIan Lepore 	 *  busy indications.  In those cases, this wait-loop serves the purpose
9178775ab45SIan Lepore 	 *  of waiting for the prior command and data transfers to be done, and
9188775ab45SIan Lepore 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
9198775ab45SIan Lepore 	 *  (It's usually more like 20-30ms in the real world.)
9208775ab45SIan Lepore 	 */
9218775ab45SIan Lepore 	timeout = 250;
92290993663SIan Lepore 	while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
923831f5dcfSAlexander Motin 		if (timeout == 0) {
924831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
925831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
926831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
927831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
928e64f01a9SIan Lepore 			sdhci_req_done(slot);
929831f5dcfSAlexander Motin 			return;
930831f5dcfSAlexander Motin 		}
931831f5dcfSAlexander Motin 		timeout--;
932831f5dcfSAlexander Motin 		DELAY(1000);
933831f5dcfSAlexander Motin 	}
934831f5dcfSAlexander Motin 
935831f5dcfSAlexander Motin 	/* Prepare command flags. */
936831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
937831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
938831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
939831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
940831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
941831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
942831f5dcfSAlexander Motin 	else
943831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
944831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
945831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
946831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
947831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
948831f5dcfSAlexander Motin 	if (cmd->data)
949831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
950831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
951831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
952831f5dcfSAlexander Motin 	/* Prepare data. */
953831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
954831f5dcfSAlexander Motin 	/*
955831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
956831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
957831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
958831f5dcfSAlexander Motin 	 */
959831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
960831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
961831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
962831f5dcfSAlexander Motin 	}
963831f5dcfSAlexander Motin 	/* Set command argument. */
964831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
965831f5dcfSAlexander Motin 	/* Set data transfer mode. */
966831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
967831f5dcfSAlexander Motin 	/* Start command. */
968d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
969a6873fd1SIan Lepore 	/* Start timeout callout. */
970ba6fc1c7SLuiz Otavio O Souza 	callout_reset(&slot->timeout_callout, slot->timeout * hz,
971ba6fc1c7SLuiz Otavio O Souza 	    sdhci_timeout, slot);
972831f5dcfSAlexander Motin }
973831f5dcfSAlexander Motin 
974831f5dcfSAlexander Motin static void
975831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
976831f5dcfSAlexander Motin {
977831f5dcfSAlexander Motin 	int i;
978831f5dcfSAlexander Motin 
979831f5dcfSAlexander Motin 	slot->cmd_done = 1;
980831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
981831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
982831f5dcfSAlexander Motin 	 * happened first. */
983831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE);
984831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
985831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
986831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
987831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
988831f5dcfSAlexander Motin 		sdhci_start(slot);
989831f5dcfSAlexander Motin 		return;
990831f5dcfSAlexander Motin 	}
991831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
992831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
993831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
994831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
995831f5dcfSAlexander Motin 			uint8_t extra = 0;
996831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
997831f5dcfSAlexander Motin 				uint32_t val = RD4(slot, SDHCI_RESPONSE + i * 4);
998677ee494SIan Lepore 				if (slot->quirks & SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
999677ee494SIan Lepore 					slot->curcmd->resp[3 - i] = val;
1000677ee494SIan Lepore 				else {
1001677ee494SIan Lepore 					slot->curcmd->resp[3 - i] =
1002677ee494SIan Lepore 					    (val << 8) | extra;
1003831f5dcfSAlexander Motin 					extra = val >> 24;
1004831f5dcfSAlexander Motin 				}
1005677ee494SIan Lepore 			}
1006831f5dcfSAlexander Motin 		} else
1007831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1008831f5dcfSAlexander Motin 	}
1009831f5dcfSAlexander Motin 	/* If data ready - finish. */
1010831f5dcfSAlexander Motin 	if (slot->data_done)
1011831f5dcfSAlexander Motin 		sdhci_start(slot);
1012831f5dcfSAlexander Motin }
1013831f5dcfSAlexander Motin 
1014831f5dcfSAlexander Motin static void
1015831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data)
1016831f5dcfSAlexander Motin {
1017831f5dcfSAlexander Motin 	uint32_t target_timeout, current_timeout;
1018831f5dcfSAlexander Motin 	uint8_t div;
1019831f5dcfSAlexander Motin 
1020831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1021831f5dcfSAlexander Motin 		slot->data_done = 1;
1022831f5dcfSAlexander Motin 		return;
1023831f5dcfSAlexander Motin 	}
1024831f5dcfSAlexander Motin 
1025831f5dcfSAlexander Motin 	slot->data_done = 0;
1026831f5dcfSAlexander Motin 
1027831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
1028831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
1029ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1030ceb9e9f7SIan Lepore 		div = 0xE;
1031ceb9e9f7SIan Lepore 	} else {
1032831f5dcfSAlexander Motin 		target_timeout = 1000000;
1033831f5dcfSAlexander Motin 		div = 0;
1034831f5dcfSAlexander Motin 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1035ceb9e9f7SIan Lepore 		while (current_timeout < target_timeout && div < 0xE) {
1036ceb9e9f7SIan Lepore 			++div;
1037831f5dcfSAlexander Motin 			current_timeout <<= 1;
1038831f5dcfSAlexander Motin 		}
1039831f5dcfSAlexander Motin 		/* Compensate for an off-by-one error in the CaFe chip.*/
1040ceb9e9f7SIan Lepore 		if (div < 0xE &&
1041ceb9e9f7SIan Lepore 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1042ceb9e9f7SIan Lepore 			++div;
1043831f5dcfSAlexander Motin 		}
1044ceb9e9f7SIan Lepore 	}
1045831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1046831f5dcfSAlexander Motin 
1047831f5dcfSAlexander Motin 	if (data == NULL)
1048831f5dcfSAlexander Motin 		return;
1049831f5dcfSAlexander Motin 
1050831f5dcfSAlexander Motin 	/* Use DMA if possible. */
1051831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
1052831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
1053831f5dcfSAlexander Motin 	/* If data is small, broken DMA may return zeroes instead of data, */
1054d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1055831f5dcfSAlexander Motin 	    (data->len <= 512))
1056831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1057831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
1058d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1059831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
1060831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1061831f5dcfSAlexander Motin 	/* Load DMA buffer. */
1062831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
1063831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
1064ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1065ecc2d997SRui Paulo 			    BUS_DMASYNC_PREREAD);
1066831f5dcfSAlexander Motin 		else {
1067831f5dcfSAlexander Motin 			memcpy(slot->dmamem, data->data,
1068ecc2d997SRui Paulo 			    (data->len < DMA_BLOCK_SIZE) ?
1069ecc2d997SRui Paulo 			    data->len : DMA_BLOCK_SIZE);
1070ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1071ecc2d997SRui Paulo 			    BUS_DMASYNC_PREWRITE);
1072831f5dcfSAlexander Motin 		}
1073831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1074831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1075831f5dcfSAlexander Motin 		 * for the last page and unmask else. */
1076831f5dcfSAlexander Motin 		if (data->len == DMA_BLOCK_SIZE)
1077831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1078831f5dcfSAlexander Motin 		else
1079831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
1080831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1081831f5dcfSAlexander Motin 	}
1082831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
1083831f5dcfSAlexander Motin 	slot->offset = 0;
1084831f5dcfSAlexander Motin 	/* Set block size and request IRQ on 4K border. */
1085831f5dcfSAlexander Motin 	WR2(slot, SDHCI_BLOCK_SIZE,
1086831f5dcfSAlexander Motin 	    SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512)?data->len:512));
1087831f5dcfSAlexander Motin 	/* Set block count. */
1088831f5dcfSAlexander Motin 	WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
1089831f5dcfSAlexander Motin }
1090831f5dcfSAlexander Motin 
1091c3a0f75aSOleksandr Tymoshenko void
1092831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
1093831f5dcfSAlexander Motin {
1094831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
1095831f5dcfSAlexander Motin 
1096831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
1097ecc2d997SRui Paulo 	 * Auxiliary restore point for the case when data interrupt
1098831f5dcfSAlexander Motin 	 * happened first. */
1099831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
1100831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1101831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
1102831f5dcfSAlexander Motin 	}
1103831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
1104a98788edSIan Lepore 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA)) {
1105831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
1106831f5dcfSAlexander Motin 			size_t left = data->len - slot->offset;
1107ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1108ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTREAD);
1109831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1110831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1111831f5dcfSAlexander Motin 		} else
1112ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1113ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTWRITE);
1114831f5dcfSAlexander Motin 	}
1115a98788edSIan Lepore 	slot->data_done = 1;
1116831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
1117831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1118831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1119831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1120831f5dcfSAlexander Motin 		sdhci_start(slot);
1121831f5dcfSAlexander Motin 		return;
1122831f5dcfSAlexander Motin 	}
1123831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
1124831f5dcfSAlexander Motin 	if (slot->cmd_done)
1125831f5dcfSAlexander Motin 		sdhci_start(slot);
1126831f5dcfSAlexander Motin }
1127831f5dcfSAlexander Motin 
1128831f5dcfSAlexander Motin static void
1129831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
1130831f5dcfSAlexander Motin {
1131831f5dcfSAlexander Motin 	struct mmc_request *req;
1132831f5dcfSAlexander Motin 
1133831f5dcfSAlexander Motin 	req = slot->req;
1134831f5dcfSAlexander Motin 	if (req == NULL)
1135831f5dcfSAlexander Motin 		return;
1136831f5dcfSAlexander Motin 
1137831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
1138831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
1139831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
1140831f5dcfSAlexander Motin 		return;
1141831f5dcfSAlexander Motin 	}
1142831f5dcfSAlexander Motin /* 	We don't need this until using Auto-CMD12 feature
1143831f5dcfSAlexander Motin 	if (!(slot->flags & STOP_STARTED) && req->stop) {
1144831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
1145831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
1146831f5dcfSAlexander Motin 		return;
1147831f5dcfSAlexander Motin 	}
1148831f5dcfSAlexander Motin */
11495b69a497SAlexander Motin 	if (sdhci_debug > 1)
11505b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
11515b69a497SAlexander Motin 	if (!req->cmd->error &&
1152d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
1153831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1154831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1155831f5dcfSAlexander Motin 	}
1156831f5dcfSAlexander Motin 
1157e64f01a9SIan Lepore 	sdhci_req_done(slot);
1158831f5dcfSAlexander Motin }
1159831f5dcfSAlexander Motin 
1160d6b3aaf8SOleksandr Tymoshenko int
1161d6b3aaf8SOleksandr Tymoshenko sdhci_generic_request(device_t brdev, device_t reqdev, struct mmc_request *req)
1162831f5dcfSAlexander Motin {
1163831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1164831f5dcfSAlexander Motin 
1165831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1166831f5dcfSAlexander Motin 	if (slot->req != NULL) {
1167831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1168831f5dcfSAlexander Motin 		return (EBUSY);
1169831f5dcfSAlexander Motin 	}
11705b69a497SAlexander Motin 	if (sdhci_debug > 1) {
11715b69a497SAlexander Motin 		slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1172831f5dcfSAlexander Motin     		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
11735b69a497SAlexander Motin     		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
11745b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
11755b69a497SAlexander Motin 	}
1176831f5dcfSAlexander Motin 	slot->req = req;
1177831f5dcfSAlexander Motin 	slot->flags = 0;
1178831f5dcfSAlexander Motin 	sdhci_start(slot);
1179831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1180bea2dca2SAlexander Motin 	if (dumping) {
1181bea2dca2SAlexander Motin 		while (slot->req != NULL) {
1182d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
1183bea2dca2SAlexander Motin 			DELAY(10);
1184bea2dca2SAlexander Motin 		}
1185bea2dca2SAlexander Motin 	}
1186831f5dcfSAlexander Motin 	return (0);
1187831f5dcfSAlexander Motin }
1188831f5dcfSAlexander Motin 
1189d6b3aaf8SOleksandr Tymoshenko int
1190d6b3aaf8SOleksandr Tymoshenko sdhci_generic_get_ro(device_t brdev, device_t reqdev)
1191831f5dcfSAlexander Motin {
1192831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1193831f5dcfSAlexander Motin 	uint32_t val;
1194831f5dcfSAlexander Motin 
1195831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1196831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
1197831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1198831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
1199831f5dcfSAlexander Motin }
1200831f5dcfSAlexander Motin 
1201d6b3aaf8SOleksandr Tymoshenko int
1202d6b3aaf8SOleksandr Tymoshenko sdhci_generic_acquire_host(device_t brdev, device_t reqdev)
1203831f5dcfSAlexander Motin {
1204831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1205831f5dcfSAlexander Motin 	int err = 0;
1206831f5dcfSAlexander Motin 
1207831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1208831f5dcfSAlexander Motin 	while (slot->bus_busy)
1209d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
1210831f5dcfSAlexander Motin 	slot->bus_busy++;
1211831f5dcfSAlexander Motin 	/* Activate led. */
1212831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
1213831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1214831f5dcfSAlexander Motin 	return (err);
1215831f5dcfSAlexander Motin }
1216831f5dcfSAlexander Motin 
1217d6b3aaf8SOleksandr Tymoshenko int
1218d6b3aaf8SOleksandr Tymoshenko sdhci_generic_release_host(device_t brdev, device_t reqdev)
1219831f5dcfSAlexander Motin {
1220831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1221831f5dcfSAlexander Motin 
1222831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1223831f5dcfSAlexander Motin 	/* Deactivate led. */
1224831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
1225831f5dcfSAlexander Motin 	slot->bus_busy--;
1226831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1227d493985aSAlexander Motin 	wakeup(slot);
1228831f5dcfSAlexander Motin 	return (0);
1229831f5dcfSAlexander Motin }
1230831f5dcfSAlexander Motin 
1231831f5dcfSAlexander Motin static void
1232831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
1233831f5dcfSAlexander Motin {
1234831f5dcfSAlexander Motin 
1235831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1236831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
1237831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1238831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1239831f5dcfSAlexander Motin 		return;
1240831f5dcfSAlexander Motin 	}
1241831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
1242831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1243831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
1244831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1245831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
1246831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
1247831f5dcfSAlexander Motin 
1248831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
1249831f5dcfSAlexander Motin }
1250831f5dcfSAlexander Motin 
1251831f5dcfSAlexander Motin static void
1252831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
1253831f5dcfSAlexander Motin {
1254831f5dcfSAlexander Motin 
1255831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1256831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1257831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1258831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1259831f5dcfSAlexander Motin 		return;
1260831f5dcfSAlexander Motin 	}
1261831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
1262831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1263831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1264831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
1265831f5dcfSAlexander Motin 		    intmask);
1266831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1267831f5dcfSAlexander Motin 		return;
1268831f5dcfSAlexander Motin 	}
1269831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
1270831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1271acbaa69fSOleksandr Tymoshenko 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1272831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1273831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
1274831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
1275831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
1276831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1277831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
1278831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1279831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
1280831f5dcfSAlexander Motin 	}
1281831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1282831f5dcfSAlexander Motin 		/* No need to continue after any error. */
1283a98788edSIan Lepore 		goto done;
1284831f5dcfSAlexander Motin 	}
1285831f5dcfSAlexander Motin 
1286831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
1287c3a0f75aSOleksandr Tymoshenko 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
1288c3a0f75aSOleksandr Tymoshenko 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
1289c3a0f75aSOleksandr Tymoshenko 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
1290c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, &intmask);
1291c3a0f75aSOleksandr Tymoshenko 			slot->flags |= PLATFORM_DATA_STARTED;
1292c3a0f75aSOleksandr Tymoshenko 		} else
1293831f5dcfSAlexander Motin 			sdhci_transfer_pio(slot);
1294c3a0f75aSOleksandr Tymoshenko 	}
1295831f5dcfSAlexander Motin 	/* Handle DMA border. */
1296831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
1297831f5dcfSAlexander Motin 		struct mmc_data *data = slot->curcmd->data;
1298831f5dcfSAlexander Motin 		size_t left;
1299831f5dcfSAlexander Motin 
1300831f5dcfSAlexander Motin 		/* Unload DMA buffer... */
1301831f5dcfSAlexander Motin 		left = data->len - slot->offset;
1302831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
1303831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1304831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
1305831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1306831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1307831f5dcfSAlexander Motin 		} else {
1308831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1309831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
1310831f5dcfSAlexander Motin 		}
1311831f5dcfSAlexander Motin 		/* ... and reload it again. */
1312831f5dcfSAlexander Motin 		slot->offset += DMA_BLOCK_SIZE;
1313831f5dcfSAlexander Motin 		left = data->len - slot->offset;
1314831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
1315831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1316831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREREAD);
1317831f5dcfSAlexander Motin 		} else {
1318831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
1319831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1320831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1321831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
1322831f5dcfSAlexander Motin 		}
1323831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1324831f5dcfSAlexander Motin 		 * for the last page. */
1325831f5dcfSAlexander Motin 		if (left == DMA_BLOCK_SIZE) {
1326831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1327831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1328831f5dcfSAlexander Motin 		}
1329831f5dcfSAlexander Motin 		/* Restart DMA. */
1330831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1331831f5dcfSAlexander Motin 	}
1332831f5dcfSAlexander Motin 	/* We have got all data. */
1333c3a0f75aSOleksandr Tymoshenko 	if (intmask & SDHCI_INT_DATA_END) {
1334c3a0f75aSOleksandr Tymoshenko 		if (slot->flags & PLATFORM_DATA_STARTED) {
1335c3a0f75aSOleksandr Tymoshenko 			slot->flags &= ~PLATFORM_DATA_STARTED;
1336c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1337c3a0f75aSOleksandr Tymoshenko 		} else
1338831f5dcfSAlexander Motin 			sdhci_finish_data(slot);
1339831f5dcfSAlexander Motin 	}
1340a98788edSIan Lepore done:
1341a98788edSIan Lepore 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
1342a98788edSIan Lepore 		if (slot->flags & PLATFORM_DATA_STARTED) {
1343a98788edSIan Lepore 			slot->flags &= ~PLATFORM_DATA_STARTED;
1344a98788edSIan Lepore 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1345a98788edSIan Lepore 		} else
1346a98788edSIan Lepore 			sdhci_finish_data(slot);
1347a98788edSIan Lepore 		return;
1348a98788edSIan Lepore 	}
1349c3a0f75aSOleksandr Tymoshenko }
1350831f5dcfSAlexander Motin 
1351831f5dcfSAlexander Motin static void
1352831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot)
1353831f5dcfSAlexander Motin {
1354831f5dcfSAlexander Motin 	uint16_t err;
1355831f5dcfSAlexander Motin 
1356831f5dcfSAlexander Motin 	err = RD4(slot, SDHCI_ACMD12_ERR);
1357831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1358831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
1359831f5dcfSAlexander Motin 		    "there is no active command.\n", err);
1360831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1361831f5dcfSAlexander Motin 		return;
1362831f5dcfSAlexander Motin 	}
1363831f5dcfSAlexander Motin 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err);
1364831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_CMD);
1365831f5dcfSAlexander Motin }
1366831f5dcfSAlexander Motin 
1367d6b3aaf8SOleksandr Tymoshenko void
1368d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
1369831f5dcfSAlexander Motin {
13702b96b955SJustin Hibbits 	uint32_t intmask, present;
1371831f5dcfSAlexander Motin 
1372831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1373831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
1374831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
1375831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
1376831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1377d6b3aaf8SOleksandr Tymoshenko 		return;
1378831f5dcfSAlexander Motin 	}
13795b69a497SAlexander Motin 	if (sdhci_debug > 2)
13805b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
13815b69a497SAlexander Motin 
1382831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
1383831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1384*639f59f0SIan Lepore 		present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
13852b96b955SJustin Hibbits 		slot->intmask &=
13862b96b955SJustin Hibbits 		    ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
13872b96b955SJustin Hibbits 		slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
13882b96b955SJustin Hibbits 		    SDHCI_INT_CARD_INSERT;
13892b96b955SJustin Hibbits 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
13902b96b955SJustin Hibbits 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1391831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
1392831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
1393*639f59f0SIan Lepore 		sdhci_handle_card_present(slot, present);
1394831f5dcfSAlexander Motin 		intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1395831f5dcfSAlexander Motin 	}
1396831f5dcfSAlexander Motin 	/* Handle command interrupts. */
1397831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
1398831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
1399831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
1400831f5dcfSAlexander Motin 	}
1401831f5dcfSAlexander Motin 	/* Handle data interrupts. */
1402831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
1403831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
14047e586643SIan Lepore 		/* Dont call data_irq in case of errored command */
14057e586643SIan Lepore 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
1406831f5dcfSAlexander Motin 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
1407831f5dcfSAlexander Motin 	}
1408831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
1409831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
1410831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
1411831f5dcfSAlexander Motin 		sdhci_acmd_irq(slot);
1412831f5dcfSAlexander Motin 	}
1413831f5dcfSAlexander Motin 	intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1414831f5dcfSAlexander Motin 	intmask &= ~SDHCI_INT_ACMD12ERR;
1415831f5dcfSAlexander Motin 	intmask &= ~SDHCI_INT_ERROR;
1416831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
1417831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
1418831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
1419831f5dcfSAlexander Motin 		slot_printf(slot,
1420831f5dcfSAlexander Motin 		    "Card is consuming too much power!\n");
1421831f5dcfSAlexander Motin 		intmask &= ~SDHCI_INT_BUS_POWER;
1422831f5dcfSAlexander Motin 	}
1423831f5dcfSAlexander Motin 	/* The rest is unknown. */
1424831f5dcfSAlexander Motin 	if (intmask) {
1425831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
1426831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
1427831f5dcfSAlexander Motin 		    intmask);
1428831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1429831f5dcfSAlexander Motin 	}
1430831f5dcfSAlexander Motin 
1431831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1432831f5dcfSAlexander Motin }
1433831f5dcfSAlexander Motin 
1434d6b3aaf8SOleksandr Tymoshenko int
1435d6b3aaf8SOleksandr Tymoshenko sdhci_generic_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1436831f5dcfSAlexander Motin {
1437831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
1438831f5dcfSAlexander Motin 
1439831f5dcfSAlexander Motin 	switch (which) {
1440831f5dcfSAlexander Motin 	default:
1441831f5dcfSAlexander Motin 		return (EINVAL);
1442831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
1443bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
1444831f5dcfSAlexander Motin 		break;
1445831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
1446bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
1447831f5dcfSAlexander Motin 		break;
1448831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
1449bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
1450831f5dcfSAlexander Motin 		break;
1451831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
1452bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
1453831f5dcfSAlexander Motin 		break;
1454831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
1455bcd91d25SJayachandran C. 		*result = slot->host.f_min;
1456831f5dcfSAlexander Motin 		break;
1457831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
1458bcd91d25SJayachandran C. 		*result = slot->host.f_max;
1459831f5dcfSAlexander Motin 		break;
1460831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
1461bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
1462831f5dcfSAlexander Motin 		break;
1463831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
1464bcd91d25SJayachandran C. 		*result = slot->host.mode;
1465831f5dcfSAlexander Motin 		break;
1466831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
1467bcd91d25SJayachandran C. 		*result = slot->host.ocr;
1468831f5dcfSAlexander Motin 		break;
1469831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
1470bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
1471831f5dcfSAlexander Motin 		break;
1472831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
1473bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
1474831f5dcfSAlexander Motin 		break;
1475831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
1476bcd91d25SJayachandran C. 		*result = slot->host.caps;
1477831f5dcfSAlexander Motin 		break;
1478831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
1479bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
1480831f5dcfSAlexander Motin 		break;
14813a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
1482bcd91d25SJayachandran C. 		*result = 65535;
14833a4a2557SAlexander Motin 		break;
1484831f5dcfSAlexander Motin 	}
1485831f5dcfSAlexander Motin 	return (0);
1486831f5dcfSAlexander Motin }
1487831f5dcfSAlexander Motin 
1488d6b3aaf8SOleksandr Tymoshenko int
1489d6b3aaf8SOleksandr Tymoshenko sdhci_generic_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1490831f5dcfSAlexander Motin {
1491831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
1492831f5dcfSAlexander Motin 
1493831f5dcfSAlexander Motin 	switch (which) {
1494831f5dcfSAlexander Motin 	default:
1495831f5dcfSAlexander Motin 		return (EINVAL);
1496831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
1497831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
1498831f5dcfSAlexander Motin 		break;
1499831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
1500831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
1501831f5dcfSAlexander Motin 		break;
1502831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
1503831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
1504831f5dcfSAlexander Motin 		break;
1505831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
1506831f5dcfSAlexander Motin 		if (value > 0) {
150757677a3aSOleksandr Tymoshenko 			uint32_t max_clock;
150857677a3aSOleksandr Tymoshenko 			uint32_t clock;
1509831f5dcfSAlexander Motin 			int i;
1510831f5dcfSAlexander Motin 
151157677a3aSOleksandr Tymoshenko 			max_clock = slot->max_clk;
151257677a3aSOleksandr Tymoshenko 			clock = max_clock;
151357677a3aSOleksandr Tymoshenko 
151457677a3aSOleksandr Tymoshenko 			if (slot->version < SDHCI_SPEC_300) {
151557677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
151657677a3aSOleksandr Tymoshenko 				    i <<= 1) {
1517831f5dcfSAlexander Motin 					if (clock <= value)
1518831f5dcfSAlexander Motin 						break;
1519831f5dcfSAlexander Motin 					clock >>= 1;
1520831f5dcfSAlexander Motin 				}
152157677a3aSOleksandr Tymoshenko 			}
152257677a3aSOleksandr Tymoshenko 			else {
152357677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
152457677a3aSOleksandr Tymoshenko 				    i += 2) {
152557677a3aSOleksandr Tymoshenko 					if (clock <= value)
152657677a3aSOleksandr Tymoshenko 						break;
152757677a3aSOleksandr Tymoshenko 					clock = max_clock / (i + 2);
152857677a3aSOleksandr Tymoshenko 				}
152957677a3aSOleksandr Tymoshenko 			}
153057677a3aSOleksandr Tymoshenko 
1531831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
1532831f5dcfSAlexander Motin 		} else
1533831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
1534831f5dcfSAlexander Motin 		break;
1535831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
1536831f5dcfSAlexander Motin 		slot->host.mode = value;
1537831f5dcfSAlexander Motin 		break;
1538831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
1539831f5dcfSAlexander Motin 		slot->host.ocr = value;
1540831f5dcfSAlexander Motin 		break;
1541831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
1542831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
1543831f5dcfSAlexander Motin 		break;
1544831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
1545831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
1546831f5dcfSAlexander Motin 		break;
1547831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
1548831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
1549831f5dcfSAlexander Motin 		break;
1550831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
1551831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
1552831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
1553831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
15543a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
1555831f5dcfSAlexander Motin 		return (EINVAL);
1556831f5dcfSAlexander Motin 	}
1557831f5dcfSAlexander Motin 	return (0);
1558831f5dcfSAlexander Motin }
1559831f5dcfSAlexander Motin 
1560d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1);
1561