xref: /freebsd/sys/dev/sdhci/sdhci.c (revision 33aad34de61a0f6bf3fd1379e7d6f312f979121a)
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>
32831f5dcfSAlexander Motin #include <sys/conf.h>
33831f5dcfSAlexander Motin #include <sys/kernel.h>
34831f5dcfSAlexander Motin #include <sys/lock.h>
35831f5dcfSAlexander Motin #include <sys/module.h>
36831f5dcfSAlexander Motin #include <sys/mutex.h>
37831f5dcfSAlexander Motin #include <sys/resource.h>
38831f5dcfSAlexander Motin #include <sys/rman.h>
395b69a497SAlexander Motin #include <sys/sysctl.h>
40831f5dcfSAlexander Motin #include <sys/taskqueue.h>
41831f5dcfSAlexander Motin 
42831f5dcfSAlexander Motin #include <machine/bus.h>
43831f5dcfSAlexander Motin #include <machine/resource.h>
44831f5dcfSAlexander Motin #include <machine/stdarg.h>
45831f5dcfSAlexander Motin 
46831f5dcfSAlexander Motin #include <dev/mmc/bridge.h>
47831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h>
48831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h>
49831f5dcfSAlexander Motin 
50831f5dcfSAlexander Motin #include "mmcbr_if.h"
51831f5dcfSAlexander Motin #include "sdhci.h"
52d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h"
53831f5dcfSAlexander Motin 
54831f5dcfSAlexander Motin struct sdhci_softc;
55831f5dcfSAlexander Motin 
56831f5dcfSAlexander Motin struct sdhci_softc {
57831f5dcfSAlexander Motin 	device_t	dev;		/* Controller device */
58831f5dcfSAlexander Motin 	struct resource *irq_res;	/* IRQ resource */
59831f5dcfSAlexander Motin 	int 		irq_rid;
60831f5dcfSAlexander Motin 	void 		*intrhand;	/* Interrupt handle */
61831f5dcfSAlexander Motin 
62831f5dcfSAlexander Motin 	int		num_slots;	/* Number of slots on this controller */
63831f5dcfSAlexander Motin 	struct sdhci_slot slots[6];
64831f5dcfSAlexander Motin };
65831f5dcfSAlexander Motin 
666472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver");
675b69a497SAlexander Motin 
68d6b3aaf8SOleksandr Tymoshenko int	sdhci_debug = 0;
695b69a497SAlexander Motin TUNABLE_INT("hw.sdhci.debug", &sdhci_debug);
705b69a497SAlexander Motin SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RW, &sdhci_debug, 0, "Debug level");
715b69a497SAlexander Motin 
72d6b3aaf8SOleksandr Tymoshenko #define RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
73d6b3aaf8SOleksandr Tymoshenko #define RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
74d6b3aaf8SOleksandr Tymoshenko #define RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
75d6b3aaf8SOleksandr Tymoshenko #define RD_MULTI_4(slot, off, ptr, count)	\
76d6b3aaf8SOleksandr Tymoshenko     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
77831f5dcfSAlexander Motin 
78d6b3aaf8SOleksandr Tymoshenko #define WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
79d6b3aaf8SOleksandr Tymoshenko #define WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
80d6b3aaf8SOleksandr Tymoshenko #define WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
81d6b3aaf8SOleksandr Tymoshenko #define WR_MULTI_4(slot, off, ptr, count)	\
82d6b3aaf8SOleksandr Tymoshenko     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
83831f5dcfSAlexander Motin 
84831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
85831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot);
86831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data);
87831f5dcfSAlexander Motin 
88831f5dcfSAlexander Motin static void sdhci_card_task(void *, int);
89831f5dcfSAlexander Motin 
90831f5dcfSAlexander Motin /* helper routines */
91831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot)		mtx_lock(&(_slot)->mtx)
92831f5dcfSAlexander Motin #define	SDHCI_UNLOCK(_slot)		mtx_unlock(&(_slot)->mtx)
93831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \
94831f5dcfSAlexander Motin 	mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
95831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot)	mtx_destroy(&_slot->mtx);
96831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot)	mtx_assert(&_slot->mtx, MA_OWNED);
97831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot)	mtx_assert(&_slot->mtx, MA_NOTOWNED);
98831f5dcfSAlexander Motin 
99*33aad34dSOleksandr Tymoshenko #define	SDHCI_DEFAULT_MAX_FREQ	50
100*33aad34dSOleksandr Tymoshenko 
101831f5dcfSAlexander Motin static void
102831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
103831f5dcfSAlexander Motin {
104831f5dcfSAlexander Motin 	if (error != 0) {
105831f5dcfSAlexander Motin 		printf("getaddr: error %d\n", error);
106831f5dcfSAlexander Motin 		return;
107831f5dcfSAlexander Motin 	}
108831f5dcfSAlexander Motin 	*(bus_addr_t *)arg = segs[0].ds_addr;
109831f5dcfSAlexander Motin }
110831f5dcfSAlexander Motin 
111d6b3aaf8SOleksandr Tymoshenko static int
112d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
113d6b3aaf8SOleksandr Tymoshenko {
114d6b3aaf8SOleksandr Tymoshenko 	va_list ap;
115d6b3aaf8SOleksandr Tymoshenko 	int retval;
116d6b3aaf8SOleksandr Tymoshenko 
117d6b3aaf8SOleksandr Tymoshenko     	retval = printf("%s-slot%d: ",
118d6b3aaf8SOleksandr Tymoshenko 	    device_get_nameunit(slot->bus), slot->num);
119d6b3aaf8SOleksandr Tymoshenko 
120d6b3aaf8SOleksandr Tymoshenko 	va_start(ap, fmt);
121d6b3aaf8SOleksandr Tymoshenko 	retval += vprintf(fmt, ap);
122d6b3aaf8SOleksandr Tymoshenko 	va_end(ap);
123d6b3aaf8SOleksandr Tymoshenko 	return (retval);
124d6b3aaf8SOleksandr Tymoshenko }
125d6b3aaf8SOleksandr Tymoshenko 
126831f5dcfSAlexander Motin static void
127831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot)
128831f5dcfSAlexander Motin {
129831f5dcfSAlexander Motin 	slot_printf(slot,
130831f5dcfSAlexander Motin 	    "============== REGISTER DUMP ==============\n");
131831f5dcfSAlexander Motin 
132831f5dcfSAlexander Motin 	slot_printf(slot, "Sys addr: 0x%08x | Version:  0x%08x\n",
133831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
134831f5dcfSAlexander Motin 	slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
135831f5dcfSAlexander Motin 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
136831f5dcfSAlexander Motin 	slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
137831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
138831f5dcfSAlexander Motin 	slot_printf(slot, "Present:  0x%08x | Host ctl: 0x%08x\n",
139831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
140831f5dcfSAlexander Motin 	slot_printf(slot, "Power:    0x%08x | Blk gap:  0x%08x\n",
141831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
142831f5dcfSAlexander Motin 	slot_printf(slot, "Wake-up:  0x%08x | Clock:    0x%08x\n",
143831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
144831f5dcfSAlexander Motin 	slot_printf(slot, "Timeout:  0x%08x | Int stat: 0x%08x\n",
145831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
146831f5dcfSAlexander Motin 	slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
147831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
148831f5dcfSAlexander Motin 	slot_printf(slot, "AC12 err: 0x%08x | Slot int: 0x%08x\n",
149831f5dcfSAlexander Motin 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_SLOT_INT_STATUS));
150831f5dcfSAlexander Motin 	slot_printf(slot, "Caps:     0x%08x | Max curr: 0x%08x\n",
151831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_MAX_CURRENT));
152831f5dcfSAlexander Motin 
153831f5dcfSAlexander Motin 	slot_printf(slot,
154831f5dcfSAlexander Motin 	    "===========================================\n");
155831f5dcfSAlexander Motin }
156831f5dcfSAlexander Motin 
157831f5dcfSAlexander Motin static void
158831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
159831f5dcfSAlexander Motin {
160831f5dcfSAlexander Motin 	int timeout;
161831f5dcfSAlexander Motin 	uint8_t res;
162831f5dcfSAlexander Motin 
163d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
164831f5dcfSAlexander Motin 		if (!(RD4(slot, SDHCI_PRESENT_STATE) &
165831f5dcfSAlexander Motin 			SDHCI_CARD_PRESENT))
166831f5dcfSAlexander Motin 			return;
167831f5dcfSAlexander Motin 	}
168831f5dcfSAlexander Motin 
169831f5dcfSAlexander Motin 	/* Some controllers need this kick or reset won't work. */
170831f5dcfSAlexander Motin 	if ((mask & SDHCI_RESET_ALL) == 0 &&
171d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
172831f5dcfSAlexander Motin 		uint32_t clock;
173831f5dcfSAlexander Motin 
174831f5dcfSAlexander Motin 		/* This is to force an update */
175831f5dcfSAlexander Motin 		clock = slot->clock;
176831f5dcfSAlexander Motin 		slot->clock = 0;
177831f5dcfSAlexander Motin 		sdhci_set_clock(slot, clock);
178831f5dcfSAlexander Motin 	}
179831f5dcfSAlexander Motin 
180831f5dcfSAlexander Motin 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
181831f5dcfSAlexander Motin 
182d8208d9eSAlexander Motin 	if (mask & SDHCI_RESET_ALL) {
183831f5dcfSAlexander Motin 		slot->clock = 0;
184d8208d9eSAlexander Motin 		slot->power = 0;
185d8208d9eSAlexander Motin 	}
186831f5dcfSAlexander Motin 
187831f5dcfSAlexander Motin 	/* Wait max 100 ms */
188831f5dcfSAlexander Motin 	timeout = 100;
189831f5dcfSAlexander Motin 	/* Controller clears the bits when it's done */
190831f5dcfSAlexander Motin 	while ((res = RD1(slot, SDHCI_SOFTWARE_RESET)) & mask) {
191831f5dcfSAlexander Motin 		if (timeout == 0) {
192831f5dcfSAlexander Motin 			slot_printf(slot,
193831f5dcfSAlexander Motin 			    "Reset 0x%x never completed - 0x%x.\n",
194831f5dcfSAlexander Motin 			    (int)mask, (int)res);
195831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
196831f5dcfSAlexander Motin 			return;
197831f5dcfSAlexander Motin 		}
198831f5dcfSAlexander Motin 		timeout--;
199831f5dcfSAlexander Motin 		DELAY(1000);
200831f5dcfSAlexander Motin 	}
201831f5dcfSAlexander Motin }
202831f5dcfSAlexander Motin 
203831f5dcfSAlexander Motin static void
204831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot)
205831f5dcfSAlexander Motin {
206831f5dcfSAlexander Motin 
207831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
208831f5dcfSAlexander Motin 
209831f5dcfSAlexander Motin 	/* Enable interrupts. */
210831f5dcfSAlexander Motin 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
211831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
212831f5dcfSAlexander Motin 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
213831f5dcfSAlexander Motin 	    SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
214831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
215831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
216831f5dcfSAlexander Motin 	    SDHCI_INT_ACMD12ERR;
217831f5dcfSAlexander Motin 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
218831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
219831f5dcfSAlexander Motin }
220831f5dcfSAlexander Motin 
221831f5dcfSAlexander Motin static void
222831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
223831f5dcfSAlexander Motin {
224831f5dcfSAlexander Motin 	uint32_t res;
225831f5dcfSAlexander Motin 	uint16_t clk;
2268f3b7d56SOleksandr Tymoshenko 	uint16_t div;
227831f5dcfSAlexander Motin 	int timeout;
228831f5dcfSAlexander Motin 
229831f5dcfSAlexander Motin 	if (clock == slot->clock)
230831f5dcfSAlexander Motin 		return;
231831f5dcfSAlexander Motin 	slot->clock = clock;
232831f5dcfSAlexander Motin 
233831f5dcfSAlexander Motin 	/* Turn off the clock. */
234831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, 0);
235831f5dcfSAlexander Motin 	/* If no clock requested - left it so. */
236831f5dcfSAlexander Motin 	if (clock == 0)
237831f5dcfSAlexander Motin 		return;
2388f3b7d56SOleksandr Tymoshenko 	if (slot->version < SDHCI_SPEC_300) {
239831f5dcfSAlexander Motin 		/* Looking for highest freq <= clock. */
240831f5dcfSAlexander Motin 		res = slot->max_clk;
2418f3b7d56SOleksandr Tymoshenko 		for (div = 1; div < 256; div <<= 1) {
242831f5dcfSAlexander Motin 			if (res <= clock)
243831f5dcfSAlexander Motin 				break;
244831f5dcfSAlexander Motin 			res >>= 1;
245831f5dcfSAlexander Motin 		}
246831f5dcfSAlexander Motin 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
2478f3b7d56SOleksandr Tymoshenko 		div >>= 1;
2488f3b7d56SOleksandr Tymoshenko 	}
2498f3b7d56SOleksandr Tymoshenko 	else {
2508f3b7d56SOleksandr Tymoshenko 		/* Version 3.0 divisors are multiples of two up to 1023*2 */
2518f3b7d56SOleksandr Tymoshenko 		if (clock > slot->max_clk)
2528f3b7d56SOleksandr Tymoshenko 			div = 2;
2538f3b7d56SOleksandr Tymoshenko 		else {
2548f3b7d56SOleksandr Tymoshenko 			for (div = 2; div < 1023*2; div += 2) {
2558f3b7d56SOleksandr Tymoshenko 				if ((slot->max_clk / div) <= clock)
2568f3b7d56SOleksandr Tymoshenko 					break;
2578f3b7d56SOleksandr Tymoshenko 			}
2588f3b7d56SOleksandr Tymoshenko 		}
2598f3b7d56SOleksandr Tymoshenko 		div >>= 1;
2608f3b7d56SOleksandr Tymoshenko 	}
2618f3b7d56SOleksandr Tymoshenko 
2628f3b7d56SOleksandr Tymoshenko 	if (bootverbose || sdhci_debug)
2638f3b7d56SOleksandr Tymoshenko 		slot_printf(slot, "Divider %d for freq %d (max %d)\n",
2648f3b7d56SOleksandr Tymoshenko 			div, clock, slot->max_clk);
2658f3b7d56SOleksandr Tymoshenko 
266831f5dcfSAlexander Motin 	/* Now we have got divider, set it. */
2678f3b7d56SOleksandr Tymoshenko 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
2688f3b7d56SOleksandr Tymoshenko 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
2698f3b7d56SOleksandr Tymoshenko 		<< SDHCI_DIVIDER_HI_SHIFT;
2708f3b7d56SOleksandr Tymoshenko 
271831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
272831f5dcfSAlexander Motin 	/* Enable clock. */
273831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_INT_EN;
274831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
275831f5dcfSAlexander Motin 	/* Wait up to 10 ms until it stabilize. */
276831f5dcfSAlexander Motin 	timeout = 10;
277831f5dcfSAlexander Motin 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
278831f5dcfSAlexander Motin 		& SDHCI_CLOCK_INT_STABLE)) {
279831f5dcfSAlexander Motin 		if (timeout == 0) {
280831f5dcfSAlexander Motin 			slot_printf(slot,
281831f5dcfSAlexander Motin 			    "Internal clock never stabilised.\n");
282831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
283831f5dcfSAlexander Motin 			return;
284831f5dcfSAlexander Motin 		}
285831f5dcfSAlexander Motin 		timeout--;
286831f5dcfSAlexander Motin 		DELAY(1000);
287831f5dcfSAlexander Motin 	}
288831f5dcfSAlexander Motin 	/* Pass clock signal to the bus. */
289831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_CARD_EN;
290831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
291831f5dcfSAlexander Motin }
292831f5dcfSAlexander Motin 
293831f5dcfSAlexander Motin static void
294831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power)
295831f5dcfSAlexander Motin {
296831f5dcfSAlexander Motin 	uint8_t pwr;
297831f5dcfSAlexander Motin 
298831f5dcfSAlexander Motin 	if (slot->power == power)
299831f5dcfSAlexander Motin 		return;
300d6b3aaf8SOleksandr Tymoshenko 
301831f5dcfSAlexander Motin 	slot->power = power;
302831f5dcfSAlexander Motin 
303831f5dcfSAlexander Motin 	/* Turn off the power. */
304831f5dcfSAlexander Motin 	pwr = 0;
305831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
306831f5dcfSAlexander Motin 	/* If power down requested - left it so. */
307831f5dcfSAlexander Motin 	if (power == 0)
308831f5dcfSAlexander Motin 		return;
309831f5dcfSAlexander Motin 	/* Set voltage. */
310831f5dcfSAlexander Motin 	switch (1 << power) {
311831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
312831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
313831f5dcfSAlexander Motin 		break;
314831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
315831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
316831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
317831f5dcfSAlexander Motin 		break;
318831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
319831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
320831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
321831f5dcfSAlexander Motin 		break;
322831f5dcfSAlexander Motin 	}
323831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
324831f5dcfSAlexander Motin 	/* Turn on the power. */
325831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
326831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
327831f5dcfSAlexander Motin }
328831f5dcfSAlexander Motin 
329831f5dcfSAlexander Motin static void
330831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
331831f5dcfSAlexander Motin {
332831f5dcfSAlexander Motin 	uint32_t data;
333831f5dcfSAlexander Motin 	char *buffer;
334831f5dcfSAlexander Motin 	size_t left;
335831f5dcfSAlexander Motin 
336831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
337831f5dcfSAlexander Motin 	buffer += slot->offset;
338831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
339831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
340831f5dcfSAlexander Motin 	slot->offset += left;
341831f5dcfSAlexander Motin 
342831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
343d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
344831f5dcfSAlexander Motin 		DELAY(10);
345831f5dcfSAlexander Motin 	/* Handle unalligned and alligned buffer cases. */
346831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
347831f5dcfSAlexander Motin 		while (left > 3) {
348831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
349831f5dcfSAlexander Motin 			buffer[0] = data;
350831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
351831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
352831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
353831f5dcfSAlexander Motin 			buffer += 4;
354831f5dcfSAlexander Motin 			left -= 4;
355831f5dcfSAlexander Motin 		}
356831f5dcfSAlexander Motin 	} else {
357d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
358831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
359831f5dcfSAlexander Motin 		left &= 3;
360831f5dcfSAlexander Motin 	}
361831f5dcfSAlexander Motin 	/* Handle uneven size case. */
362831f5dcfSAlexander Motin 	if (left > 0) {
363831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
364831f5dcfSAlexander Motin 		while (left > 0) {
365831f5dcfSAlexander Motin 			*(buffer++) = data;
366831f5dcfSAlexander Motin 			data >>= 8;
367831f5dcfSAlexander Motin 			left--;
368831f5dcfSAlexander Motin 		}
369831f5dcfSAlexander Motin 	}
370831f5dcfSAlexander Motin }
371831f5dcfSAlexander Motin 
372831f5dcfSAlexander Motin static void
373831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
374831f5dcfSAlexander Motin {
375831f5dcfSAlexander Motin 	uint32_t data = 0;
376831f5dcfSAlexander Motin 	char *buffer;
377831f5dcfSAlexander Motin 	size_t left;
378831f5dcfSAlexander Motin 
379831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
380831f5dcfSAlexander Motin 	buffer += slot->offset;
381831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
382831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
383831f5dcfSAlexander Motin 	slot->offset += left;
384831f5dcfSAlexander Motin 
385831f5dcfSAlexander Motin 	/* Handle unalligned and alligned buffer cases. */
386831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
387831f5dcfSAlexander Motin 		while (left > 3) {
388831f5dcfSAlexander Motin 			data = buffer[0] +
389831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
390831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
391831f5dcfSAlexander Motin 			    (buffer[3] << 24);
392831f5dcfSAlexander Motin 			left -= 4;
393831f5dcfSAlexander Motin 			buffer += 4;
394831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
395831f5dcfSAlexander Motin 		}
396831f5dcfSAlexander Motin 	} else {
397d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
398831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
399831f5dcfSAlexander Motin 		left &= 3;
400831f5dcfSAlexander Motin 	}
401831f5dcfSAlexander Motin 	/* Handle uneven size case. */
402831f5dcfSAlexander Motin 	if (left > 0) {
403831f5dcfSAlexander Motin 		while (left > 0) {
404831f5dcfSAlexander Motin 			data <<= 8;
405831f5dcfSAlexander Motin 			data += *(buffer++);
406831f5dcfSAlexander Motin 			left--;
407831f5dcfSAlexander Motin 		}
408831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
409831f5dcfSAlexander Motin 	}
410831f5dcfSAlexander Motin }
411831f5dcfSAlexander Motin 
412831f5dcfSAlexander Motin static void
413831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
414831f5dcfSAlexander Motin {
415831f5dcfSAlexander Motin 
416831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
417831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
418831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
419831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
420831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
421831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
422831f5dcfSAlexander Motin 				break;
423831f5dcfSAlexander Motin 		}
424831f5dcfSAlexander Motin 	} else {
425831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
426831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
427831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
428831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
429831f5dcfSAlexander Motin 				break;
430831f5dcfSAlexander Motin 		}
431831f5dcfSAlexander Motin 	}
432831f5dcfSAlexander Motin }
433831f5dcfSAlexander Motin 
434831f5dcfSAlexander Motin static void
435831f5dcfSAlexander Motin sdhci_card_delay(void *arg)
436831f5dcfSAlexander Motin {
437831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
438831f5dcfSAlexander Motin 
439831f5dcfSAlexander Motin 	taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
440831f5dcfSAlexander Motin }
441831f5dcfSAlexander Motin 
442831f5dcfSAlexander Motin static void
443831f5dcfSAlexander Motin sdhci_card_task(void *arg, int pending)
444831f5dcfSAlexander Motin {
445831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
446831f5dcfSAlexander Motin 
447831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
448831f5dcfSAlexander Motin 	if (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT) {
449831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
450831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
451d6b3aaf8SOleksandr Tymoshenko 			slot->dev = device_add_child(slot->bus, "mmc", -1);
452831f5dcfSAlexander Motin 			device_set_ivars(slot->dev, slot);
453831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
454831f5dcfSAlexander Motin 			device_probe_and_attach(slot->dev);
455831f5dcfSAlexander Motin 		} else
456831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
457831f5dcfSAlexander Motin 	} else {
458831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
459831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
460831f5dcfSAlexander Motin 			device_t d = slot->dev;
461831f5dcfSAlexander Motin 			slot->dev = NULL;
462831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
463d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
464831f5dcfSAlexander Motin 		} else
465831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
466831f5dcfSAlexander Motin 	}
467831f5dcfSAlexander Motin }
468831f5dcfSAlexander Motin 
469d6b3aaf8SOleksandr Tymoshenko int
470d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
471831f5dcfSAlexander Motin {
472831f5dcfSAlexander Motin 	uint32_t caps;
473d6b3aaf8SOleksandr Tymoshenko 	int err;
474831f5dcfSAlexander Motin 
475831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
476d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
477d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
478d6b3aaf8SOleksandr Tymoshenko 
479831f5dcfSAlexander Motin 	/* Allocate DMA tag. */
480831f5dcfSAlexander Motin 	err = bus_dma_tag_create(bus_get_dma_tag(dev),
481831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
482831f5dcfSAlexander Motin 	    BUS_SPACE_MAXADDR, NULL, NULL,
483831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE,
484831f5dcfSAlexander Motin 	    BUS_DMA_ALLOCNOW, NULL, NULL,
485831f5dcfSAlexander Motin 	    &slot->dmatag);
486831f5dcfSAlexander Motin 	if (err != 0) {
487831f5dcfSAlexander Motin 		device_printf(dev, "Can't create DMA tag\n");
488831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
489d6b3aaf8SOleksandr Tymoshenko 		return (err);
490831f5dcfSAlexander Motin 	}
491831f5dcfSAlexander Motin 	/* Allocate DMA memory. */
492831f5dcfSAlexander Motin 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
493831f5dcfSAlexander Motin 	    BUS_DMA_NOWAIT, &slot->dmamap);
494831f5dcfSAlexander Motin 	if (err != 0) {
495831f5dcfSAlexander Motin 		device_printf(dev, "Can't alloc DMA memory\n");
496831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
497d6b3aaf8SOleksandr Tymoshenko 		return (err);
498831f5dcfSAlexander Motin 	}
499831f5dcfSAlexander Motin 	/* Map the memory. */
500831f5dcfSAlexander Motin 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
501831f5dcfSAlexander Motin 	    (void *)slot->dmamem, DMA_BLOCK_SIZE,
502831f5dcfSAlexander Motin 	    sdhci_getaddr, &slot->paddr, 0);
503831f5dcfSAlexander Motin 	if (err != 0 || slot->paddr == 0) {
504831f5dcfSAlexander Motin 		device_printf(dev, "Can't load DMA memory\n");
505831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
506d6b3aaf8SOleksandr Tymoshenko 		if(err)
507d6b3aaf8SOleksandr Tymoshenko 			return (err);
508d6b3aaf8SOleksandr Tymoshenko 		else
509d6b3aaf8SOleksandr Tymoshenko 			return (EFAULT);
510831f5dcfSAlexander Motin 	}
511d6b3aaf8SOleksandr Tymoshenko 
512831f5dcfSAlexander Motin 	/* Initialize slot. */
513831f5dcfSAlexander Motin 	sdhci_init(slot);
514d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
515d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
5168f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS)
5178f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
5188f3b7d56SOleksandr Tymoshenko 	else
519831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
520831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
521*33aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
522*33aad34dSOleksandr Tymoshenko 		slot->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK)
523*33aad34dSOleksandr Tymoshenko 		    >> SDHCI_CLOCK_BASE_SHIFT;
524*33aad34dSOleksandr Tymoshenko 	else
525*33aad34dSOleksandr Tymoshenko 		slot->max_clk = (caps & SDHCI_CLOCK_BASE_MASK)
526*33aad34dSOleksandr Tymoshenko 		    >> SDHCI_CLOCK_BASE_SHIFT;
527831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
528*33aad34dSOleksandr Tymoshenko 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ;
529831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify base clock "
530*33aad34dSOleksandr Tymoshenko 		    "frequency, using %dMHz as default.\n", SDHCI_DEFAULT_MAX_FREQ);
531831f5dcfSAlexander Motin 	}
532831f5dcfSAlexander Motin 	slot->max_clk *= 1000000;
533831f5dcfSAlexander Motin 	/* Calculate timeout clock frequency. */
5348f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
5358f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
5368f3b7d56SOleksandr Tymoshenko 	} else {
537831f5dcfSAlexander Motin 		slot->timeout_clk =
538831f5dcfSAlexander Motin 			(caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
5398f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
5408f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
5418f3b7d56SOleksandr Tymoshenko 	}
5428f3b7d56SOleksandr Tymoshenko 
543831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
544831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify timeout clock "
545831f5dcfSAlexander Motin 		    "frequency.\n");
546831f5dcfSAlexander Motin 	}
547831f5dcfSAlexander Motin 
548831f5dcfSAlexander Motin 	slot->host.f_min = slot->max_clk / 256;
549831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
550831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
551831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
552831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
553831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
554831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
555831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_180)
556831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
557831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
558831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't report any "
559831f5dcfSAlexander Motin 		    "support voltages.\n");
560831f5dcfSAlexander Motin 	}
561831f5dcfSAlexander Motin 	slot->host.caps = MMC_CAP_4_BIT_DATA;
562831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
563831f5dcfSAlexander Motin 		slot->host.caps |= MMC_CAP_HSPEED;
564831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
565831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
566831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
567d6b3aaf8SOleksandr Tymoshenko 
568d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
569831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
570d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
571831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
572831f5dcfSAlexander Motin 
5735b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
574831f5dcfSAlexander Motin 		slot_printf(slot, "%uMHz%s 4bits%s%s%s %s\n",
575831f5dcfSAlexander Motin 		    slot->max_clk / 1000000,
576831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
577831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
578831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
579831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "",
580831f5dcfSAlexander Motin 		    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO");
581831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
582831f5dcfSAlexander Motin 	}
583831f5dcfSAlexander Motin 
584831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
585831f5dcfSAlexander Motin 	callout_init(&slot->card_callout, 1);
586831f5dcfSAlexander Motin 	return (0);
587831f5dcfSAlexander Motin }
588831f5dcfSAlexander Motin 
589d6b3aaf8SOleksandr Tymoshenko void
590d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
591831f5dcfSAlexander Motin {
592d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
593d6b3aaf8SOleksandr Tymoshenko }
594831f5dcfSAlexander Motin 
595d6b3aaf8SOleksandr Tymoshenko int
596d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
597d6b3aaf8SOleksandr Tymoshenko {
598831f5dcfSAlexander Motin 	device_t d;
599831f5dcfSAlexander Motin 
600831f5dcfSAlexander Motin 	callout_drain(&slot->card_callout);
601831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
602831f5dcfSAlexander Motin 
603831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
604831f5dcfSAlexander Motin 	d = slot->dev;
605831f5dcfSAlexander Motin 	slot->dev = NULL;
606831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
607831f5dcfSAlexander Motin 	if (d != NULL)
608d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
609831f5dcfSAlexander Motin 
610831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
611831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
612831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
613831f5dcfSAlexander Motin 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
614831f5dcfSAlexander Motin 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
615831f5dcfSAlexander Motin 	bus_dma_tag_destroy(slot->dmatag);
616d6b3aaf8SOleksandr Tymoshenko 
617831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
618d6b3aaf8SOleksandr Tymoshenko 
619831f5dcfSAlexander Motin 	return (0);
620831f5dcfSAlexander Motin }
621831f5dcfSAlexander Motin 
622d6b3aaf8SOleksandr Tymoshenko int
623d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
62492bf0e27SAlexander Motin {
625d6b3aaf8SOleksandr Tymoshenko 	sdhci_reset(slot, SDHCI_RESET_ALL);
62692bf0e27SAlexander Motin 
62792bf0e27SAlexander Motin 	return (0);
62892bf0e27SAlexander Motin }
62992bf0e27SAlexander Motin 
630d6b3aaf8SOleksandr Tymoshenko int
631d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
63292bf0e27SAlexander Motin {
633d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
63492bf0e27SAlexander Motin 
635d6b3aaf8SOleksandr Tymoshenko 	return (0);
63692bf0e27SAlexander Motin }
63792bf0e27SAlexander Motin 
638d6b3aaf8SOleksandr Tymoshenko int
639d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
640831f5dcfSAlexander Motin {
641831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
642831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
643831f5dcfSAlexander Motin 
644831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
645831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
646831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
647831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
648831f5dcfSAlexander Motin 		sdhci_init(slot);
649831f5dcfSAlexander Motin 	}
650831f5dcfSAlexander Motin 	/* Configure the bus. */
651831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
652831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off)?0:ios->vdd);
653831f5dcfSAlexander Motin 	if (ios->bus_width == bus_width_4)
654831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
655831f5dcfSAlexander Motin 	else
656831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
657831f5dcfSAlexander Motin 	if (ios->timing == bus_timing_hs)
658831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
659831f5dcfSAlexander Motin 	else
660831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
661831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
662831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
663d6b3aaf8SOleksandr Tymoshenko 	if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
664831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
665831f5dcfSAlexander Motin 
666831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
667831f5dcfSAlexander Motin 	return (0);
668831f5dcfSAlexander Motin }
669831f5dcfSAlexander Motin 
670831f5dcfSAlexander Motin static void
671831f5dcfSAlexander Motin sdhci_set_transfer_mode(struct sdhci_slot *slot,
672831f5dcfSAlexander Motin 	struct mmc_data *data)
673831f5dcfSAlexander Motin {
674831f5dcfSAlexander Motin 	uint16_t mode;
675831f5dcfSAlexander Motin 
676831f5dcfSAlexander Motin 	if (data == NULL)
677831f5dcfSAlexander Motin 		return;
678831f5dcfSAlexander Motin 
679831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
680831f5dcfSAlexander Motin 	if (data->len > 512)
681831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
682831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
683831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
684831f5dcfSAlexander Motin 	if (slot->req->stop)
685831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_ACMD12;
686831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
687831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
688831f5dcfSAlexander Motin 
689831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
690831f5dcfSAlexander Motin }
691831f5dcfSAlexander Motin 
692831f5dcfSAlexander Motin static void
693831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
694831f5dcfSAlexander Motin {
695831f5dcfSAlexander Motin 	struct mmc_request *req = slot->req;
696831f5dcfSAlexander Motin 	int flags, timeout;
697831f5dcfSAlexander Motin 	uint32_t mask, state;
698831f5dcfSAlexander Motin 
699831f5dcfSAlexander Motin 	slot->curcmd = cmd;
700831f5dcfSAlexander Motin 	slot->cmd_done = 0;
701831f5dcfSAlexander Motin 
702831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
703831f5dcfSAlexander Motin 
704831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
705831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
706831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
707831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
708831f5dcfSAlexander Motin 		slot->req = NULL;
709831f5dcfSAlexander Motin 		slot->curcmd = NULL;
710831f5dcfSAlexander Motin 		req->done(req);
711831f5dcfSAlexander Motin 		return;
712831f5dcfSAlexander Motin 	}
713831f5dcfSAlexander Motin 
714831f5dcfSAlexander Motin 	/* Read controller present state. */
715831f5dcfSAlexander Motin 	state = RD4(slot, SDHCI_PRESENT_STATE);
716d8208d9eSAlexander Motin 	/* Do not issue command if there is no card, clock or power.
717d8208d9eSAlexander Motin 	 * Controller will not detect timeout without clock active. */
718d8208d9eSAlexander Motin 	if ((state & SDHCI_CARD_PRESENT) == 0 ||
719d8208d9eSAlexander Motin 	    slot->power == 0 ||
720d8208d9eSAlexander Motin 	    slot->clock == 0) {
721831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
722831f5dcfSAlexander Motin 		slot->req = NULL;
723831f5dcfSAlexander Motin 		slot->curcmd = NULL;
724831f5dcfSAlexander Motin 		req->done(req);
725831f5dcfSAlexander Motin 		return;
726831f5dcfSAlexander Motin 	}
727831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
728831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
729831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
730831f5dcfSAlexander Motin 	if (cmd->data || (cmd->flags & MMC_RSP_BUSY))
731831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
732831f5dcfSAlexander Motin 	/* We shouldn't wait for DAT for stop commands. */
733831f5dcfSAlexander Motin 	if (cmd == slot->req->stop)
734831f5dcfSAlexander Motin 		mask &= ~SDHCI_DAT_INHIBIT;
735831f5dcfSAlexander Motin 	/* Wait for bus no more then 10 ms. */
736831f5dcfSAlexander Motin 	timeout = 10;
737831f5dcfSAlexander Motin 	while (state & mask) {
738831f5dcfSAlexander Motin 		if (timeout == 0) {
739831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
740831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
741831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
742831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
743831f5dcfSAlexander Motin 			slot->req = NULL;
744831f5dcfSAlexander Motin 			slot->curcmd = NULL;
745831f5dcfSAlexander Motin 			req->done(req);
746831f5dcfSAlexander Motin 			return;
747831f5dcfSAlexander Motin 		}
748831f5dcfSAlexander Motin 		timeout--;
749831f5dcfSAlexander Motin 		DELAY(1000);
750831f5dcfSAlexander Motin 		state = RD4(slot, SDHCI_PRESENT_STATE);
751831f5dcfSAlexander Motin 	}
752831f5dcfSAlexander Motin 
753831f5dcfSAlexander Motin 	/* Prepare command flags. */
754831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
755831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
756831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
757831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
758831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
759831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
760831f5dcfSAlexander Motin 	else
761831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
762831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
763831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
764831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
765831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
766831f5dcfSAlexander Motin 	if (cmd->data)
767831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
768831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
769831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
770831f5dcfSAlexander Motin 	/* Prepare data. */
771831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
772831f5dcfSAlexander Motin 	/*
773831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
774831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
775831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
776831f5dcfSAlexander Motin 	 */
777831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
778831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
779831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
780831f5dcfSAlexander Motin 	}
781831f5dcfSAlexander Motin 	/* Set command argument. */
782831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
783831f5dcfSAlexander Motin 	/* Set data transfer mode. */
784831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
785831f5dcfSAlexander Motin 	/* Start command. */
786d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
787831f5dcfSAlexander Motin }
788831f5dcfSAlexander Motin 
789831f5dcfSAlexander Motin static void
790831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
791831f5dcfSAlexander Motin {
792831f5dcfSAlexander Motin 	int i;
793831f5dcfSAlexander Motin 
794831f5dcfSAlexander Motin 	slot->cmd_done = 1;
795831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
796831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
797831f5dcfSAlexander Motin 	 * happened first. */
798831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE);
799831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
800831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
801831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
802831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
803831f5dcfSAlexander Motin 		sdhci_start(slot);
804831f5dcfSAlexander Motin 		return;
805831f5dcfSAlexander Motin 	}
806831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
807831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
808831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
809831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
810831f5dcfSAlexander Motin 			uint8_t extra = 0;
811831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
812831f5dcfSAlexander Motin 				uint32_t val = RD4(slot, SDHCI_RESPONSE + i * 4);
813831f5dcfSAlexander Motin 				slot->curcmd->resp[3 - i] = (val << 8) + extra;
814831f5dcfSAlexander Motin 				extra = val >> 24;
815831f5dcfSAlexander Motin 			}
816831f5dcfSAlexander Motin 		} else
817831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
818831f5dcfSAlexander Motin 	}
819831f5dcfSAlexander Motin 	/* If data ready - finish. */
820831f5dcfSAlexander Motin 	if (slot->data_done)
821831f5dcfSAlexander Motin 		sdhci_start(slot);
822831f5dcfSAlexander Motin }
823831f5dcfSAlexander Motin 
824831f5dcfSAlexander Motin static void
825831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data)
826831f5dcfSAlexander Motin {
827831f5dcfSAlexander Motin 	uint32_t target_timeout, current_timeout;
828831f5dcfSAlexander Motin 	uint8_t div;
829831f5dcfSAlexander Motin 
830831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
831831f5dcfSAlexander Motin 		slot->data_done = 1;
832831f5dcfSAlexander Motin 		return;
833831f5dcfSAlexander Motin 	}
834831f5dcfSAlexander Motin 
835831f5dcfSAlexander Motin 	slot->data_done = 0;
836831f5dcfSAlexander Motin 
837831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
838831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
839831f5dcfSAlexander Motin 	target_timeout = 1000000;
840831f5dcfSAlexander Motin 	div = 0;
841831f5dcfSAlexander Motin 	current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
842831f5dcfSAlexander Motin 	while (current_timeout < target_timeout) {
843831f5dcfSAlexander Motin 		div++;
844831f5dcfSAlexander Motin 		current_timeout <<= 1;
845831f5dcfSAlexander Motin 		if (div >= 0xF)
846831f5dcfSAlexander Motin 			break;
847831f5dcfSAlexander Motin 	}
848831f5dcfSAlexander Motin 	/* Compensate for an off-by-one error in the CaFe chip.*/
849d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)
850831f5dcfSAlexander Motin 		div++;
851831f5dcfSAlexander Motin 	if (div >= 0xF) {
852831f5dcfSAlexander Motin 		slot_printf(slot, "Timeout too large!\n");
853831f5dcfSAlexander Motin 		div = 0xE;
854831f5dcfSAlexander Motin 	}
8558f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
8568f3b7d56SOleksandr Tymoshenko 		div = 0xE;
857831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
858831f5dcfSAlexander Motin 
859831f5dcfSAlexander Motin 	if (data == NULL)
860831f5dcfSAlexander Motin 		return;
861831f5dcfSAlexander Motin 
862831f5dcfSAlexander Motin 	/* Use DMA if possible. */
863831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
864831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
865831f5dcfSAlexander Motin 	/* If data is small, broken DMA may return zeroes instead of data, */
866d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
867831f5dcfSAlexander Motin 	    (data->len <= 512))
868831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
869831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
870d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
871831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
872831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
873831f5dcfSAlexander Motin 	/* Load DMA buffer. */
874831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
875831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
876831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap, BUS_DMASYNC_PREREAD);
877831f5dcfSAlexander Motin 		else {
878831f5dcfSAlexander Motin 			memcpy(slot->dmamem, data->data,
879831f5dcfSAlexander Motin 			    (data->len < DMA_BLOCK_SIZE)?data->len:DMA_BLOCK_SIZE);
880831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap, BUS_DMASYNC_PREWRITE);
881831f5dcfSAlexander Motin 		}
882831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
883831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
884831f5dcfSAlexander Motin 		 * for the last page and unmask else. */
885831f5dcfSAlexander Motin 		if (data->len == DMA_BLOCK_SIZE)
886831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
887831f5dcfSAlexander Motin 		else
888831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
889831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
890831f5dcfSAlexander Motin 	}
891831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
892831f5dcfSAlexander Motin 	slot->offset = 0;
893831f5dcfSAlexander Motin 	/* Set block size and request IRQ on 4K border. */
894831f5dcfSAlexander Motin 	WR2(slot, SDHCI_BLOCK_SIZE,
895831f5dcfSAlexander Motin 	    SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512)?data->len:512));
896831f5dcfSAlexander Motin 	/* Set block count. */
897831f5dcfSAlexander Motin 	WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
898831f5dcfSAlexander Motin }
899831f5dcfSAlexander Motin 
900831f5dcfSAlexander Motin static void
901831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
902831f5dcfSAlexander Motin {
903831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
904831f5dcfSAlexander Motin 
905831f5dcfSAlexander Motin 	slot->data_done = 1;
906831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
907831f5dcfSAlexander Motin 	 * Auxillary restore point for the case when data interrupt
908831f5dcfSAlexander Motin 	 * happened first. */
909831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
910831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
911831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
912831f5dcfSAlexander Motin 	}
913831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
914831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
915831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
916831f5dcfSAlexander Motin 			size_t left = data->len - slot->offset;
917831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap, BUS_DMASYNC_POSTREAD);
918831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
919831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
920831f5dcfSAlexander Motin 		} else
921831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap, BUS_DMASYNC_POSTWRITE);
922831f5dcfSAlexander Motin 	}
923831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
924831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
925831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
926831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
927831f5dcfSAlexander Motin 		sdhci_start(slot);
928831f5dcfSAlexander Motin 		return;
929831f5dcfSAlexander Motin 	}
930831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
931831f5dcfSAlexander Motin 	if (slot->cmd_done)
932831f5dcfSAlexander Motin 		sdhci_start(slot);
933831f5dcfSAlexander Motin }
934831f5dcfSAlexander Motin 
935831f5dcfSAlexander Motin static void
936831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
937831f5dcfSAlexander Motin {
938831f5dcfSAlexander Motin 	struct mmc_request *req;
939831f5dcfSAlexander Motin 
940831f5dcfSAlexander Motin 	req = slot->req;
941831f5dcfSAlexander Motin 	if (req == NULL)
942831f5dcfSAlexander Motin 		return;
943831f5dcfSAlexander Motin 
944831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
945831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
946831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
947831f5dcfSAlexander Motin 		return;
948831f5dcfSAlexander Motin 	}
949831f5dcfSAlexander Motin /* 	We don't need this until using Auto-CMD12 feature
950831f5dcfSAlexander Motin 	if (!(slot->flags & STOP_STARTED) && req->stop) {
951831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
952831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
953831f5dcfSAlexander Motin 		return;
954831f5dcfSAlexander Motin 	}
955831f5dcfSAlexander Motin */
9565b69a497SAlexander Motin 	if (sdhci_debug > 1)
9575b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
9585b69a497SAlexander Motin 	if (!req->cmd->error &&
959d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
960831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
961831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
962831f5dcfSAlexander Motin 	}
963831f5dcfSAlexander Motin 
964831f5dcfSAlexander Motin 	/* We must be done -- bad idea to do this while locked? */
965831f5dcfSAlexander Motin 	slot->req = NULL;
966831f5dcfSAlexander Motin 	slot->curcmd = NULL;
967831f5dcfSAlexander Motin 	req->done(req);
968831f5dcfSAlexander Motin }
969831f5dcfSAlexander Motin 
970d6b3aaf8SOleksandr Tymoshenko int
971d6b3aaf8SOleksandr Tymoshenko sdhci_generic_request(device_t brdev, device_t reqdev, struct mmc_request *req)
972831f5dcfSAlexander Motin {
973831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
974831f5dcfSAlexander Motin 
975831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
976831f5dcfSAlexander Motin 	if (slot->req != NULL) {
977831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
978831f5dcfSAlexander Motin 		return (EBUSY);
979831f5dcfSAlexander Motin 	}
9805b69a497SAlexander Motin 	if (sdhci_debug > 1) {
9815b69a497SAlexander Motin 		slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
982831f5dcfSAlexander Motin     		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
9835b69a497SAlexander Motin     		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
9845b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
9855b69a497SAlexander Motin 	}
986831f5dcfSAlexander Motin 	slot->req = req;
987831f5dcfSAlexander Motin 	slot->flags = 0;
988831f5dcfSAlexander Motin 	sdhci_start(slot);
989831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
990bea2dca2SAlexander Motin 	if (dumping) {
991bea2dca2SAlexander Motin 		while (slot->req != NULL) {
992d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
993bea2dca2SAlexander Motin 			DELAY(10);
994bea2dca2SAlexander Motin 		}
995bea2dca2SAlexander Motin 	}
996831f5dcfSAlexander Motin 	return (0);
997831f5dcfSAlexander Motin }
998831f5dcfSAlexander Motin 
999d6b3aaf8SOleksandr Tymoshenko int
1000d6b3aaf8SOleksandr Tymoshenko sdhci_generic_get_ro(device_t brdev, device_t reqdev)
1001831f5dcfSAlexander Motin {
1002831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1003831f5dcfSAlexander Motin 	uint32_t val;
1004831f5dcfSAlexander Motin 
1005831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1006831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
1007831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1008831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
1009831f5dcfSAlexander Motin }
1010831f5dcfSAlexander Motin 
1011d6b3aaf8SOleksandr Tymoshenko int
1012d6b3aaf8SOleksandr Tymoshenko sdhci_generic_acquire_host(device_t brdev, device_t reqdev)
1013831f5dcfSAlexander Motin {
1014831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1015831f5dcfSAlexander Motin 	int err = 0;
1016831f5dcfSAlexander Motin 
1017831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1018831f5dcfSAlexander Motin 	while (slot->bus_busy)
1019d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
1020831f5dcfSAlexander Motin 	slot->bus_busy++;
1021831f5dcfSAlexander Motin 	/* Activate led. */
1022831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
1023831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1024831f5dcfSAlexander Motin 	return (err);
1025831f5dcfSAlexander Motin }
1026831f5dcfSAlexander Motin 
1027d6b3aaf8SOleksandr Tymoshenko int
1028d6b3aaf8SOleksandr Tymoshenko sdhci_generic_release_host(device_t brdev, device_t reqdev)
1029831f5dcfSAlexander Motin {
1030831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1031831f5dcfSAlexander Motin 
1032831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1033831f5dcfSAlexander Motin 	/* Deactivate led. */
1034831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
1035831f5dcfSAlexander Motin 	slot->bus_busy--;
1036831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1037d493985aSAlexander Motin 	wakeup(slot);
1038831f5dcfSAlexander Motin 	return (0);
1039831f5dcfSAlexander Motin }
1040831f5dcfSAlexander Motin 
1041831f5dcfSAlexander Motin static void
1042831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
1043831f5dcfSAlexander Motin {
1044831f5dcfSAlexander Motin 
1045831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1046831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
1047831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1048831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1049831f5dcfSAlexander Motin 		return;
1050831f5dcfSAlexander Motin 	}
1051831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
1052831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1053831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
1054831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1055831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
1056831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
1057831f5dcfSAlexander Motin 
1058831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
1059831f5dcfSAlexander Motin }
1060831f5dcfSAlexander Motin 
1061831f5dcfSAlexander Motin static void
1062831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
1063831f5dcfSAlexander Motin {
1064831f5dcfSAlexander Motin 
1065831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1066831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1067831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1068831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1069831f5dcfSAlexander Motin 		return;
1070831f5dcfSAlexander Motin 	}
1071831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
1072831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1073831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1074831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
1075831f5dcfSAlexander Motin 		    intmask);
1076831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1077831f5dcfSAlexander Motin 		return;
1078831f5dcfSAlexander Motin 	}
1079831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
1080831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1081831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1082831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1083831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
1084831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
1085831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
1086831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1087831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
1088831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1089831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
1090831f5dcfSAlexander Motin 	}
1091831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1092831f5dcfSAlexander Motin 		/* No need to continue after any error. */
1093831f5dcfSAlexander Motin 		sdhci_finish_data(slot);
1094831f5dcfSAlexander Motin 		return;
1095831f5dcfSAlexander Motin 	}
1096831f5dcfSAlexander Motin 
1097831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
1098831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
1099831f5dcfSAlexander Motin 		sdhci_transfer_pio(slot);
1100831f5dcfSAlexander Motin 	/* Handle DMA border. */
1101831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
1102831f5dcfSAlexander Motin 		struct mmc_data *data = slot->curcmd->data;
1103831f5dcfSAlexander Motin 		size_t left;
1104831f5dcfSAlexander Motin 
1105831f5dcfSAlexander Motin 		/* Unload DMA buffer... */
1106831f5dcfSAlexander Motin 		left = data->len - slot->offset;
1107831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
1108831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1109831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
1110831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1111831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1112831f5dcfSAlexander Motin 		} else {
1113831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1114831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
1115831f5dcfSAlexander Motin 		}
1116831f5dcfSAlexander Motin 		/* ... and reload it again. */
1117831f5dcfSAlexander Motin 		slot->offset += DMA_BLOCK_SIZE;
1118831f5dcfSAlexander Motin 		left = data->len - slot->offset;
1119831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
1120831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1121831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREREAD);
1122831f5dcfSAlexander Motin 		} else {
1123831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
1124831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1125831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1126831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
1127831f5dcfSAlexander Motin 		}
1128831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1129831f5dcfSAlexander Motin 		 * for the last page. */
1130831f5dcfSAlexander Motin 		if (left == DMA_BLOCK_SIZE) {
1131831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1132831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1133831f5dcfSAlexander Motin 		}
1134831f5dcfSAlexander Motin 		/* Restart DMA. */
1135831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1136831f5dcfSAlexander Motin 	}
1137831f5dcfSAlexander Motin 	/* We have got all data. */
1138831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_END)
1139831f5dcfSAlexander Motin 		sdhci_finish_data(slot);
1140831f5dcfSAlexander Motin }
1141831f5dcfSAlexander Motin 
1142831f5dcfSAlexander Motin static void
1143831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot)
1144831f5dcfSAlexander Motin {
1145831f5dcfSAlexander Motin 	uint16_t err;
1146831f5dcfSAlexander Motin 
1147831f5dcfSAlexander Motin 	err = RD4(slot, SDHCI_ACMD12_ERR);
1148831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1149831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
1150831f5dcfSAlexander Motin 		    "there is no active command.\n", err);
1151831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1152831f5dcfSAlexander Motin 		return;
1153831f5dcfSAlexander Motin 	}
1154831f5dcfSAlexander Motin 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err);
1155831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_CMD);
1156831f5dcfSAlexander Motin }
1157831f5dcfSAlexander Motin 
1158d6b3aaf8SOleksandr Tymoshenko void
1159d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
1160831f5dcfSAlexander Motin {
1161831f5dcfSAlexander Motin 	uint32_t intmask;
1162831f5dcfSAlexander Motin 
1163831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1164831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
1165831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
1166831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
1167831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1168d6b3aaf8SOleksandr Tymoshenko 		return;
1169831f5dcfSAlexander Motin 	}
11705b69a497SAlexander Motin 	if (sdhci_debug > 2)
11715b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
11725b69a497SAlexander Motin 
1173831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
1174831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1175831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
1176831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
1177831f5dcfSAlexander Motin 
1178831f5dcfSAlexander Motin 		if (intmask & SDHCI_INT_CARD_REMOVE) {
11795b69a497SAlexander Motin 			if (bootverbose || sdhci_debug)
1180831f5dcfSAlexander Motin 				slot_printf(slot, "Card removed\n");
1181831f5dcfSAlexander Motin 			callout_stop(&slot->card_callout);
1182831f5dcfSAlexander Motin 			taskqueue_enqueue(taskqueue_swi_giant,
1183831f5dcfSAlexander Motin 			    &slot->card_task);
1184831f5dcfSAlexander Motin 		}
1185831f5dcfSAlexander Motin 		if (intmask & SDHCI_INT_CARD_INSERT) {
11865b69a497SAlexander Motin 			if (bootverbose || sdhci_debug)
1187831f5dcfSAlexander Motin 				slot_printf(slot, "Card inserted\n");
1188831f5dcfSAlexander Motin 			callout_reset(&slot->card_callout, hz / 2,
1189831f5dcfSAlexander Motin 			    sdhci_card_delay, slot);
1190831f5dcfSAlexander Motin 		}
1191831f5dcfSAlexander Motin 		intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1192831f5dcfSAlexander Motin 	}
1193831f5dcfSAlexander Motin 	/* Handle command interrupts. */
1194831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
1195831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
1196831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
1197831f5dcfSAlexander Motin 	}
1198831f5dcfSAlexander Motin 	/* Handle data interrupts. */
1199831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
1200831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
1201831f5dcfSAlexander Motin 		sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
1202831f5dcfSAlexander Motin 	}
1203831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
1204831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
1205831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
1206831f5dcfSAlexander Motin 		sdhci_acmd_irq(slot);
1207831f5dcfSAlexander Motin 	}
1208831f5dcfSAlexander Motin 	intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1209831f5dcfSAlexander Motin 	intmask &= ~SDHCI_INT_ACMD12ERR;
1210831f5dcfSAlexander Motin 	intmask &= ~SDHCI_INT_ERROR;
1211831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
1212831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
1213831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
1214831f5dcfSAlexander Motin 		slot_printf(slot,
1215831f5dcfSAlexander Motin 		    "Card is consuming too much power!\n");
1216831f5dcfSAlexander Motin 		intmask &= ~SDHCI_INT_BUS_POWER;
1217831f5dcfSAlexander Motin 	}
1218831f5dcfSAlexander Motin 	/* The rest is unknown. */
1219831f5dcfSAlexander Motin 	if (intmask) {
1220831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
1221831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
1222831f5dcfSAlexander Motin 		    intmask);
1223831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1224831f5dcfSAlexander Motin 	}
1225831f5dcfSAlexander Motin 
1226831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1227831f5dcfSAlexander Motin }
1228831f5dcfSAlexander Motin 
1229d6b3aaf8SOleksandr Tymoshenko int
1230d6b3aaf8SOleksandr Tymoshenko sdhci_generic_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1231831f5dcfSAlexander Motin {
1232831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
1233831f5dcfSAlexander Motin 
1234831f5dcfSAlexander Motin 	switch (which) {
1235831f5dcfSAlexander Motin 	default:
1236831f5dcfSAlexander Motin 		return (EINVAL);
1237831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
1238bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
1239831f5dcfSAlexander Motin 		break;
1240831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
1241bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
1242831f5dcfSAlexander Motin 		break;
1243831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
1244bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
1245831f5dcfSAlexander Motin 		break;
1246831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
1247bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
1248831f5dcfSAlexander Motin 		break;
1249831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
1250bcd91d25SJayachandran C. 		*result = slot->host.f_min;
1251831f5dcfSAlexander Motin 		break;
1252831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
1253bcd91d25SJayachandran C. 		*result = slot->host.f_max;
1254831f5dcfSAlexander Motin 		break;
1255831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
1256bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
1257831f5dcfSAlexander Motin 		break;
1258831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
1259bcd91d25SJayachandran C. 		*result = slot->host.mode;
1260831f5dcfSAlexander Motin 		break;
1261831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
1262bcd91d25SJayachandran C. 		*result = slot->host.ocr;
1263831f5dcfSAlexander Motin 		break;
1264831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
1265bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
1266831f5dcfSAlexander Motin 		break;
1267831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
1268bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
1269831f5dcfSAlexander Motin 		break;
1270831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
1271bcd91d25SJayachandran C. 		*result = slot->host.caps;
1272831f5dcfSAlexander Motin 		break;
1273831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
1274bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
1275831f5dcfSAlexander Motin 		break;
12763a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
1277bcd91d25SJayachandran C. 		*result = 65535;
12783a4a2557SAlexander Motin 		break;
1279831f5dcfSAlexander Motin 	}
1280831f5dcfSAlexander Motin 	return (0);
1281831f5dcfSAlexander Motin }
1282831f5dcfSAlexander Motin 
1283d6b3aaf8SOleksandr Tymoshenko int
1284d6b3aaf8SOleksandr Tymoshenko sdhci_generic_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1285831f5dcfSAlexander Motin {
1286831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
1287831f5dcfSAlexander Motin 
1288831f5dcfSAlexander Motin 	switch (which) {
1289831f5dcfSAlexander Motin 	default:
1290831f5dcfSAlexander Motin 		return (EINVAL);
1291831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
1292831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
1293831f5dcfSAlexander Motin 		break;
1294831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
1295831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
1296831f5dcfSAlexander Motin 		break;
1297831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
1298831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
1299831f5dcfSAlexander Motin 		break;
1300831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
1301831f5dcfSAlexander Motin 		if (value > 0) {
1302831f5dcfSAlexander Motin 			uint32_t clock = slot->max_clk;
1303831f5dcfSAlexander Motin 			int i;
1304831f5dcfSAlexander Motin 
1305831f5dcfSAlexander Motin 			for (i = 0; i < 8; i++) {
1306831f5dcfSAlexander Motin 				if (clock <= value)
1307831f5dcfSAlexander Motin 					break;
1308831f5dcfSAlexander Motin 				clock >>= 1;
1309831f5dcfSAlexander Motin 			}
1310831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
1311831f5dcfSAlexander Motin 		} else
1312831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
1313831f5dcfSAlexander Motin 		break;
1314831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
1315831f5dcfSAlexander Motin 		slot->host.mode = value;
1316831f5dcfSAlexander Motin 		break;
1317831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
1318831f5dcfSAlexander Motin 		slot->host.ocr = value;
1319831f5dcfSAlexander Motin 		break;
1320831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
1321831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
1322831f5dcfSAlexander Motin 		break;
1323831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
1324831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
1325831f5dcfSAlexander Motin 		break;
1326831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
1327831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
1328831f5dcfSAlexander Motin 		break;
1329831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
1330831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
1331831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
1332831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
13333a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
1334831f5dcfSAlexander Motin 		return (EINVAL);
1335831f5dcfSAlexander Motin 	}
1336831f5dcfSAlexander Motin 	return (0);
1337831f5dcfSAlexander Motin }
1338831f5dcfSAlexander Motin 
1339d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1);
1340