xref: /freebsd/sys/dev/sdhci/sdhci.c (revision a94a63f0a6bc1ec25bf0d162e1dd9a53e020d176)
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 
51*a94a63f0SWarner Losh #include <cam/cam.h>
52*a94a63f0SWarner Losh #include <cam/cam_ccb.h>
53*a94a63f0SWarner Losh #include <cam/cam_debug.h>
54*a94a63f0SWarner Losh #include <cam/cam_sim.h>
55*a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h>
56*a94a63f0SWarner Losh 
57831f5dcfSAlexander Motin #include "mmcbr_if.h"
58831f5dcfSAlexander Motin #include "sdhci.h"
59d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h"
60831f5dcfSAlexander Motin 
61*a94a63f0SWarner Losh #include "opt_mmccam.h"
62*a94a63f0SWarner Losh 
63f0d2731dSMarius Strobl SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver");
64831f5dcfSAlexander Motin 
65*a94a63f0SWarner Losh static int sdhci_debug = 0;
667e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0,
677e6ccea3SMarius Strobl     "Debug level");
680f34084fSMarius Strobl u_int sdhci_quirk_clear = 0;
690f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear,
700f34084fSMarius Strobl     0, "Mask of quirks to clear");
710f34084fSMarius Strobl u_int sdhci_quirk_set = 0;
720f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0,
730f34084fSMarius Strobl     "Mask of quirks to set");
745b69a497SAlexander Motin 
75d6b3aaf8SOleksandr Tymoshenko #define	RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
76d6b3aaf8SOleksandr Tymoshenko #define	RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
77d6b3aaf8SOleksandr Tymoshenko #define	RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
78d6b3aaf8SOleksandr Tymoshenko #define	RD_MULTI_4(slot, off, ptr, count)	\
79d6b3aaf8SOleksandr Tymoshenko     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
80831f5dcfSAlexander Motin 
81d6b3aaf8SOleksandr Tymoshenko #define	WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
82d6b3aaf8SOleksandr Tymoshenko #define	WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
83d6b3aaf8SOleksandr Tymoshenko #define	WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
84d6b3aaf8SOleksandr Tymoshenko #define	WR_MULTI_4(slot, off, ptr, count)	\
85d6b3aaf8SOleksandr Tymoshenko     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
86831f5dcfSAlexander Motin 
87831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
88831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot);
89831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data);
90831f5dcfSAlexander Motin 
91639f59f0SIan Lepore static void sdhci_card_poll(void *);
92831f5dcfSAlexander Motin static void sdhci_card_task(void *, int);
93831f5dcfSAlexander Motin 
94*a94a63f0SWarner Losh /* CAM-related */
95*a94a63f0SWarner Losh int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock);
96*a94a63f0SWarner Losh static int sdhci_cam_update_ios(struct sdhci_slot *slot);
97*a94a63f0SWarner Losh static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb);
98*a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb);
99*a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim);
100*a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb);
101*a94a63f0SWarner Losh 
102831f5dcfSAlexander Motin /* helper routines */
1030f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot);
1040f34084fSMarius Strobl static int slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
1050f34084fSMarius Strobl     __printflike(2, 3);
1060f34084fSMarius Strobl 
107831f5dcfSAlexander Motin #define	SDHCI_LOCK(_slot)		mtx_lock(&(_slot)->mtx)
108831f5dcfSAlexander Motin #define	SDHCI_UNLOCK(_slot)		mtx_unlock(&(_slot)->mtx)
109831f5dcfSAlexander Motin #define	SDHCI_LOCK_INIT(_slot) \
110831f5dcfSAlexander Motin 	mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
111831f5dcfSAlexander Motin #define	SDHCI_LOCK_DESTROY(_slot)	mtx_destroy(&_slot->mtx);
112831f5dcfSAlexander Motin #define	SDHCI_ASSERT_LOCKED(_slot)	mtx_assert(&_slot->mtx, MA_OWNED);
113831f5dcfSAlexander Motin #define	SDHCI_ASSERT_UNLOCKED(_slot)	mtx_assert(&_slot->mtx, MA_NOTOWNED);
114831f5dcfSAlexander Motin 
11533aad34dSOleksandr Tymoshenko #define	SDHCI_DEFAULT_MAX_FREQ	50
11633aad34dSOleksandr Tymoshenko 
11757677a3aSOleksandr Tymoshenko #define	SDHCI_200_MAX_DIVIDER	256
11857677a3aSOleksandr Tymoshenko #define	SDHCI_300_MAX_DIVIDER	2046
11957677a3aSOleksandr Tymoshenko 
120639f59f0SIan Lepore #define	SDHCI_CARD_PRESENT_TICKS	(hz / 5)
121639f59f0SIan Lepore #define	SDHCI_INSERT_DELAY_TICKS	(hz / 2)
122639f59f0SIan Lepore 
12393efdc63SAdrian Chadd /*
12493efdc63SAdrian Chadd  * Broadcom BCM577xx Controller Constants
12593efdc63SAdrian Chadd  */
1261bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */
1271bacf3beSMarius Strobl #define	BCM577XX_DEFAULT_MAX_DIVIDER	256
1281bacf3beSMarius Strobl /* Alternative clock's base frequency. */
1291bacf3beSMarius Strobl #define	BCM577XX_ALT_CLOCK_BASE		63000000
13093efdc63SAdrian Chadd 
13193efdc63SAdrian Chadd #define	BCM577XX_HOST_CONTROL		0x198
13293efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_MASK	0xFFFFCFFF
13393efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_SHIFT	12
13493efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_DEFAULT	0x0
13593efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_64MHZ	0x3
13693efdc63SAdrian Chadd 
137831f5dcfSAlexander Motin static void
138831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
139831f5dcfSAlexander Motin {
1407e6ccea3SMarius Strobl 
141831f5dcfSAlexander Motin 	if (error != 0) {
142831f5dcfSAlexander Motin 		printf("getaddr: error %d\n", error);
143831f5dcfSAlexander Motin 		return;
144831f5dcfSAlexander Motin 	}
145831f5dcfSAlexander Motin 	*(bus_addr_t *)arg = segs[0].ds_addr;
146831f5dcfSAlexander Motin }
147831f5dcfSAlexander Motin 
148d6b3aaf8SOleksandr Tymoshenko static int
149d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
150d6b3aaf8SOleksandr Tymoshenko {
151d6b3aaf8SOleksandr Tymoshenko 	va_list ap;
152d6b3aaf8SOleksandr Tymoshenko 	int retval;
153d6b3aaf8SOleksandr Tymoshenko 
154d6b3aaf8SOleksandr Tymoshenko 	retval = printf("%s-slot%d: ",
155d6b3aaf8SOleksandr Tymoshenko 	    device_get_nameunit(slot->bus), slot->num);
156d6b3aaf8SOleksandr Tymoshenko 
157d6b3aaf8SOleksandr Tymoshenko 	va_start(ap, fmt);
158d6b3aaf8SOleksandr Tymoshenko 	retval += vprintf(fmt, ap);
159d6b3aaf8SOleksandr Tymoshenko 	va_end(ap);
160d6b3aaf8SOleksandr Tymoshenko 	return (retval);
161d6b3aaf8SOleksandr Tymoshenko }
162d6b3aaf8SOleksandr Tymoshenko 
163831f5dcfSAlexander Motin static void
164831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot)
165831f5dcfSAlexander Motin {
1667e6ccea3SMarius Strobl 
167831f5dcfSAlexander Motin 	slot_printf(slot,
168831f5dcfSAlexander Motin 	    "============== REGISTER DUMP ==============\n");
169831f5dcfSAlexander Motin 
170831f5dcfSAlexander Motin 	slot_printf(slot, "Sys addr: 0x%08x | Version:  0x%08x\n",
171831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
172831f5dcfSAlexander Motin 	slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
173831f5dcfSAlexander Motin 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
174831f5dcfSAlexander Motin 	slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
175831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
176831f5dcfSAlexander Motin 	slot_printf(slot, "Present:  0x%08x | Host ctl: 0x%08x\n",
177831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
178831f5dcfSAlexander Motin 	slot_printf(slot, "Power:    0x%08x | Blk gap:  0x%08x\n",
179831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
180831f5dcfSAlexander Motin 	slot_printf(slot, "Wake-up:  0x%08x | Clock:    0x%08x\n",
181831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
182831f5dcfSAlexander Motin 	slot_printf(slot, "Timeout:  0x%08x | Int stat: 0x%08x\n",
183831f5dcfSAlexander Motin 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
184831f5dcfSAlexander Motin 	slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
185831f5dcfSAlexander Motin 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
1869dbf8c46SMarius Strobl 	slot_printf(slot, "AC12 err: 0x%08x | Host ctl2: 0x%08x\n",
1879dbf8c46SMarius Strobl 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2));
1889dbf8c46SMarius Strobl 	slot_printf(slot, "Caps:     0x%08x | Caps2:    0x%08x\n",
1899dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2));
1909dbf8c46SMarius Strobl 	slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n",
1919dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR));
1929dbf8c46SMarius Strobl 	slot_printf(slot, "ADMA addr: 0x%08x | Slot int: 0x%08x\n",
1939dbf8c46SMarius Strobl 	    RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS));
194831f5dcfSAlexander Motin 
195831f5dcfSAlexander Motin 	slot_printf(slot,
196831f5dcfSAlexander Motin 	    "===========================================\n");
197831f5dcfSAlexander Motin }
198831f5dcfSAlexander Motin 
199831f5dcfSAlexander Motin static void
200831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
201831f5dcfSAlexander Motin {
202831f5dcfSAlexander Motin 	int timeout;
203b440e965SMarius Strobl 	uint32_t clock;
204831f5dcfSAlexander Motin 
205d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
2066e37fb2bSIan Lepore 		if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
207831f5dcfSAlexander Motin 			return;
208831f5dcfSAlexander Motin 	}
209831f5dcfSAlexander Motin 
210831f5dcfSAlexander Motin 	/* Some controllers need this kick or reset won't work. */
211831f5dcfSAlexander Motin 	if ((mask & SDHCI_RESET_ALL) == 0 &&
212d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
213831f5dcfSAlexander Motin 		/* This is to force an update */
214831f5dcfSAlexander Motin 		clock = slot->clock;
215831f5dcfSAlexander Motin 		slot->clock = 0;
216831f5dcfSAlexander Motin 		sdhci_set_clock(slot, clock);
217831f5dcfSAlexander Motin 	}
218831f5dcfSAlexander Motin 
219d8208d9eSAlexander Motin 	if (mask & SDHCI_RESET_ALL) {
220831f5dcfSAlexander Motin 		slot->clock = 0;
221d8208d9eSAlexander Motin 		slot->power = 0;
222d8208d9eSAlexander Motin 	}
223831f5dcfSAlexander Motin 
22461bc42f7SIan Lepore 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
22561bc42f7SIan Lepore 
22661bc42f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
22761bc42f7SIan Lepore 		/*
22861bc42f7SIan Lepore 		 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
22961bc42f7SIan Lepore 		 * specification.  The reset bit has internal propagation delay,
23061bc42f7SIan Lepore 		 * so a fast read after write returns 0 even if reset process is
23161bc42f7SIan Lepore 		 * in progress.  The workaround is to poll for 1 before polling
23261bc42f7SIan Lepore 		 * for 0.  In the worst case, if we miss seeing it asserted the
23361bc42f7SIan Lepore 		 * time we spent waiting is enough to ensure the reset finishes.
23461bc42f7SIan Lepore 		 */
23561bc42f7SIan Lepore 		timeout = 10000;
23661bc42f7SIan Lepore 		while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
23761bc42f7SIan Lepore 			if (timeout <= 0)
23861bc42f7SIan Lepore 				break;
23961bc42f7SIan Lepore 			timeout--;
24061bc42f7SIan Lepore 			DELAY(1);
24161bc42f7SIan Lepore 		}
24261bc42f7SIan Lepore 	}
24361bc42f7SIan Lepore 
244831f5dcfSAlexander Motin 	/* Wait max 100 ms */
24561bc42f7SIan Lepore 	timeout = 10000;
246831f5dcfSAlexander Motin 	/* Controller clears the bits when it's done */
24761bc42f7SIan Lepore 	while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
24861bc42f7SIan Lepore 		if (timeout <= 0) {
24961bc42f7SIan Lepore 			slot_printf(slot, "Reset 0x%x never completed.\n",
25061bc42f7SIan Lepore 			    mask);
251831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
252831f5dcfSAlexander Motin 			return;
253831f5dcfSAlexander Motin 		}
254831f5dcfSAlexander Motin 		timeout--;
25561bc42f7SIan Lepore 		DELAY(10);
256831f5dcfSAlexander Motin 	}
257831f5dcfSAlexander Motin }
258831f5dcfSAlexander Motin 
259831f5dcfSAlexander Motin static void
260831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot)
261831f5dcfSAlexander Motin {
262831f5dcfSAlexander Motin 
263831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
264831f5dcfSAlexander Motin 
265831f5dcfSAlexander Motin 	/* Enable interrupts. */
266831f5dcfSAlexander Motin 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
267831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
268831f5dcfSAlexander Motin 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
269831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
270831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
271*a94a63f0SWarner Losh 	    SDHCI_INT_ACMD12ERR | SDHCI_INT_CARD_INT;
272639f59f0SIan Lepore 
273639f59f0SIan Lepore 	if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
274639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
275639f59f0SIan Lepore 		slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
276639f59f0SIan Lepore 	}
277639f59f0SIan Lepore 
278831f5dcfSAlexander Motin 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
279831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
280831f5dcfSAlexander Motin }
281831f5dcfSAlexander Motin 
282831f5dcfSAlexander Motin static void
283831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
284831f5dcfSAlexander Motin {
28593efdc63SAdrian Chadd 	uint32_t clk_base;
28693efdc63SAdrian Chadd 	uint32_t clk_sel;
287831f5dcfSAlexander Motin 	uint32_t res;
288831f5dcfSAlexander Motin 	uint16_t clk;
2898f3b7d56SOleksandr Tymoshenko 	uint16_t div;
290831f5dcfSAlexander Motin 	int timeout;
291831f5dcfSAlexander Motin 
292831f5dcfSAlexander Motin 	if (clock == slot->clock)
293831f5dcfSAlexander Motin 		return;
294831f5dcfSAlexander Motin 	slot->clock = clock;
295831f5dcfSAlexander Motin 
296831f5dcfSAlexander Motin 	/* Turn off the clock. */
2974ddc0172SIan Lepore 	clk = RD2(slot, SDHCI_CLOCK_CONTROL);
2984ddc0172SIan Lepore 	WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
299b440e965SMarius Strobl 	/* If no clock requested - leave it so. */
300831f5dcfSAlexander Motin 	if (clock == 0)
301831f5dcfSAlexander Motin 		return;
302ceb9e9f7SIan Lepore 
30393efdc63SAdrian Chadd 	/* Determine the clock base frequency */
30493efdc63SAdrian Chadd 	clk_base = slot->max_clk;
30593efdc63SAdrian Chadd 	if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
3061bacf3beSMarius Strobl 		clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) &
3071bacf3beSMarius Strobl 		    BCM577XX_CTRL_CLKSEL_MASK;
30893efdc63SAdrian Chadd 
3091bacf3beSMarius Strobl 		/*
3101bacf3beSMarius Strobl 		 * Select clock source appropriate for the requested frequency.
3111bacf3beSMarius Strobl 		 */
31293efdc63SAdrian Chadd 		if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
31393efdc63SAdrian Chadd 			clk_base = BCM577XX_ALT_CLOCK_BASE;
3141bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ <<
3151bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
31693efdc63SAdrian Chadd 		} else {
3171bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT <<
3181bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
31993efdc63SAdrian Chadd 		}
32093efdc63SAdrian Chadd 
32193efdc63SAdrian Chadd 		WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
32293efdc63SAdrian Chadd 	}
32393efdc63SAdrian Chadd 
324ceb9e9f7SIan Lepore 	/* Recalculate timeout clock frequency based on the new sd clock. */
325ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
326ceb9e9f7SIan Lepore 		slot->timeout_clk = slot->clock / 1000;
327ceb9e9f7SIan Lepore 
3288f3b7d56SOleksandr Tymoshenko 	if (slot->version < SDHCI_SPEC_300) {
329831f5dcfSAlexander Motin 		/* Looking for highest freq <= clock. */
33093efdc63SAdrian Chadd 		res = clk_base;
33157677a3aSOleksandr Tymoshenko 		for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
332831f5dcfSAlexander Motin 			if (res <= clock)
333831f5dcfSAlexander Motin 				break;
334831f5dcfSAlexander Motin 			res >>= 1;
335831f5dcfSAlexander Motin 		}
336831f5dcfSAlexander Motin 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
3378f3b7d56SOleksandr Tymoshenko 		div >>= 1;
338c11bbc7dSMarius Strobl 	} else {
3398f3b7d56SOleksandr Tymoshenko 		/* Version 3.0 divisors are multiples of two up to 1023 * 2 */
34093efdc63SAdrian Chadd 		if (clock >= clk_base)
34157677a3aSOleksandr Tymoshenko 			div = 0;
3428f3b7d56SOleksandr Tymoshenko 		else {
34357677a3aSOleksandr Tymoshenko 			for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
34493efdc63SAdrian Chadd 				if ((clk_base / div) <= clock)
3458f3b7d56SOleksandr Tymoshenko 					break;
3468f3b7d56SOleksandr Tymoshenko 			}
3478f3b7d56SOleksandr Tymoshenko 		}
3488f3b7d56SOleksandr Tymoshenko 		div >>= 1;
3498f3b7d56SOleksandr Tymoshenko 	}
3508f3b7d56SOleksandr Tymoshenko 
3518f3b7d56SOleksandr Tymoshenko 	if (bootverbose || sdhci_debug)
35293efdc63SAdrian Chadd 		slot_printf(slot, "Divider %d for freq %d (base %d)\n",
35393efdc63SAdrian Chadd 			div, clock, clk_base);
3548f3b7d56SOleksandr Tymoshenko 
355831f5dcfSAlexander Motin 	/* Now we have got divider, set it. */
3568f3b7d56SOleksandr Tymoshenko 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
3578f3b7d56SOleksandr Tymoshenko 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
3588f3b7d56SOleksandr Tymoshenko 		<< SDHCI_DIVIDER_HI_SHIFT;
3598f3b7d56SOleksandr Tymoshenko 
360831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
361831f5dcfSAlexander Motin 	/* Enable clock. */
362831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_INT_EN;
363831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
364831f5dcfSAlexander Motin 	/* Wait up to 10 ms until it stabilize. */
365831f5dcfSAlexander Motin 	timeout = 10;
366831f5dcfSAlexander Motin 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
367831f5dcfSAlexander Motin 		& SDHCI_CLOCK_INT_STABLE)) {
368831f5dcfSAlexander Motin 		if (timeout == 0) {
369831f5dcfSAlexander Motin 			slot_printf(slot,
370831f5dcfSAlexander Motin 			    "Internal clock never stabilised.\n");
371831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
372831f5dcfSAlexander Motin 			return;
373831f5dcfSAlexander Motin 		}
374831f5dcfSAlexander Motin 		timeout--;
375831f5dcfSAlexander Motin 		DELAY(1000);
376831f5dcfSAlexander Motin 	}
377831f5dcfSAlexander Motin 	/* Pass clock signal to the bus. */
378831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_CARD_EN;
379831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
380831f5dcfSAlexander Motin }
381831f5dcfSAlexander Motin 
382831f5dcfSAlexander Motin static void
383831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power)
384831f5dcfSAlexander Motin {
38585083a80SMarius Strobl 	int i;
386831f5dcfSAlexander Motin 	uint8_t pwr;
387831f5dcfSAlexander Motin 
388831f5dcfSAlexander Motin 	if (slot->power == power)
389831f5dcfSAlexander Motin 		return;
390d6b3aaf8SOleksandr Tymoshenko 
391831f5dcfSAlexander Motin 	slot->power = power;
392831f5dcfSAlexander Motin 
393831f5dcfSAlexander Motin 	/* Turn off the power. */
394831f5dcfSAlexander Motin 	pwr = 0;
395831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
396b440e965SMarius Strobl 	/* If power down requested - leave it so. */
397831f5dcfSAlexander Motin 	if (power == 0)
398831f5dcfSAlexander Motin 		return;
399831f5dcfSAlexander Motin 	/* Set voltage. */
400831f5dcfSAlexander Motin 	switch (1 << power) {
401831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
402831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
403831f5dcfSAlexander Motin 		break;
404831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
405831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
406831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
407831f5dcfSAlexander Motin 		break;
408831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
409831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
410831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
411831f5dcfSAlexander Motin 		break;
412831f5dcfSAlexander Motin 	}
413831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
41485083a80SMarius Strobl 	/*
41585083a80SMarius Strobl 	 * Turn on VDD1 power.  Note that at least some Intel controllers can
41685083a80SMarius Strobl 	 * fail to enable bus power on the first try after transiting from D3
4178022c8ebSMarius Strobl 	 * to D0, so we give them up to 2 ms.
41885083a80SMarius Strobl 	 */
419831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
42085083a80SMarius Strobl 	for (i = 0; i < 20; i++) {
421831f5dcfSAlexander Motin 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
42285083a80SMarius Strobl 		if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)
42385083a80SMarius Strobl 			break;
42485083a80SMarius Strobl 		DELAY(100);
42585083a80SMarius Strobl 	}
42685083a80SMarius Strobl 	if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON))
42785083a80SMarius Strobl 		slot_printf(slot, "Bus power failed to enable");
428a2832f9fSMarius Strobl 
429a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) {
430a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10);
431a2832f9fSMarius Strobl 		DELAY(10);
432a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
433a2832f9fSMarius Strobl 		DELAY(300);
434a2832f9fSMarius Strobl 	}
435831f5dcfSAlexander Motin }
436831f5dcfSAlexander Motin 
437831f5dcfSAlexander Motin static void
438831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
439831f5dcfSAlexander Motin {
440831f5dcfSAlexander Motin 	uint32_t data;
441831f5dcfSAlexander Motin 	char *buffer;
442831f5dcfSAlexander Motin 	size_t left;
443831f5dcfSAlexander Motin 
444831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
445831f5dcfSAlexander Motin 	buffer += slot->offset;
446831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
447831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
448831f5dcfSAlexander Motin 	slot->offset += left;
449831f5dcfSAlexander Motin 
450831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
451d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
452831f5dcfSAlexander Motin 		DELAY(10);
453ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
454831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
455831f5dcfSAlexander Motin 		while (left > 3) {
456831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
457831f5dcfSAlexander Motin 			buffer[0] = data;
458831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
459831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
460831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
461831f5dcfSAlexander Motin 			buffer += 4;
462831f5dcfSAlexander Motin 			left -= 4;
463831f5dcfSAlexander Motin 		}
464831f5dcfSAlexander Motin 	} else {
465d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
466831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
467831f5dcfSAlexander Motin 		left &= 3;
468831f5dcfSAlexander Motin 	}
469831f5dcfSAlexander Motin 	/* Handle uneven size case. */
470831f5dcfSAlexander Motin 	if (left > 0) {
471831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
472831f5dcfSAlexander Motin 		while (left > 0) {
473831f5dcfSAlexander Motin 			*(buffer++) = data;
474831f5dcfSAlexander Motin 			data >>= 8;
475831f5dcfSAlexander Motin 			left--;
476831f5dcfSAlexander Motin 		}
477831f5dcfSAlexander Motin 	}
478831f5dcfSAlexander Motin }
479831f5dcfSAlexander Motin 
480831f5dcfSAlexander Motin static void
481831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
482831f5dcfSAlexander Motin {
483831f5dcfSAlexander Motin 	uint32_t data = 0;
484831f5dcfSAlexander Motin 	char *buffer;
485831f5dcfSAlexander Motin 	size_t left;
486831f5dcfSAlexander Motin 
487831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
488831f5dcfSAlexander Motin 	buffer += slot->offset;
489831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
490831f5dcfSAlexander Motin 	left = min(512, slot->curcmd->data->len - slot->offset);
491831f5dcfSAlexander Motin 	slot->offset += left;
492831f5dcfSAlexander Motin 
493ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
494831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
495831f5dcfSAlexander Motin 		while (left > 3) {
496831f5dcfSAlexander Motin 			data = buffer[0] +
497831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
498831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
499831f5dcfSAlexander Motin 			    (buffer[3] << 24);
500831f5dcfSAlexander Motin 			left -= 4;
501831f5dcfSAlexander Motin 			buffer += 4;
502831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
503831f5dcfSAlexander Motin 		}
504831f5dcfSAlexander Motin 	} else {
505d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
506831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
507831f5dcfSAlexander Motin 		left &= 3;
508831f5dcfSAlexander Motin 	}
509831f5dcfSAlexander Motin 	/* Handle uneven size case. */
510831f5dcfSAlexander Motin 	if (left > 0) {
511831f5dcfSAlexander Motin 		while (left > 0) {
512831f5dcfSAlexander Motin 			data <<= 8;
513831f5dcfSAlexander Motin 			data += *(buffer++);
514831f5dcfSAlexander Motin 			left--;
515831f5dcfSAlexander Motin 		}
516831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
517831f5dcfSAlexander Motin 	}
518831f5dcfSAlexander Motin }
519831f5dcfSAlexander Motin 
520831f5dcfSAlexander Motin static void
521831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
522831f5dcfSAlexander Motin {
523831f5dcfSAlexander Motin 
524831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
525831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
526831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
527831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
528831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
529831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
530831f5dcfSAlexander Motin 				break;
531831f5dcfSAlexander Motin 		}
532831f5dcfSAlexander Motin 	} else {
533831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
534831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
535831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
536831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
537831f5dcfSAlexander Motin 				break;
538831f5dcfSAlexander Motin 		}
539831f5dcfSAlexander Motin 	}
540831f5dcfSAlexander Motin }
541831f5dcfSAlexander Motin 
542831f5dcfSAlexander Motin static void
5437e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused)
544831f5dcfSAlexander Motin {
545831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
5467e6ccea3SMarius Strobl 	device_t d;
547831f5dcfSAlexander Motin 
548831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
5496e37fb2bSIan Lepore 	if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
550*a94a63f0SWarner Losh #ifdef MMCCAM
551*a94a63f0SWarner Losh 		if (slot->card_present == 0) {
552*a94a63f0SWarner Losh #else
553831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
554*a94a63f0SWarner Losh #endif
555831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
556639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
557639f59f0SIan Lepore 				slot_printf(slot, "Card inserted\n");
558*a94a63f0SWarner Losh #ifdef MMCCAM
559*a94a63f0SWarner Losh 			slot->card_present = 1;
560*a94a63f0SWarner Losh 			union ccb *ccb;
561*a94a63f0SWarner Losh 			uint32_t pathid;
562*a94a63f0SWarner Losh 			pathid = cam_sim_path(slot->sim);
563*a94a63f0SWarner Losh 			ccb = xpt_alloc_ccb_nowait();
564*a94a63f0SWarner Losh 			if (ccb == NULL) {
565*a94a63f0SWarner Losh 				slot_printf(slot, "Unable to alloc CCB for rescan\n");
566*a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
567*a94a63f0SWarner Losh 				return;
568*a94a63f0SWarner Losh 			}
569*a94a63f0SWarner Losh 
570*a94a63f0SWarner Losh 			/*
571*a94a63f0SWarner Losh 			 * We create a rescan request for BUS:0:0, since the card
572*a94a63f0SWarner Losh 			 * will be at lun 0.
573*a94a63f0SWarner Losh 			 */
574*a94a63f0SWarner Losh 			if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
575*a94a63f0SWarner Losh 					    /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
576*a94a63f0SWarner Losh 				slot_printf(slot, "Unable to create path for rescan\n");
577*a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
578*a94a63f0SWarner Losh 				xpt_free_ccb(ccb);
579*a94a63f0SWarner Losh 				return;
580*a94a63f0SWarner Losh 			}
581*a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
582*a94a63f0SWarner Losh 			xpt_rescan(ccb);
583*a94a63f0SWarner Losh #else
584d6b3aaf8SOleksandr Tymoshenko 			slot->dev = device_add_child(slot->bus, "mmc", -1);
585831f5dcfSAlexander Motin 			device_set_ivars(slot->dev, slot);
586831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
587831f5dcfSAlexander Motin 			device_probe_and_attach(slot->dev);
588*a94a63f0SWarner Losh #endif
589831f5dcfSAlexander Motin 		} else
590831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
591831f5dcfSAlexander Motin 	} else {
592*a94a63f0SWarner Losh #ifdef MMCCAM
593*a94a63f0SWarner Losh 		if (slot->card_present == 1) {
594*a94a63f0SWarner Losh #else
595831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
596*a94a63f0SWarner Losh #endif
597831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
598639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
599639f59f0SIan Lepore 				slot_printf(slot, "Card removed\n");
6007e6ccea3SMarius Strobl 			d = slot->dev;
601831f5dcfSAlexander Motin 			slot->dev = NULL;
602*a94a63f0SWarner Losh #ifdef MMCCAM
603*a94a63f0SWarner Losh 			slot->card_present = 0;
604*a94a63f0SWarner Losh 			union ccb *ccb;
605*a94a63f0SWarner Losh 			uint32_t pathid;
606*a94a63f0SWarner Losh 			pathid = cam_sim_path(slot->sim);
607*a94a63f0SWarner Losh 			ccb = xpt_alloc_ccb_nowait();
608*a94a63f0SWarner Losh 			if (ccb == NULL) {
609*a94a63f0SWarner Losh 				slot_printf(slot, "Unable to alloc CCB for rescan\n");
610*a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
611*a94a63f0SWarner Losh 				return;
612*a94a63f0SWarner Losh 			}
613*a94a63f0SWarner Losh 
614*a94a63f0SWarner Losh 			/*
615*a94a63f0SWarner Losh 			 * We create a rescan request for BUS:0:0, since the card
616*a94a63f0SWarner Losh 			 * will be at lun 0.
617*a94a63f0SWarner Losh 			 */
618*a94a63f0SWarner Losh 			if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
619*a94a63f0SWarner Losh 					    /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) {
620*a94a63f0SWarner Losh 				slot_printf(slot, "Unable to create path for rescan\n");
621*a94a63f0SWarner Losh 				SDHCI_UNLOCK(slot);
622*a94a63f0SWarner Losh 				xpt_free_ccb(ccb);
623*a94a63f0SWarner Losh 				return;
624*a94a63f0SWarner Losh 			}
625*a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
626*a94a63f0SWarner Losh 			xpt_rescan(ccb);
627*a94a63f0SWarner Losh #else
628831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
629d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
630*a94a63f0SWarner Losh #endif
631831f5dcfSAlexander Motin 		} else
632831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
633831f5dcfSAlexander Motin 	}
634831f5dcfSAlexander Motin }
635831f5dcfSAlexander Motin 
636b8bf08b1SIan Lepore static void
637b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
638639f59f0SIan Lepore {
639639f59f0SIan Lepore 	bool was_present;
640639f59f0SIan Lepore 
641639f59f0SIan Lepore 	/*
642639f59f0SIan Lepore 	 * If there was no card and now there is one, schedule the task to
643639f59f0SIan Lepore 	 * create the child device after a short delay.  The delay is to
644639f59f0SIan Lepore 	 * debounce the card insert (sometimes the card detect pin stabilizes
645639f59f0SIan Lepore 	 * before the other pins have made good contact).
646639f59f0SIan Lepore 	 *
647639f59f0SIan Lepore 	 * If there was a card present and now it's gone, immediately schedule
648639f59f0SIan Lepore 	 * the task to delete the child device.  No debouncing -- gone is gone,
649639f59f0SIan Lepore 	 * because once power is removed, a full card re-init is needed, and
650639f59f0SIan Lepore 	 * that happens by deleting and recreating the child device.
651639f59f0SIan Lepore 	 */
652*a94a63f0SWarner Losh #ifdef MMCCAM
653*a94a63f0SWarner Losh 	was_present = slot->card_present;
654*a94a63f0SWarner Losh #else
655639f59f0SIan Lepore 	was_present = slot->dev != NULL;
656*a94a63f0SWarner Losh #endif
657639f59f0SIan Lepore 	if (!was_present && is_present) {
658639f59f0SIan Lepore 		taskqueue_enqueue_timeout(taskqueue_swi_giant,
659639f59f0SIan Lepore 		    &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
660639f59f0SIan Lepore 	} else if (was_present && !is_present) {
661639f59f0SIan Lepore 		taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
662639f59f0SIan Lepore 	}
663b8bf08b1SIan Lepore }
664b8bf08b1SIan Lepore 
665b8bf08b1SIan Lepore void
666b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
667b8bf08b1SIan Lepore {
668b8bf08b1SIan Lepore 
669b8bf08b1SIan Lepore 	SDHCI_LOCK(slot);
670b8bf08b1SIan Lepore 	sdhci_handle_card_present_locked(slot, is_present);
671639f59f0SIan Lepore 	SDHCI_UNLOCK(slot);
672639f59f0SIan Lepore }
673639f59f0SIan Lepore 
674639f59f0SIan Lepore static void
675639f59f0SIan Lepore sdhci_card_poll(void *arg)
676639f59f0SIan Lepore {
677639f59f0SIan Lepore 	struct sdhci_slot *slot = arg;
678639f59f0SIan Lepore 
679639f59f0SIan Lepore 	sdhci_handle_card_present(slot,
680639f59f0SIan Lepore 	    SDHCI_GET_CARD_PRESENT(slot->bus, slot));
681639f59f0SIan Lepore 	callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
682639f59f0SIan Lepore 	    sdhci_card_poll, slot);
683639f59f0SIan Lepore }
684639f59f0SIan Lepore 
685d6b3aaf8SOleksandr Tymoshenko int
686d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
687831f5dcfSAlexander Motin {
6880f34084fSMarius Strobl 	uint32_t caps, caps2, freq, host_caps;
689d6b3aaf8SOleksandr Tymoshenko 	int err;
690831f5dcfSAlexander Motin 
691831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
692*a94a63f0SWarner Losh 
693d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
694d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
695d6b3aaf8SOleksandr Tymoshenko 
696831f5dcfSAlexander Motin 	/* Allocate DMA tag. */
697831f5dcfSAlexander Motin 	err = bus_dma_tag_create(bus_get_dma_tag(dev),
698831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
699831f5dcfSAlexander Motin 	    BUS_SPACE_MAXADDR, NULL, NULL,
700831f5dcfSAlexander Motin 	    DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE,
701831f5dcfSAlexander Motin 	    BUS_DMA_ALLOCNOW, NULL, NULL,
702831f5dcfSAlexander Motin 	    &slot->dmatag);
703831f5dcfSAlexander Motin 	if (err != 0) {
704831f5dcfSAlexander Motin 		device_printf(dev, "Can't create DMA tag\n");
705831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
706d6b3aaf8SOleksandr Tymoshenko 		return (err);
707831f5dcfSAlexander Motin 	}
708831f5dcfSAlexander Motin 	/* Allocate DMA memory. */
709831f5dcfSAlexander Motin 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
710831f5dcfSAlexander Motin 	    BUS_DMA_NOWAIT, &slot->dmamap);
711831f5dcfSAlexander Motin 	if (err != 0) {
712831f5dcfSAlexander Motin 		device_printf(dev, "Can't alloc DMA memory\n");
713831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
714d6b3aaf8SOleksandr Tymoshenko 		return (err);
715831f5dcfSAlexander Motin 	}
716831f5dcfSAlexander Motin 	/* Map the memory. */
717831f5dcfSAlexander Motin 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
718831f5dcfSAlexander Motin 	    (void *)slot->dmamem, DMA_BLOCK_SIZE,
719831f5dcfSAlexander Motin 	    sdhci_getaddr, &slot->paddr, 0);
720831f5dcfSAlexander Motin 	if (err != 0 || slot->paddr == 0) {
721831f5dcfSAlexander Motin 		device_printf(dev, "Can't load DMA memory\n");
722831f5dcfSAlexander Motin 		SDHCI_LOCK_DESTROY(slot);
723d6b3aaf8SOleksandr Tymoshenko 		if (err)
724d6b3aaf8SOleksandr Tymoshenko 			return (err);
725d6b3aaf8SOleksandr Tymoshenko 		else
726d6b3aaf8SOleksandr Tymoshenko 			return (EFAULT);
727831f5dcfSAlexander Motin 	}
728d6b3aaf8SOleksandr Tymoshenko 
729831f5dcfSAlexander Motin 	/* Initialize slot. */
730831f5dcfSAlexander Motin 	sdhci_init(slot);
731d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
732d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
7330f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) {
7348f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
7350f34084fSMarius Strobl 		caps2 = slot->caps2;
7360f34084fSMarius Strobl 	} else {
737831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
7380f34084fSMarius Strobl 		if (slot->version >= SDHCI_SPEC_300)
7390f34084fSMarius Strobl 			caps2 = RD4(slot, SDHCI_CAPABILITIES2);
7400f34084fSMarius Strobl 		else
7410f34084fSMarius Strobl 			caps2 = 0;
7420f34084fSMarius Strobl 	}
743831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
74433aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
74587a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
74687a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
74733aad34dSOleksandr Tymoshenko 	else
74887a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
74987a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
75087a6a871SIan Lepore 	if (freq != 0)
75187a6a871SIan Lepore 		slot->max_clk = freq * 1000000;
75287a6a871SIan Lepore 	/*
75387a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
75487a6a871SIan Lepore 	 * hasn't already set max_clk we're probably not going to work right
75587a6a871SIan Lepore 	 * with an assumption, so complain about it.
75687a6a871SIan Lepore 	 */
757831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
75887a6a871SIan Lepore 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
759831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify base clock "
7601bacf3beSMarius Strobl 		    "frequency, using %dMHz as default.\n",
7611bacf3beSMarius Strobl 		    SDHCI_DEFAULT_MAX_FREQ);
762831f5dcfSAlexander Motin 	}
763a2832f9fSMarius Strobl 	/* Calculate/set timeout clock frequency. */
7648f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
7658f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
766a2832f9fSMarius Strobl 	} else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) {
767a2832f9fSMarius Strobl 		slot->timeout_clk = 1000;
7688f3b7d56SOleksandr Tymoshenko 	} else {
7691bacf3beSMarius Strobl 		slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >>
7701bacf3beSMarius Strobl 		    SDHCI_TIMEOUT_CLK_SHIFT;
7718f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
7728f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
7738f3b7d56SOleksandr Tymoshenko 	}
77487a6a871SIan Lepore 	/*
77587a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
77687a6a871SIan Lepore 	 * hasn't already set timeout_clk we'll probably work okay using the
77787a6a871SIan Lepore 	 * max timeout, but still mention it.
77887a6a871SIan Lepore 	 */
779831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
780831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't specify timeout clock "
781ceb9e9f7SIan Lepore 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
782ceb9e9f7SIan Lepore 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
783831f5dcfSAlexander Motin 	}
784831f5dcfSAlexander Motin 
78557677a3aSOleksandr Tymoshenko 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
786831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
787831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
788831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
789831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
790831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
791831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
792831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_180)
793831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
794831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
795831f5dcfSAlexander Motin 		device_printf(dev, "Hardware doesn't report any "
796831f5dcfSAlexander Motin 		    "support voltages.\n");
797831f5dcfSAlexander Motin 	}
7980f34084fSMarius Strobl 	host_caps = MMC_CAP_4_BIT_DATA;
7992d1731b8SIan Lepore 	if (caps & SDHCI_CAN_DO_8BITBUS)
8000f34084fSMarius Strobl 		host_caps |= MMC_CAP_8_BIT_DATA;
801831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
8020f34084fSMarius Strobl 		host_caps |= MMC_CAP_HSPEED;
80372dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC)
8040f34084fSMarius Strobl 		host_caps |= MMC_CAP_BOOT_NOACC;
80572dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY)
8060f34084fSMarius Strobl 		host_caps |= MMC_CAP_WAIT_WHILE_BUSY;
8070f34084fSMarius Strobl 	if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50))
8080f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
8090f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_SDR104) {
8100f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
8110f34084fSMarius Strobl 		if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200))
8120f34084fSMarius Strobl 			host_caps |= MMC_CAP_MMC_HS200;
8130f34084fSMarius Strobl 	} else if (caps2 & SDHCI_CAN_SDR50)
8140f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR50;
8150f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_DDR50 &&
8160f34084fSMarius Strobl 	    !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50))
8170f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_DDR50;
8180f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_DDR52)
8190f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_DDR52;
8200f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 &&
8210f34084fSMarius Strobl 	    caps2 & SDHCI_CAN_MMC_HS400)
8220f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
8230f34084fSMarius Strobl 	host_caps |= MMC_CAP_SIGNALING_330;
8240f34084fSMarius Strobl 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
8250f34084fSMarius Strobl 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50 |
8260f34084fSMarius Strobl 	    MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 |
8270f34084fSMarius Strobl 	    MMC_CAP_MMC_HS400_180))
8280f34084fSMarius Strobl 		host_caps |= MMC_CAP_SIGNALING_180;
829f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_A)
8300f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_A;
831f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_C)
8320f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_C;
833f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_D)
8340f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_D;
8350f34084fSMarius Strobl 	slot->host.caps = host_caps;
8360f34084fSMarius Strobl 
837831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
838831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
839831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
840d6b3aaf8SOleksandr Tymoshenko 
841d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
842831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
843d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
844831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
845a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE)
846a2832f9fSMarius Strobl 		slot->opt |= SDHCI_NON_REMOVABLE;
847831f5dcfSAlexander Motin 
848c3a0f75aSOleksandr Tymoshenko 	/*
849c3a0f75aSOleksandr Tymoshenko 	 * Use platform-provided transfer backend
850c3a0f75aSOleksandr Tymoshenko 	 * with PIO as a fallback mechanism
851c3a0f75aSOleksandr Tymoshenko 	 */
852c3a0f75aSOleksandr Tymoshenko 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
853c3a0f75aSOleksandr Tymoshenko 		slot->opt &= ~SDHCI_HAVE_DMA;
854c3a0f75aSOleksandr Tymoshenko 
8555b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
8560f34084fSMarius Strobl 		slot_printf(slot,
8570f34084fSMarius Strobl 		    "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s\n",
858831f5dcfSAlexander Motin 		    slot->max_clk / 1000000,
859831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
8600f34084fSMarius Strobl 		    (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
8610f34084fSMarius Strobl 			((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
862831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
863831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
864831f5dcfSAlexander Motin 		    (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "",
8650f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "",
8660f34084fSMarius Strobl 		    (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "",
867f8b883c1SImre Vadász 		    (caps2 & SDHCI_CAN_DRIVE_TYPE_A) ? "A" : "",
868f8b883c1SImre Vadász 		    (caps2 & SDHCI_CAN_DRIVE_TYPE_C) ? "C" : "",
869f8b883c1SImre Vadász 		    (caps2 & SDHCI_CAN_DRIVE_TYPE_D) ? "D" : "",
870831f5dcfSAlexander Motin 		    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO");
8710f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 |
8720f34084fSMarius Strobl 		    MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE))
8730f34084fSMarius Strobl 			slot_printf(slot, "eMMC:%s%s%s%s\n",
8740f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "",
8750f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "",
8760f34084fSMarius Strobl 			    (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "",
8770f34084fSMarius Strobl 			    ((host_caps &
8780f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ==
8790f34084fSMarius Strobl 			    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ?
8800f34084fSMarius Strobl 			    " HS400ES" : "");
8810f34084fSMarius Strobl 		if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
8820f34084fSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104))
8830f34084fSMarius Strobl 			slot_printf(slot, "UHS-I:%s%s%s%s%s\n",
8840f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "",
8850f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "",
8860f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "",
8870f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "",
8880f34084fSMarius Strobl 			    (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : "");
889831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
890831f5dcfSAlexander Motin 	}
891831f5dcfSAlexander Motin 
892ba6fc1c7SLuiz Otavio O Souza 	slot->timeout = 10;
893ba6fc1c7SLuiz Otavio O Souza 	SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
894ba6fc1c7SLuiz Otavio O Souza 	    SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
895ba6fc1c7SLuiz Otavio O Souza 	    "timeout", CTLFLAG_RW, &slot->timeout, 0,
896ba6fc1c7SLuiz Otavio O Souza 	    "Maximum timeout for SDHCI transfers (in secs)");
897831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
898639f59f0SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
899639f59f0SIan Lepore 		sdhci_card_task, slot);
900639f59f0SIan Lepore 	callout_init(&slot->card_poll_callout, 1);
901e64f01a9SIan Lepore 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
902ba6fc1c7SLuiz Otavio O Souza 
903639f59f0SIan Lepore 	if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
904639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
905639f59f0SIan Lepore 		callout_reset(&slot->card_poll_callout,
906639f59f0SIan Lepore 		    SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
907639f59f0SIan Lepore 	}
908639f59f0SIan Lepore 
909831f5dcfSAlexander Motin 	return (0);
910831f5dcfSAlexander Motin }
911831f5dcfSAlexander Motin 
912d6b3aaf8SOleksandr Tymoshenko void
913d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
914831f5dcfSAlexander Motin {
9157e6ccea3SMarius Strobl 
916d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
917d6b3aaf8SOleksandr Tymoshenko }
918831f5dcfSAlexander Motin 
919d6b3aaf8SOleksandr Tymoshenko int
920d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
921d6b3aaf8SOleksandr Tymoshenko {
922831f5dcfSAlexander Motin 	device_t d;
923831f5dcfSAlexander Motin 
924e64f01a9SIan Lepore 	callout_drain(&slot->timeout_callout);
925639f59f0SIan Lepore 	callout_drain(&slot->card_poll_callout);
926831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
927639f59f0SIan Lepore 	taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
928831f5dcfSAlexander Motin 
929831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
930831f5dcfSAlexander Motin 	d = slot->dev;
931831f5dcfSAlexander Motin 	slot->dev = NULL;
932831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
933831f5dcfSAlexander Motin 	if (d != NULL)
934d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
935831f5dcfSAlexander Motin 
936831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
937831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
938831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
939831f5dcfSAlexander Motin 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
940831f5dcfSAlexander Motin 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
941831f5dcfSAlexander Motin 	bus_dma_tag_destroy(slot->dmatag);
942d6b3aaf8SOleksandr Tymoshenko 
943831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
944d6b3aaf8SOleksandr Tymoshenko 
945831f5dcfSAlexander Motin 	return (0);
946831f5dcfSAlexander Motin }
947831f5dcfSAlexander Motin 
948d6b3aaf8SOleksandr Tymoshenko int
949d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
95092bf0e27SAlexander Motin {
9517e6ccea3SMarius Strobl 
952d6b3aaf8SOleksandr Tymoshenko 	sdhci_reset(slot, SDHCI_RESET_ALL);
95392bf0e27SAlexander Motin 
95492bf0e27SAlexander Motin 	return (0);
95592bf0e27SAlexander Motin }
95692bf0e27SAlexander Motin 
957d6b3aaf8SOleksandr Tymoshenko int
958d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
95992bf0e27SAlexander Motin {
9607e6ccea3SMarius Strobl 
961d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
96292bf0e27SAlexander Motin 
963d6b3aaf8SOleksandr Tymoshenko 	return (0);
96492bf0e27SAlexander Motin }
96592bf0e27SAlexander Motin 
96657677a3aSOleksandr Tymoshenko uint32_t
967b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot)
96857677a3aSOleksandr Tymoshenko {
9697e6ccea3SMarius Strobl 
97057677a3aSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
97157677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
97257677a3aSOleksandr Tymoshenko 	else
97357677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
97457677a3aSOleksandr Tymoshenko }
97557677a3aSOleksandr Tymoshenko 
9766e37fb2bSIan Lepore bool
977b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot)
9786e37fb2bSIan Lepore {
9796e37fb2bSIan Lepore 
980639f59f0SIan Lepore 	if (slot->opt & SDHCI_NON_REMOVABLE)
981639f59f0SIan Lepore 		return true;
982639f59f0SIan Lepore 
9836e37fb2bSIan Lepore 	return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
9846e37fb2bSIan Lepore }
9856e37fb2bSIan Lepore 
9860f34084fSMarius Strobl void
9870f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot)
9880f34084fSMarius Strobl {
9890f34084fSMarius Strobl 	struct mmc_ios *ios;
9900f34084fSMarius Strobl 	uint16_t hostctrl2;
9910f34084fSMarius Strobl 
9920f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
9930f34084fSMarius Strobl 		return;
9940f34084fSMarius Strobl 
9950f34084fSMarius Strobl 	ios = &slot->host.ios;
9960f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
9970f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
9980f34084fSMarius Strobl 	hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK;
9990f34084fSMarius Strobl 	if (ios->timing == bus_timing_mmc_hs400 ||
10000f34084fSMarius Strobl 	    ios->timing == bus_timing_mmc_hs400es)
10010f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_MMC_HS400;
10020f34084fSMarius Strobl 	else if (ios->clock > SD_SDR50_MAX)
10030f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR104;
10040f34084fSMarius Strobl 	else if (ios->clock > SD_SDR25_MAX)
10050f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR50;
10060f34084fSMarius Strobl 	else if (ios->clock > SD_SDR12_MAX) {
10070f34084fSMarius Strobl 		if (ios->timing == bus_timing_uhs_ddr50 ||
10080f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_ddr52)
10090f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_DDR50;
10100f34084fSMarius Strobl 		else
10110f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR25;
10120f34084fSMarius Strobl 	} else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY)
10130f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR12;
10140f34084fSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
10150f34084fSMarius Strobl 	sdhci_set_clock(slot, ios->clock);
10160f34084fSMarius Strobl }
10170f34084fSMarius Strobl 
1018d6b3aaf8SOleksandr Tymoshenko int
1019d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
1020831f5dcfSAlexander Motin {
1021831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1022831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
1023831f5dcfSAlexander Motin 
1024*a94a63f0SWarner Losh 	device_printf(brdev,  "This is a bridge device\n");
1025*a94a63f0SWarner Losh 	device_printf(reqdev, "This is a request device\n");
1026*a94a63f0SWarner Losh 
1027*a94a63f0SWarner Losh 	slot_printf(slot, " <--- The locking slot is this\n");
1028831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1029831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
1030831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
1031831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
1032831f5dcfSAlexander Motin 		sdhci_init(slot);
1033831f5dcfSAlexander Motin 	}
1034831f5dcfSAlexander Motin 	/* Configure the bus. */
1035831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
1036831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
10372d1731b8SIan Lepore 	if (ios->bus_width == bus_width_8) {
10382d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
1039831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
10402d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_4) {
10412d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
10422d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
10432d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_1) {
10442d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
10452d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
10462d1731b8SIan Lepore 	} else {
10472d1731b8SIan Lepore 		panic("Invalid bus width: %d", ios->bus_width);
10482d1731b8SIan Lepore 	}
10490f34084fSMarius Strobl 	if (ios->clock > SD_SDR12_MAX &&
1050bba987dcSIan Lepore 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
1051831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
1052831f5dcfSAlexander Motin 	else
1053831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
1054831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
10550f34084fSMarius Strobl 	SDHCI_SET_UHS_TIMING(brdev, slot);
1056831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
1057d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
1058831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1059831f5dcfSAlexander Motin 
1060831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1061831f5dcfSAlexander Motin 	return (0);
1062831f5dcfSAlexander Motin }
1063831f5dcfSAlexander Motin 
10640f34084fSMarius Strobl int
10650f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev)
10660f34084fSMarius Strobl {
10670f34084fSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
10680f34084fSMarius Strobl 	enum mmc_vccq vccq;
10690f34084fSMarius Strobl 	int err;
10700f34084fSMarius Strobl 	uint16_t hostctrl2;
10710f34084fSMarius Strobl 
10720f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
10730f34084fSMarius Strobl 		return (0);
10740f34084fSMarius Strobl 
10750f34084fSMarius Strobl 	err = 0;
10760f34084fSMarius Strobl 	vccq = slot->host.ios.vccq;
10770f34084fSMarius Strobl 	SDHCI_LOCK(slot);
10780f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
10790f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
10800f34084fSMarius Strobl 	switch (vccq) {
10810f34084fSMarius Strobl 	case vccq_330:
10820f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
10830f34084fSMarius Strobl 			goto done;
10840f34084fSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE;
10850f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
10860f34084fSMarius Strobl 		DELAY(5000);
10870f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
10880f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
10890f34084fSMarius Strobl 			goto done;
10900f34084fSMarius Strobl 		err = EAGAIN;
10910f34084fSMarius Strobl 		break;
10920f34084fSMarius Strobl 	case vccq_180:
10930f34084fSMarius Strobl 		if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) {
10940f34084fSMarius Strobl 			err = EINVAL;
10950f34084fSMarius Strobl 			goto done;
10960f34084fSMarius Strobl 		}
10970f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
10980f34084fSMarius Strobl 			goto done;
10990f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_S18_ENABLE;
11000f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
11010f34084fSMarius Strobl 		DELAY(5000);
11020f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
11030f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
11040f34084fSMarius Strobl 			goto done;
11050f34084fSMarius Strobl 		err = EAGAIN;
11060f34084fSMarius Strobl 		break;
11070f34084fSMarius Strobl 	default:
11080f34084fSMarius Strobl 		slot_printf(slot,
11090f34084fSMarius Strobl 		    "Attempt to set unsupported signaling voltage\n");
11100f34084fSMarius Strobl 		err = EINVAL;
11110f34084fSMarius Strobl 		break;
11120f34084fSMarius Strobl 	}
11130f34084fSMarius Strobl done:
11140f34084fSMarius Strobl 	sdhci_set_clock(slot, slot->host.ios.clock);
11150f34084fSMarius Strobl 	SDHCI_UNLOCK(slot);
11160f34084fSMarius Strobl 	return (err);
11170f34084fSMarius Strobl }
11180f34084fSMarius Strobl 
1119*a94a63f0SWarner Losh #ifdef MMCCAM
1120*a94a63f0SWarner Losh static void
1121*a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot)
1122*a94a63f0SWarner Losh {
1123*a94a63f0SWarner Losh         union ccb *ccb;
1124*a94a63f0SWarner Losh 	if (sdhci_debug > 1)
1125*a94a63f0SWarner Losh 		slot_printf(slot, "sdhci_req_done()\n");
1126*a94a63f0SWarner Losh 	if (slot->ccb != NULL && slot->curcmd != NULL) {
1127*a94a63f0SWarner Losh 		callout_stop(&slot->timeout_callout);
1128*a94a63f0SWarner Losh                 ccb = slot->ccb;
1129*a94a63f0SWarner Losh                 slot->ccb = NULL;
1130*a94a63f0SWarner Losh 		slot->curcmd = NULL;
1131*a94a63f0SWarner Losh 
1132*a94a63f0SWarner Losh                 /* Tell CAM the request is finished */
1133*a94a63f0SWarner Losh                 struct ccb_mmcio *mmcio;
1134*a94a63f0SWarner Losh                 mmcio = &ccb->mmcio;
1135*a94a63f0SWarner Losh 
1136*a94a63f0SWarner Losh                 ccb->ccb_h.status =
1137*a94a63f0SWarner Losh                         (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR);
1138*a94a63f0SWarner Losh                 xpt_done(ccb);
1139*a94a63f0SWarner Losh 	}
1140*a94a63f0SWarner Losh }
1141*a94a63f0SWarner Losh #else
1142831f5dcfSAlexander Motin static void
1143e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot)
1144e64f01a9SIan Lepore {
1145e64f01a9SIan Lepore 	struct mmc_request *req;
1146e64f01a9SIan Lepore 
1147e64f01a9SIan Lepore 	if (slot->req != NULL && slot->curcmd != NULL) {
1148e64f01a9SIan Lepore 		callout_stop(&slot->timeout_callout);
1149e64f01a9SIan Lepore 		req = slot->req;
1150e64f01a9SIan Lepore 		slot->req = NULL;
1151e64f01a9SIan Lepore 		slot->curcmd = NULL;
1152e64f01a9SIan Lepore 		req->done(req);
1153e64f01a9SIan Lepore 	}
1154e64f01a9SIan Lepore }
1155*a94a63f0SWarner Losh #endif
1156e64f01a9SIan Lepore 
1157e64f01a9SIan Lepore static void
1158e64f01a9SIan Lepore sdhci_timeout(void *arg)
1159e64f01a9SIan Lepore {
1160e64f01a9SIan Lepore 	struct sdhci_slot *slot = arg;
1161e64f01a9SIan Lepore 
1162e64f01a9SIan Lepore 	if (slot->curcmd != NULL) {
11637e586643SIan Lepore 		slot_printf(slot, " Controller timeout\n");
11647e586643SIan Lepore 		sdhci_dumpregs(slot);
1165a6873fd1SIan Lepore 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1166e64f01a9SIan Lepore 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1167e64f01a9SIan Lepore 		sdhci_req_done(slot);
11687e586643SIan Lepore 	} else {
11697e586643SIan Lepore 		slot_printf(slot, " Spurious timeout - no active command\n");
1170e64f01a9SIan Lepore 	}
1171e64f01a9SIan Lepore }
1172e64f01a9SIan Lepore 
1173e64f01a9SIan Lepore static void
1174b440e965SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, struct mmc_data *data)
1175831f5dcfSAlexander Motin {
1176831f5dcfSAlexander Motin 	uint16_t mode;
1177831f5dcfSAlexander Motin 
1178831f5dcfSAlexander Motin 	if (data == NULL)
1179831f5dcfSAlexander Motin 		return;
1180831f5dcfSAlexander Motin 
1181831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
1182831f5dcfSAlexander Motin 	if (data->len > 512)
1183831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
1184831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
1185831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
1186*a94a63f0SWarner Losh #ifdef MMCCAM
1187*a94a63f0SWarner Losh 	struct ccb_mmcio *mmcio;
1188*a94a63f0SWarner Losh 	mmcio = &slot->ccb->mmcio;
1189*a94a63f0SWarner Losh 	if (mmcio->stop.opcode == MMC_STOP_TRANSMISSION
1190*a94a63f0SWarner Losh 	    && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))
1191*a94a63f0SWarner Losh 		mode |= SDHCI_TRNS_ACMD12;
1192*a94a63f0SWarner Losh #else
1193915780d7SLuiz Otavio O Souza 	if (slot->req->stop && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))
1194831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_ACMD12;
1195*a94a63f0SWarner Losh #endif
1196831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
1197831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
1198831f5dcfSAlexander Motin 
1199831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
1200831f5dcfSAlexander Motin }
1201831f5dcfSAlexander Motin 
1202831f5dcfSAlexander Motin static void
1203831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
1204831f5dcfSAlexander Motin {
1205831f5dcfSAlexander Motin 	int flags, timeout;
120690993663SIan Lepore 	uint32_t mask;
1207831f5dcfSAlexander Motin 
1208831f5dcfSAlexander Motin 	slot->curcmd = cmd;
1209831f5dcfSAlexander Motin 	slot->cmd_done = 0;
1210831f5dcfSAlexander Motin 
1211831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
1212831f5dcfSAlexander Motin 
1213831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
1214831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1215831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
1216831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1217e64f01a9SIan Lepore 		sdhci_req_done(slot);
1218831f5dcfSAlexander Motin 		return;
1219831f5dcfSAlexander Motin 	}
1220831f5dcfSAlexander Motin 
1221b440e965SMarius Strobl 	/*
1222b440e965SMarius Strobl 	 * Do not issue command if there is no card, clock or power.
1223b440e965SMarius Strobl 	 * Controller will not detect timeout without clock active.
1224b440e965SMarius Strobl 	 */
12256e37fb2bSIan Lepore 	if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
1226d8208d9eSAlexander Motin 	    slot->power == 0 ||
1227d8208d9eSAlexander Motin 	    slot->clock == 0) {
1228*a94a63f0SWarner Losh 		slot_printf(slot,
1229*a94a63f0SWarner Losh 			    "Cannot issue a command (power=%d clock=%d)",
1230*a94a63f0SWarner Losh 			    slot->power, slot->clock);
1231831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1232e64f01a9SIan Lepore 		sdhci_req_done(slot);
1233831f5dcfSAlexander Motin 		return;
1234831f5dcfSAlexander Motin 	}
1235831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
1236831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
1237831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
1238*a94a63f0SWarner Losh 	if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY))
1239831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
1240831f5dcfSAlexander Motin 	/* We shouldn't wait for DAT for stop commands. */
1241*a94a63f0SWarner Losh #ifdef MMCCAM
1242*a94a63f0SWarner Losh 	struct ccb_mmcio *mmcio = &slot->ccb->mmcio;
1243*a94a63f0SWarner Losh 	if (cmd == &mmcio->stop)
1244*a94a63f0SWarner Losh 		mask &= ~SDHCI_DAT_INHIBIT;
1245*a94a63f0SWarner Losh #else
1246831f5dcfSAlexander Motin 	if (cmd == slot->req->stop)
1247831f5dcfSAlexander Motin 		mask &= ~SDHCI_DAT_INHIBIT;
1248*a94a63f0SWarner Losh #endif
12498775ab45SIan Lepore 	/*
12508775ab45SIan Lepore 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
12518775ab45SIan Lepore 	 *  here at all, but when writing a crash dump we may be bypassing the
12528775ab45SIan Lepore 	 *  host platform's interrupt handler, and in some cases that handler
12538775ab45SIan Lepore 	 *  may be working around hardware quirks such as not respecting r1b
12548775ab45SIan Lepore 	 *  busy indications.  In those cases, this wait-loop serves the purpose
12558775ab45SIan Lepore 	 *  of waiting for the prior command and data transfers to be done, and
12568775ab45SIan Lepore 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
12578775ab45SIan Lepore 	 *  (It's usually more like 20-30ms in the real world.)
12588775ab45SIan Lepore 	 */
12598775ab45SIan Lepore 	timeout = 250;
126090993663SIan Lepore 	while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
1261831f5dcfSAlexander Motin 		if (timeout == 0) {
1262831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
1263831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
1264831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
1265831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
1266e64f01a9SIan Lepore 			sdhci_req_done(slot);
1267831f5dcfSAlexander Motin 			return;
1268831f5dcfSAlexander Motin 		}
1269831f5dcfSAlexander Motin 		timeout--;
1270831f5dcfSAlexander Motin 		DELAY(1000);
1271831f5dcfSAlexander Motin 	}
1272831f5dcfSAlexander Motin 
1273831f5dcfSAlexander Motin 	/* Prepare command flags. */
1274831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
1275831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
1276831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
1277831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
1278831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
1279831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
1280831f5dcfSAlexander Motin 	else
1281831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
1282831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
1283831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
1284831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
1285831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
1286*a94a63f0SWarner Losh 	if (cmd->data != NULL)
1287831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
1288831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
1289831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
1290831f5dcfSAlexander Motin 	/* Prepare data. */
1291831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
1292831f5dcfSAlexander Motin 	/*
1293831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
1294831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
1295831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
1296831f5dcfSAlexander Motin 	 */
1297831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
1298831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1299831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
1300831f5dcfSAlexander Motin 	}
1301831f5dcfSAlexander Motin 	/* Set command argument. */
1302831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
1303831f5dcfSAlexander Motin 	/* Set data transfer mode. */
1304831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
1305*a94a63f0SWarner Losh 	if (sdhci_debug > 1)
1306*a94a63f0SWarner Losh 		slot_printf(slot, "Starting command!\n");
1307831f5dcfSAlexander Motin 	/* Start command. */
1308d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
1309a6873fd1SIan Lepore 	/* Start timeout callout. */
1310ba6fc1c7SLuiz Otavio O Souza 	callout_reset(&slot->timeout_callout, slot->timeout * hz,
1311ba6fc1c7SLuiz Otavio O Souza 	    sdhci_timeout, slot);
1312831f5dcfSAlexander Motin }
1313831f5dcfSAlexander Motin 
1314831f5dcfSAlexander Motin static void
1315831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
1316831f5dcfSAlexander Motin {
1317831f5dcfSAlexander Motin 	int i;
13181bacf3beSMarius Strobl 	uint32_t val;
13191bacf3beSMarius Strobl 	uint8_t extra;
1320831f5dcfSAlexander Motin 
1321*a94a63f0SWarner Losh 	if (sdhci_debug > 1)
1322*a94a63f0SWarner Losh 		slot_printf(slot, "%s: called, err %d flags %d\n",
1323*a94a63f0SWarner Losh 			    __func__, slot->curcmd->error, slot->curcmd->flags);
1324831f5dcfSAlexander Motin 	slot->cmd_done = 1;
132572dec079SMarius Strobl 	/*
132672dec079SMarius Strobl 	 * Interrupt aggregation: Restore command interrupt.
1327831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
132872dec079SMarius Strobl 	 * happened first.
132972dec079SMarius Strobl 	 */
1330831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE);
1331831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
1332831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1333831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1334831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1335831f5dcfSAlexander Motin 		sdhci_start(slot);
1336831f5dcfSAlexander Motin 		return;
1337831f5dcfSAlexander Motin 	}
1338831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
1339831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1340831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
1341831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
13421bacf3beSMarius Strobl 			extra = 0;
1343831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
13441bacf3beSMarius Strobl 				val = RD4(slot, SDHCI_RESPONSE + i * 4);
13451bacf3beSMarius Strobl 				if (slot->quirks &
13461bacf3beSMarius Strobl 				    SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1347677ee494SIan Lepore 					slot->curcmd->resp[3 - i] = val;
1348677ee494SIan Lepore 				else {
1349677ee494SIan Lepore 					slot->curcmd->resp[3 - i] =
1350677ee494SIan Lepore 					    (val << 8) | extra;
1351831f5dcfSAlexander Motin 					extra = val >> 24;
1352831f5dcfSAlexander Motin 				}
1353677ee494SIan Lepore 			}
1354831f5dcfSAlexander Motin 		} else
1355831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1356831f5dcfSAlexander Motin 	}
1357*a94a63f0SWarner Losh 	if (sdhci_debug > 1)
1358*a94a63f0SWarner Losh 		printf("Resp: %02x %02x %02x %02x\n",
1359*a94a63f0SWarner Losh 		       slot->curcmd->resp[0], slot->curcmd->resp[1],
1360*a94a63f0SWarner Losh 		       slot->curcmd->resp[2], slot->curcmd->resp[3]);
1361*a94a63f0SWarner Losh 
1362831f5dcfSAlexander Motin 	/* If data ready - finish. */
1363831f5dcfSAlexander Motin 	if (slot->data_done)
1364831f5dcfSAlexander Motin 		sdhci_start(slot);
1365831f5dcfSAlexander Motin }
1366831f5dcfSAlexander Motin 
1367831f5dcfSAlexander Motin static void
1368831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data)
1369831f5dcfSAlexander Motin {
1370831f5dcfSAlexander Motin 	uint32_t target_timeout, current_timeout;
1371831f5dcfSAlexander Motin 	uint8_t div;
1372831f5dcfSAlexander Motin 
1373831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1374831f5dcfSAlexander Motin 		slot->data_done = 1;
1375831f5dcfSAlexander Motin 		return;
1376831f5dcfSAlexander Motin 	}
1377831f5dcfSAlexander Motin 
1378831f5dcfSAlexander Motin 	slot->data_done = 0;
1379831f5dcfSAlexander Motin 
1380831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
1381831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
1382ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1383ceb9e9f7SIan Lepore 		div = 0xE;
1384ceb9e9f7SIan Lepore 	} else {
1385831f5dcfSAlexander Motin 		target_timeout = 1000000;
1386831f5dcfSAlexander Motin 		div = 0;
1387831f5dcfSAlexander Motin 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1388ceb9e9f7SIan Lepore 		while (current_timeout < target_timeout && div < 0xE) {
1389ceb9e9f7SIan Lepore 			++div;
1390831f5dcfSAlexander Motin 			current_timeout <<= 1;
1391831f5dcfSAlexander Motin 		}
1392831f5dcfSAlexander Motin 		/* Compensate for an off-by-one error in the CaFe chip.*/
1393ceb9e9f7SIan Lepore 		if (div < 0xE &&
1394ceb9e9f7SIan Lepore 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1395ceb9e9f7SIan Lepore 			++div;
1396831f5dcfSAlexander Motin 		}
1397ceb9e9f7SIan Lepore 	}
1398831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1399831f5dcfSAlexander Motin 
1400831f5dcfSAlexander Motin 	if (data == NULL)
1401831f5dcfSAlexander Motin 		return;
1402831f5dcfSAlexander Motin 
1403831f5dcfSAlexander Motin 	/* Use DMA if possible. */
1404831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
1405831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
1406831f5dcfSAlexander Motin 	/* If data is small, broken DMA may return zeroes instead of data, */
1407d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1408831f5dcfSAlexander Motin 	    (data->len <= 512))
1409831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1410831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
1411d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1412831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
1413831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1414831f5dcfSAlexander Motin 	/* Load DMA buffer. */
1415831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
1416831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
1417ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1418ecc2d997SRui Paulo 			    BUS_DMASYNC_PREREAD);
1419831f5dcfSAlexander Motin 		else {
1420831f5dcfSAlexander Motin 			memcpy(slot->dmamem, data->data,
1421ecc2d997SRui Paulo 			    (data->len < DMA_BLOCK_SIZE) ?
1422ecc2d997SRui Paulo 			    data->len : DMA_BLOCK_SIZE);
1423ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1424ecc2d997SRui Paulo 			    BUS_DMASYNC_PREWRITE);
1425831f5dcfSAlexander Motin 		}
1426831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1427831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1428831f5dcfSAlexander Motin 		 * for the last page and unmask else. */
1429831f5dcfSAlexander Motin 		if (data->len == DMA_BLOCK_SIZE)
1430831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1431831f5dcfSAlexander Motin 		else
1432831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
1433831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1434831f5dcfSAlexander Motin 	}
1435831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
1436831f5dcfSAlexander Motin 	slot->offset = 0;
1437831f5dcfSAlexander Motin 	/* Set block size and request IRQ on 4K border. */
14381bacf3beSMarius Strobl 	WR2(slot, SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(DMA_BOUNDARY,
14391bacf3beSMarius Strobl 	    (data->len < 512) ? data->len : 512));
1440831f5dcfSAlexander Motin 	/* Set block count. */
1441831f5dcfSAlexander Motin 	WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
1442*a94a63f0SWarner Losh 
1443*a94a63f0SWarner Losh 	if (sdhci_debug > 1)
1444*a94a63f0SWarner Losh 		slot_printf(slot, "Block size: %02x, count %lu\n", (unsigned int)
1445*a94a63f0SWarner Losh 		    SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512)?data->len:512),
1446*a94a63f0SWarner Losh 		    (unsigned long)(data->len + 511) / 512);
1447831f5dcfSAlexander Motin }
1448831f5dcfSAlexander Motin 
1449c3a0f75aSOleksandr Tymoshenko void
1450831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
1451831f5dcfSAlexander Motin {
1452831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
14537e6ccea3SMarius Strobl 	size_t left;
1454831f5dcfSAlexander Motin 
1455831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
1456ecc2d997SRui Paulo 	 * Auxiliary restore point for the case when data interrupt
1457831f5dcfSAlexander Motin 	 * happened first. */
1458831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
1459831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1460831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
1461831f5dcfSAlexander Motin 	}
1462831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
1463915780d7SLuiz Otavio O Souza 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) &&
1464915780d7SLuiz Otavio O Souza 	    slot->curcmd->data != NULL) {
1465831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
14667e6ccea3SMarius Strobl 			left = data->len - slot->offset;
1467ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1468ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTREAD);
1469831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1470831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
1471831f5dcfSAlexander Motin 		} else
1472ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1473ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTWRITE);
1474831f5dcfSAlexander Motin 	}
1475a98788edSIan Lepore 	slot->data_done = 1;
1476831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
1477831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1478831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1479831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1480831f5dcfSAlexander Motin 		sdhci_start(slot);
1481831f5dcfSAlexander Motin 		return;
1482831f5dcfSAlexander Motin 	}
1483831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
1484831f5dcfSAlexander Motin 	if (slot->cmd_done)
1485831f5dcfSAlexander Motin 		sdhci_start(slot);
1486831f5dcfSAlexander Motin }
1487831f5dcfSAlexander Motin 
1488*a94a63f0SWarner Losh #ifdef MMCCAM
1489*a94a63f0SWarner Losh static void
1490*a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot)
1491*a94a63f0SWarner Losh {
1492*a94a63f0SWarner Losh         union ccb *ccb;
1493*a94a63f0SWarner Losh 
1494*a94a63f0SWarner Losh 	ccb = slot->ccb;
1495*a94a63f0SWarner Losh 	if (ccb == NULL)
1496*a94a63f0SWarner Losh 		return;
1497*a94a63f0SWarner Losh 
1498*a94a63f0SWarner Losh         struct ccb_mmcio *mmcio;
1499*a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
1500*a94a63f0SWarner Losh 
1501*a94a63f0SWarner Losh 	if (!(slot->flags & CMD_STARTED)) {
1502*a94a63f0SWarner Losh 		slot->flags |= CMD_STARTED;
1503*a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->cmd);
1504*a94a63f0SWarner Losh 		return;
1505*a94a63f0SWarner Losh 	}
1506*a94a63f0SWarner Losh 
1507*a94a63f0SWarner Losh 	/*
1508*a94a63f0SWarner Losh 	 * Old stack doesn't use this!
1509*a94a63f0SWarner Losh 	 * Enabling this code causes significant performance degradation
1510*a94a63f0SWarner Losh 	 * and IRQ storms on BBB, Wandboard behaves fine.
1511*a94a63f0SWarner Losh 	 * Not using this code does no harm...
1512*a94a63f0SWarner Losh 	if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) {
1513*a94a63f0SWarner Losh 		slot->flags |= STOP_STARTED;
1514*a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->stop);
1515*a94a63f0SWarner Losh 		return;
1516*a94a63f0SWarner Losh 	}
1517*a94a63f0SWarner Losh 	*/
1518*a94a63f0SWarner Losh 	if (sdhci_debug > 1)
1519*a94a63f0SWarner Losh 		slot_printf(slot, "result: %d\n", mmcio->cmd.error);
1520*a94a63f0SWarner Losh 	if (mmcio->cmd.error == 0 &&
1521*a94a63f0SWarner Losh 	    (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
1522*a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD);
1523*a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_DATA);
1524*a94a63f0SWarner Losh 	}
1525*a94a63f0SWarner Losh 
1526*a94a63f0SWarner Losh 	sdhci_req_done(slot);
1527*a94a63f0SWarner Losh }
1528*a94a63f0SWarner Losh #else
1529831f5dcfSAlexander Motin static void
1530831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
1531831f5dcfSAlexander Motin {
1532831f5dcfSAlexander Motin 	struct mmc_request *req;
1533831f5dcfSAlexander Motin 
1534831f5dcfSAlexander Motin 	req = slot->req;
1535831f5dcfSAlexander Motin 	if (req == NULL)
1536831f5dcfSAlexander Motin 		return;
1537831f5dcfSAlexander Motin 
1538831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
1539831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
1540831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
1541831f5dcfSAlexander Motin 		return;
1542831f5dcfSAlexander Motin 	}
1543915780d7SLuiz Otavio O Souza 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) &&
1544915780d7SLuiz Otavio O Souza 	    !(slot->flags & STOP_STARTED) && req->stop) {
1545831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
1546831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
1547831f5dcfSAlexander Motin 		return;
1548831f5dcfSAlexander Motin 	}
15495b69a497SAlexander Motin 	if (sdhci_debug > 1)
15505b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
15515b69a497SAlexander Motin 	if (!req->cmd->error &&
1552915780d7SLuiz Otavio O Souza 	    ((slot->curcmd == req->stop &&
1553915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) ||
1554915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
1555831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1556831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1557831f5dcfSAlexander Motin 	}
1558831f5dcfSAlexander Motin 
1559e64f01a9SIan Lepore 	sdhci_req_done(slot);
1560831f5dcfSAlexander Motin }
1561*a94a63f0SWarner Losh #endif
1562831f5dcfSAlexander Motin 
1563d6b3aaf8SOleksandr Tymoshenko int
1564b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev,
1565b440e965SMarius Strobl     struct mmc_request *req)
1566831f5dcfSAlexander Motin {
1567831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1568831f5dcfSAlexander Motin 
1569831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1570831f5dcfSAlexander Motin 	if (slot->req != NULL) {
1571831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1572831f5dcfSAlexander Motin 		return (EBUSY);
1573831f5dcfSAlexander Motin 	}
15745b69a497SAlexander Motin 	if (sdhci_debug > 1) {
15751bacf3beSMarius Strobl 		slot_printf(slot,
15761bacf3beSMarius Strobl 		    "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1577831f5dcfSAlexander Motin 		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
15785b69a497SAlexander Motin 		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
15795b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
15805b69a497SAlexander Motin 	}
1581831f5dcfSAlexander Motin 	slot->req = req;
1582831f5dcfSAlexander Motin 	slot->flags = 0;
1583831f5dcfSAlexander Motin 	sdhci_start(slot);
1584831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1585bea2dca2SAlexander Motin 	if (dumping) {
1586bea2dca2SAlexander Motin 		while (slot->req != NULL) {
1587d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
1588bea2dca2SAlexander Motin 			DELAY(10);
1589bea2dca2SAlexander Motin 		}
1590bea2dca2SAlexander Motin 	}
1591831f5dcfSAlexander Motin 	return (0);
1592831f5dcfSAlexander Motin }
1593831f5dcfSAlexander Motin 
1594d6b3aaf8SOleksandr Tymoshenko int
1595b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev)
1596831f5dcfSAlexander Motin {
1597831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1598831f5dcfSAlexander Motin 	uint32_t val;
1599831f5dcfSAlexander Motin 
1600831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1601831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
1602831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1603831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
1604831f5dcfSAlexander Motin }
1605831f5dcfSAlexander Motin 
1606d6b3aaf8SOleksandr Tymoshenko int
1607b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev)
1608831f5dcfSAlexander Motin {
1609831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1610831f5dcfSAlexander Motin 	int err = 0;
1611831f5dcfSAlexander Motin 
1612831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1613831f5dcfSAlexander Motin 	while (slot->bus_busy)
1614d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
1615831f5dcfSAlexander Motin 	slot->bus_busy++;
1616831f5dcfSAlexander Motin 	/* Activate led. */
1617831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
1618831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1619831f5dcfSAlexander Motin 	return (err);
1620831f5dcfSAlexander Motin }
1621831f5dcfSAlexander Motin 
1622d6b3aaf8SOleksandr Tymoshenko int
1623b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev)
1624831f5dcfSAlexander Motin {
1625831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1626831f5dcfSAlexander Motin 
1627831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1628831f5dcfSAlexander Motin 	/* Deactivate led. */
1629831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
1630831f5dcfSAlexander Motin 	slot->bus_busy--;
1631831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1632d493985aSAlexander Motin 	wakeup(slot);
1633831f5dcfSAlexander Motin 	return (0);
1634831f5dcfSAlexander Motin }
1635831f5dcfSAlexander Motin 
1636831f5dcfSAlexander Motin static void
1637831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
1638831f5dcfSAlexander Motin {
1639831f5dcfSAlexander Motin 
1640831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1641831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
1642831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1643831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1644831f5dcfSAlexander Motin 		return;
1645831f5dcfSAlexander Motin 	}
1646831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
1647831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1648831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
1649831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1650831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
1651831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
1652831f5dcfSAlexander Motin 
1653831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
1654831f5dcfSAlexander Motin }
1655831f5dcfSAlexander Motin 
1656831f5dcfSAlexander Motin static void
1657831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
1658831f5dcfSAlexander Motin {
16591bacf3beSMarius Strobl 	struct mmc_data *data;
1660831f5dcfSAlexander Motin 
1661831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1662831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1663831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
1664831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1665831f5dcfSAlexander Motin 		return;
1666831f5dcfSAlexander Motin 	}
1667831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
1668831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1669831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1670831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
1671831f5dcfSAlexander Motin 		    intmask);
1672831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1673831f5dcfSAlexander Motin 		return;
1674831f5dcfSAlexander Motin 	}
1675831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
1676831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1677acbaa69fSOleksandr Tymoshenko 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1678831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
1679831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
1680831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
1681831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
1682831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1683831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
1684831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1685831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
1686831f5dcfSAlexander Motin 	}
1687831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1688831f5dcfSAlexander Motin 		/* No need to continue after any error. */
1689a98788edSIan Lepore 		goto done;
1690831f5dcfSAlexander Motin 	}
1691831f5dcfSAlexander Motin 
1692831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
1693c3a0f75aSOleksandr Tymoshenko 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
1694c3a0f75aSOleksandr Tymoshenko 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
1695c3a0f75aSOleksandr Tymoshenko 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
16961bacf3beSMarius Strobl 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot,
16971bacf3beSMarius Strobl 			    &intmask);
1698c3a0f75aSOleksandr Tymoshenko 			slot->flags |= PLATFORM_DATA_STARTED;
1699c3a0f75aSOleksandr Tymoshenko 		} else
1700831f5dcfSAlexander Motin 			sdhci_transfer_pio(slot);
1701c3a0f75aSOleksandr Tymoshenko 	}
1702831f5dcfSAlexander Motin 	/* Handle DMA border. */
1703831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
17041bacf3beSMarius Strobl 		data = slot->curcmd->data;
1705*a94a63f0SWarner Losh 		size_t left;
1706831f5dcfSAlexander Motin 
1707831f5dcfSAlexander Motin 		/* Unload DMA buffer ... */
1708831f5dcfSAlexander Motin 		left = data->len - slot->offset;
1709831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
1710831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1711831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
1712831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1713831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
1714831f5dcfSAlexander Motin 		} else {
1715831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1716831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
1717831f5dcfSAlexander Motin 		}
1718831f5dcfSAlexander Motin 		/* ... and reload it again. */
1719831f5dcfSAlexander Motin 		slot->offset += DMA_BLOCK_SIZE;
1720831f5dcfSAlexander Motin 		left = data->len - slot->offset;
1721831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
1722831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1723831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREREAD);
1724831f5dcfSAlexander Motin 		} else {
1725831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
1726831f5dcfSAlexander Motin 			    (left < DMA_BLOCK_SIZE)? left : DMA_BLOCK_SIZE);
1727831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1728831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
1729831f5dcfSAlexander Motin 		}
1730831f5dcfSAlexander Motin 		/* Interrupt aggregation: Mask border interrupt
1731831f5dcfSAlexander Motin 		 * for the last page. */
1732831f5dcfSAlexander Motin 		if (left == DMA_BLOCK_SIZE) {
1733831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1734831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1735831f5dcfSAlexander Motin 		}
1736831f5dcfSAlexander Motin 		/* Restart DMA. */
1737831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1738831f5dcfSAlexander Motin 	}
1739831f5dcfSAlexander Motin 	/* We have got all data. */
1740c3a0f75aSOleksandr Tymoshenko 	if (intmask & SDHCI_INT_DATA_END) {
1741c3a0f75aSOleksandr Tymoshenko 		if (slot->flags & PLATFORM_DATA_STARTED) {
1742c3a0f75aSOleksandr Tymoshenko 			slot->flags &= ~PLATFORM_DATA_STARTED;
1743c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1744c3a0f75aSOleksandr Tymoshenko 		} else
1745831f5dcfSAlexander Motin 			sdhci_finish_data(slot);
1746831f5dcfSAlexander Motin 	}
1747a98788edSIan Lepore done:
1748a98788edSIan Lepore 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
1749a98788edSIan Lepore 		if (slot->flags & PLATFORM_DATA_STARTED) {
1750a98788edSIan Lepore 			slot->flags &= ~PLATFORM_DATA_STARTED;
1751a98788edSIan Lepore 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1752a98788edSIan Lepore 		} else
1753a98788edSIan Lepore 			sdhci_finish_data(slot);
1754a98788edSIan Lepore 	}
1755c3a0f75aSOleksandr Tymoshenko }
1756831f5dcfSAlexander Motin 
1757831f5dcfSAlexander Motin static void
1758831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot)
1759831f5dcfSAlexander Motin {
1760831f5dcfSAlexander Motin 	uint16_t err;
1761831f5dcfSAlexander Motin 
1762831f5dcfSAlexander Motin 	err = RD4(slot, SDHCI_ACMD12_ERR);
1763831f5dcfSAlexander Motin 	if (!slot->curcmd) {
1764831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
1765831f5dcfSAlexander Motin 		    "there is no active command.\n", err);
1766831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1767831f5dcfSAlexander Motin 		return;
1768831f5dcfSAlexander Motin 	}
1769831f5dcfSAlexander Motin 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err);
1770831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_CMD);
1771831f5dcfSAlexander Motin }
1772831f5dcfSAlexander Motin 
1773d6b3aaf8SOleksandr Tymoshenko void
1774d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
1775831f5dcfSAlexander Motin {
17762b96b955SJustin Hibbits 	uint32_t intmask, present;
1777831f5dcfSAlexander Motin 
1778831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1779831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
1780831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
1781831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
1782831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
1783d6b3aaf8SOleksandr Tymoshenko 		return;
1784831f5dcfSAlexander Motin 	}
17855b69a497SAlexander Motin 	if (sdhci_debug > 2)
17865b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
17875b69a497SAlexander Motin 
1788831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
1789831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1790639f59f0SIan Lepore 		present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
17912b96b955SJustin Hibbits 		slot->intmask &=
17922b96b955SJustin Hibbits 		    ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
17932b96b955SJustin Hibbits 		slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
17942b96b955SJustin Hibbits 		    SDHCI_INT_CARD_INSERT;
17952b96b955SJustin Hibbits 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
17962b96b955SJustin Hibbits 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1797831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
1798831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
1799b8bf08b1SIan Lepore 		sdhci_handle_card_present_locked(slot, present);
1800831f5dcfSAlexander Motin 		intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1801831f5dcfSAlexander Motin 	}
1802831f5dcfSAlexander Motin 	/* Handle command interrupts. */
1803831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
1804831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
1805831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
1806831f5dcfSAlexander Motin 	}
1807831f5dcfSAlexander Motin 	/* Handle data interrupts. */
1808831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
1809831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
18107e6ccea3SMarius Strobl 		/* Don't call data_irq in case of errored command. */
18117e586643SIan Lepore 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
1812831f5dcfSAlexander Motin 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
1813831f5dcfSAlexander Motin 	}
1814831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
1815831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
1816831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
1817831f5dcfSAlexander Motin 		sdhci_acmd_irq(slot);
1818831f5dcfSAlexander Motin 	}
1819831f5dcfSAlexander Motin 	intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1820831f5dcfSAlexander Motin 	intmask &= ~SDHCI_INT_ACMD12ERR;
1821831f5dcfSAlexander Motin 	intmask &= ~SDHCI_INT_ERROR;
1822831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
1823831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
1824831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
1825831f5dcfSAlexander Motin 		slot_printf(slot,
1826831f5dcfSAlexander Motin 		    "Card is consuming too much power!\n");
1827831f5dcfSAlexander Motin 		intmask &= ~SDHCI_INT_BUS_POWER;
1828831f5dcfSAlexander Motin 	}
1829*a94a63f0SWarner Losh 	/* Handle card interrupt. */
1830*a94a63f0SWarner Losh 	if (intmask & SDHCI_INT_CARD_INT) {
1831*a94a63f0SWarner Losh 
1832*a94a63f0SWarner Losh 	}
1833831f5dcfSAlexander Motin 	/* The rest is unknown. */
1834831f5dcfSAlexander Motin 	if (intmask) {
1835831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
1836831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
1837831f5dcfSAlexander Motin 		    intmask);
1838831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1839831f5dcfSAlexander Motin 	}
1840831f5dcfSAlexander Motin 
1841831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1842831f5dcfSAlexander Motin }
1843831f5dcfSAlexander Motin 
1844d6b3aaf8SOleksandr Tymoshenko int
18451bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which,
18461bacf3beSMarius Strobl     uintptr_t *result)
1847831f5dcfSAlexander Motin {
1848831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
1849831f5dcfSAlexander Motin 
1850831f5dcfSAlexander Motin 	switch (which) {
1851831f5dcfSAlexander Motin 	default:
1852831f5dcfSAlexander Motin 		return (EINVAL);
1853831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
1854bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
1855831f5dcfSAlexander Motin 		break;
1856831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
1857bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
1858831f5dcfSAlexander Motin 		break;
1859831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
1860bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
1861831f5dcfSAlexander Motin 		break;
1862831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
1863bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
1864831f5dcfSAlexander Motin 		break;
1865831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
1866bcd91d25SJayachandran C. 		*result = slot->host.f_min;
1867831f5dcfSAlexander Motin 		break;
1868831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
1869bcd91d25SJayachandran C. 		*result = slot->host.f_max;
1870831f5dcfSAlexander Motin 		break;
1871831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
1872bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
1873831f5dcfSAlexander Motin 		break;
1874831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
1875bcd91d25SJayachandran C. 		*result = slot->host.mode;
1876831f5dcfSAlexander Motin 		break;
1877831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
1878bcd91d25SJayachandran C. 		*result = slot->host.ocr;
1879831f5dcfSAlexander Motin 		break;
1880831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
1881bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
1882831f5dcfSAlexander Motin 		break;
1883831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
1884bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
1885831f5dcfSAlexander Motin 		break;
18860f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
18870f34084fSMarius Strobl 		*result = slot->host.ios.vccq;
18880f34084fSMarius Strobl 		break;
1889831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
1890bcd91d25SJayachandran C. 		*result = slot->host.caps;
1891831f5dcfSAlexander Motin 		break;
1892831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
1893bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
1894831f5dcfSAlexander Motin 		break;
18953a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
1896bcd91d25SJayachandran C. 		*result = 65535;
18973a4a2557SAlexander Motin 		break;
189872dec079SMarius Strobl 	case MMCBR_IVAR_MAX_BUSY_TIMEOUT:
189972dec079SMarius Strobl 		/*
190072dec079SMarius Strobl 		 * Currently, sdhci_start_data() hardcodes 1 s for all CMDs.
190172dec079SMarius Strobl 		 */
190272dec079SMarius Strobl 		*result = 1000000;
190372dec079SMarius Strobl 		break;
1904831f5dcfSAlexander Motin 	}
1905831f5dcfSAlexander Motin 	return (0);
1906831f5dcfSAlexander Motin }
1907831f5dcfSAlexander Motin 
1908d6b3aaf8SOleksandr Tymoshenko int
19091bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which,
19101bacf3beSMarius Strobl     uintptr_t value)
1911831f5dcfSAlexander Motin {
1912831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
1913b440e965SMarius Strobl 	uint32_t clock, max_clock;
1914b440e965SMarius Strobl 	int i;
1915831f5dcfSAlexander Motin 
1916*a94a63f0SWarner Losh 	slot_printf(slot, "sdhci_generic_write_ivar, var=%d\n", which);
1917831f5dcfSAlexander Motin 	switch (which) {
1918831f5dcfSAlexander Motin 	default:
1919831f5dcfSAlexander Motin 		return (EINVAL);
1920831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
1921831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
1922831f5dcfSAlexander Motin 		break;
1923831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
1924831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
1925831f5dcfSAlexander Motin 		break;
1926831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
1927831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
1928831f5dcfSAlexander Motin 		break;
1929831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
1930831f5dcfSAlexander Motin 		if (value > 0) {
193157677a3aSOleksandr Tymoshenko 			max_clock = slot->max_clk;
193257677a3aSOleksandr Tymoshenko 			clock = max_clock;
193357677a3aSOleksandr Tymoshenko 
193457677a3aSOleksandr Tymoshenko 			if (slot->version < SDHCI_SPEC_300) {
193557677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
193657677a3aSOleksandr Tymoshenko 				    i <<= 1) {
1937831f5dcfSAlexander Motin 					if (clock <= value)
1938831f5dcfSAlexander Motin 						break;
1939831f5dcfSAlexander Motin 					clock >>= 1;
1940831f5dcfSAlexander Motin 				}
1941b440e965SMarius Strobl 			} else {
194257677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
194357677a3aSOleksandr Tymoshenko 				    i += 2) {
194457677a3aSOleksandr Tymoshenko 					if (clock <= value)
194557677a3aSOleksandr Tymoshenko 						break;
194657677a3aSOleksandr Tymoshenko 					clock = max_clock / (i + 2);
194757677a3aSOleksandr Tymoshenko 				}
194857677a3aSOleksandr Tymoshenko 			}
194957677a3aSOleksandr Tymoshenko 
1950831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
1951831f5dcfSAlexander Motin 		} else
1952831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
1953831f5dcfSAlexander Motin 		break;
1954831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
1955831f5dcfSAlexander Motin 		slot->host.mode = value;
1956831f5dcfSAlexander Motin 		break;
1957831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
1958831f5dcfSAlexander Motin 		slot->host.ocr = value;
1959831f5dcfSAlexander Motin 		break;
1960831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
1961831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
1962831f5dcfSAlexander Motin 		break;
1963831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
1964831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
1965831f5dcfSAlexander Motin 		break;
19660f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
19670f34084fSMarius Strobl 		slot->host.ios.vccq = value;
19680f34084fSMarius Strobl 		break;
1969831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
1970831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
1971831f5dcfSAlexander Motin 		break;
1972831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
1973831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
1974831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
1975831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
19763a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
1977831f5dcfSAlexander Motin 		return (EINVAL);
1978831f5dcfSAlexander Motin 	}
1979831f5dcfSAlexander Motin 	return (0);
1980831f5dcfSAlexander Motin }
1981831f5dcfSAlexander Motin 
1982*a94a63f0SWarner Losh /* CAM-related functions */
1983*a94a63f0SWarner Losh #include <cam/cam.h>
1984*a94a63f0SWarner Losh #include <cam/cam_ccb.h>
1985*a94a63f0SWarner Losh #include <cam/cam_debug.h>
1986*a94a63f0SWarner Losh #include <cam/cam_sim.h>
1987*a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h>
1988*a94a63f0SWarner Losh 
1989*a94a63f0SWarner Losh void
1990*a94a63f0SWarner Losh sdhci_cam_start_slot(struct sdhci_slot *slot)
1991*a94a63f0SWarner Losh {
1992*a94a63f0SWarner Losh         if ((slot->devq = cam_simq_alloc(1)) == NULL) {
1993*a94a63f0SWarner Losh                 goto fail;
1994*a94a63f0SWarner Losh         }
1995*a94a63f0SWarner Losh 
1996*a94a63f0SWarner Losh         mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF);
1997*a94a63f0SWarner Losh         slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll,
1998*a94a63f0SWarner Losh                                   "sdhci_slot", slot, device_get_unit(slot->bus),
1999*a94a63f0SWarner Losh                                   &slot->sim_mtx, 1, 1, slot->devq);
2000*a94a63f0SWarner Losh 
2001*a94a63f0SWarner Losh         if (slot->sim == NULL) {
2002*a94a63f0SWarner Losh                 cam_simq_free(slot->devq);
2003*a94a63f0SWarner Losh                 slot_printf(slot, "cannot allocate CAM SIM\n");
2004*a94a63f0SWarner Losh                 goto fail;
2005*a94a63f0SWarner Losh         }
2006*a94a63f0SWarner Losh 
2007*a94a63f0SWarner Losh         mtx_lock(&slot->sim_mtx);
2008*a94a63f0SWarner Losh         if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) {
2009*a94a63f0SWarner Losh                 slot_printf(slot,
2010*a94a63f0SWarner Losh                               "cannot register SCSI pass-through bus\n");
2011*a94a63f0SWarner Losh                 cam_sim_free(slot->sim, FALSE);
2012*a94a63f0SWarner Losh                 cam_simq_free(slot->devq);
2013*a94a63f0SWarner Losh                 mtx_unlock(&slot->sim_mtx);
2014*a94a63f0SWarner Losh                 goto fail;
2015*a94a63f0SWarner Losh         }
2016*a94a63f0SWarner Losh 
2017*a94a63f0SWarner Losh         mtx_unlock(&slot->sim_mtx);
2018*a94a63f0SWarner Losh         /* End CAM-specific init */
2019*a94a63f0SWarner Losh 	slot->card_present = 0;
2020*a94a63f0SWarner Losh 	sdhci_card_task(slot, 0);
2021*a94a63f0SWarner Losh         return;
2022*a94a63f0SWarner Losh 
2023*a94a63f0SWarner Losh fail:
2024*a94a63f0SWarner Losh         if (slot->sim != NULL) {
2025*a94a63f0SWarner Losh                 mtx_lock(&slot->sim_mtx);
2026*a94a63f0SWarner Losh                 xpt_bus_deregister(cam_sim_path(slot->sim));
2027*a94a63f0SWarner Losh                 cam_sim_free(slot->sim, FALSE);
2028*a94a63f0SWarner Losh                 mtx_unlock(&slot->sim_mtx);
2029*a94a63f0SWarner Losh         }
2030*a94a63f0SWarner Losh 
2031*a94a63f0SWarner Losh         if (slot->devq != NULL)
2032*a94a63f0SWarner Losh                 cam_simq_free(slot->devq);
2033*a94a63f0SWarner Losh }
2034*a94a63f0SWarner Losh 
2035*a94a63f0SWarner Losh static void
2036*a94a63f0SWarner Losh sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb)
2037*a94a63f0SWarner Losh {
2038*a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2039*a94a63f0SWarner Losh 
2040*a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2041*a94a63f0SWarner Losh 
2042*a94a63f0SWarner Losh 	sdhci_cam_request(slot, ccb);
2043*a94a63f0SWarner Losh }
2044*a94a63f0SWarner Losh 
2045*a94a63f0SWarner Losh void
2046*a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb)
2047*a94a63f0SWarner Losh {
2048*a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2049*a94a63f0SWarner Losh 
2050*a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2051*a94a63f0SWarner Losh 	if (slot == NULL) {
2052*a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2053*a94a63f0SWarner Losh 		xpt_done(ccb);
2054*a94a63f0SWarner Losh 		return;
2055*a94a63f0SWarner Losh 	}
2056*a94a63f0SWarner Losh 
2057*a94a63f0SWarner Losh 	mtx_assert(&slot->sim_mtx, MA_OWNED);
2058*a94a63f0SWarner Losh 
2059*a94a63f0SWarner Losh 	switch (ccb->ccb_h.func_code) {
2060*a94a63f0SWarner Losh 	case XPT_PATH_INQ:
2061*a94a63f0SWarner Losh 	{
2062*a94a63f0SWarner Losh 		struct ccb_pathinq *cpi;
2063*a94a63f0SWarner Losh 
2064*a94a63f0SWarner Losh 		cpi = &ccb->cpi;
2065*a94a63f0SWarner Losh 		cpi->version_num = 1;
2066*a94a63f0SWarner Losh 		cpi->hba_inquiry = 0;
2067*a94a63f0SWarner Losh 		cpi->target_sprt = 0;
2068*a94a63f0SWarner Losh 		cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
2069*a94a63f0SWarner Losh 		cpi->hba_eng_cnt = 0;
2070*a94a63f0SWarner Losh 		cpi->max_target = 0;
2071*a94a63f0SWarner Losh 		cpi->max_lun = 0;
2072*a94a63f0SWarner Losh 		cpi->initiator_id = 1;
2073*a94a63f0SWarner Losh 		cpi->maxio = MAXPHYS;
2074*a94a63f0SWarner Losh 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2075*a94a63f0SWarner Losh 		strncpy(cpi->hba_vid, "Deglitch Networks", HBA_IDLEN);
2076*a94a63f0SWarner Losh 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2077*a94a63f0SWarner Losh 		cpi->unit_number = cam_sim_unit(sim);
2078*a94a63f0SWarner Losh 		cpi->bus_id = cam_sim_bus(sim);
2079*a94a63f0SWarner Losh 		cpi->base_transfer_speed = 100; /* XXX WTF? */
2080*a94a63f0SWarner Losh 		cpi->protocol = PROTO_MMCSD;
2081*a94a63f0SWarner Losh 		cpi->protocol_version = SCSI_REV_0;
2082*a94a63f0SWarner Losh 		cpi->transport = XPORT_MMCSD;
2083*a94a63f0SWarner Losh 		cpi->transport_version = 0;
2084*a94a63f0SWarner Losh 
2085*a94a63f0SWarner Losh 		cpi->ccb_h.status = CAM_REQ_CMP;
2086*a94a63f0SWarner Losh 		break;
2087*a94a63f0SWarner Losh 	}
2088*a94a63f0SWarner Losh 	case XPT_GET_TRAN_SETTINGS:
2089*a94a63f0SWarner Losh 	{
2090*a94a63f0SWarner Losh 		struct ccb_trans_settings *cts = &ccb->cts;
2091*a94a63f0SWarner Losh 
2092*a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2093*a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n");
2094*a94a63f0SWarner Losh 
2095*a94a63f0SWarner Losh 		cts->protocol = PROTO_MMCSD;
2096*a94a63f0SWarner Losh 		cts->protocol_version = 1;
2097*a94a63f0SWarner Losh 		cts->transport = XPORT_MMCSD;
2098*a94a63f0SWarner Losh 		cts->transport_version = 1;
2099*a94a63f0SWarner Losh 		cts->xport_specific.valid = 0;
2100*a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_ocr = slot->host.host_ocr;
2101*a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_min = slot->host.f_min;
2102*a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_max = slot->host.f_max;
2103*a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_caps = slot->host.caps;
2104*a94a63f0SWarner Losh 		memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios));
2105*a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2106*a94a63f0SWarner Losh 		break;
2107*a94a63f0SWarner Losh 	}
2108*a94a63f0SWarner Losh 	case XPT_SET_TRAN_SETTINGS:
2109*a94a63f0SWarner Losh 	{
2110*a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2111*a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n");
2112*a94a63f0SWarner Losh 		sdhci_cam_settran_settings(slot, ccb);
2113*a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2114*a94a63f0SWarner Losh 		break;
2115*a94a63f0SWarner Losh 	}
2116*a94a63f0SWarner Losh 	case XPT_RESET_BUS:
2117*a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2118*a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n");
2119*a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2120*a94a63f0SWarner Losh 		break;
2121*a94a63f0SWarner Losh 	case XPT_MMC_IO:
2122*a94a63f0SWarner Losh 		/*
2123*a94a63f0SWarner Losh 		 * Here is the HW-dependent part of
2124*a94a63f0SWarner Losh 		 * sending the command to the underlying h/w
2125*a94a63f0SWarner Losh 		 * At some point in the future an interrupt comes.
2126*a94a63f0SWarner Losh 		 * Then the request will be marked as completed.
2127*a94a63f0SWarner Losh 		 */
2128*a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2129*a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_MMC_IO\n");
2130*a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INPROG;
2131*a94a63f0SWarner Losh 
2132*a94a63f0SWarner Losh 		sdhci_cam_handle_mmcio(sim, ccb);
2133*a94a63f0SWarner Losh 		return;
2134*a94a63f0SWarner Losh 		/* NOTREACHED */
2135*a94a63f0SWarner Losh 		break;
2136*a94a63f0SWarner Losh 	default:
2137*a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INVALID;
2138*a94a63f0SWarner Losh 		break;
2139*a94a63f0SWarner Losh 	}
2140*a94a63f0SWarner Losh 	xpt_done(ccb);
2141*a94a63f0SWarner Losh 	return;
2142*a94a63f0SWarner Losh }
2143*a94a63f0SWarner Losh 
2144*a94a63f0SWarner Losh void
2145*a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim)
2146*a94a63f0SWarner Losh {
2147*a94a63f0SWarner Losh 	return;
2148*a94a63f0SWarner Losh }
2149*a94a63f0SWarner Losh 
2150*a94a63f0SWarner Losh int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock) {
2151*a94a63f0SWarner Losh 	int max_clock, clock, i;
2152*a94a63f0SWarner Losh 
2153*a94a63f0SWarner Losh 	if (proposed_clock == 0)
2154*a94a63f0SWarner Losh 		return 0;
2155*a94a63f0SWarner Losh 	max_clock = slot->max_clk;
2156*a94a63f0SWarner Losh 	clock = max_clock;
2157*a94a63f0SWarner Losh 
2158*a94a63f0SWarner Losh 	if (slot->version < SDHCI_SPEC_300) {
2159*a94a63f0SWarner Losh 		for (i = 0; i < SDHCI_200_MAX_DIVIDER;
2160*a94a63f0SWarner Losh 		     i <<= 1) {
2161*a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2162*a94a63f0SWarner Losh 				break;
2163*a94a63f0SWarner Losh 			clock >>= 1;
2164*a94a63f0SWarner Losh 		}
2165*a94a63f0SWarner Losh 	} else {
2166*a94a63f0SWarner Losh 		for (i = 0; i < SDHCI_300_MAX_DIVIDER;
2167*a94a63f0SWarner Losh 		     i += 2) {
2168*a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2169*a94a63f0SWarner Losh 				break;
2170*a94a63f0SWarner Losh 			clock = max_clock / (i + 2);
2171*a94a63f0SWarner Losh 		}
2172*a94a63f0SWarner Losh 	}
2173*a94a63f0SWarner Losh 	return clock;
2174*a94a63f0SWarner Losh }
2175*a94a63f0SWarner Losh 
2176*a94a63f0SWarner Losh int
2177*a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb)
2178*a94a63f0SWarner Losh {
2179*a94a63f0SWarner Losh 	struct mmc_ios *ios;
2180*a94a63f0SWarner Losh 	struct mmc_ios *new_ios;
2181*a94a63f0SWarner Losh 	struct ccb_trans_settings_mmc *cts;
2182*a94a63f0SWarner Losh 
2183*a94a63f0SWarner Losh 	ios = &slot->host.ios;
2184*a94a63f0SWarner Losh 
2185*a94a63f0SWarner Losh 	cts = &ccb->cts.proto_specific.mmc;
2186*a94a63f0SWarner Losh 	new_ios = &cts->ios;
2187*a94a63f0SWarner Losh 
2188*a94a63f0SWarner Losh 	/* Update only requested fields */
2189*a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CLK) {
2190*a94a63f0SWarner Losh 		ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock);
2191*a94a63f0SWarner Losh 		slot_printf(slot, "Clock => %d\n", ios->clock);
2192*a94a63f0SWarner Losh 	}
2193*a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_VDD) {
2194*a94a63f0SWarner Losh 		ios->vdd = new_ios->vdd;
2195*a94a63f0SWarner Losh 		slot_printf(slot, "VDD => %d\n", ios->vdd);
2196*a94a63f0SWarner Losh 	}
2197*a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CS) {
2198*a94a63f0SWarner Losh 		ios->chip_select = new_ios->chip_select;
2199*a94a63f0SWarner Losh 		slot_printf(slot, "CS => %d\n", ios->chip_select);
2200*a94a63f0SWarner Losh 	}
2201*a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BW) {
2202*a94a63f0SWarner Losh 		ios->bus_width = new_ios->bus_width;
2203*a94a63f0SWarner Losh 		slot_printf(slot, "Bus width => %d\n", ios->bus_width);
2204*a94a63f0SWarner Losh 	}
2205*a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_PM) {
2206*a94a63f0SWarner Losh 		ios->power_mode = new_ios->power_mode;
2207*a94a63f0SWarner Losh 		slot_printf(slot, "Power mode => %d\n", ios->power_mode);
2208*a94a63f0SWarner Losh 	}
2209*a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BT) {
2210*a94a63f0SWarner Losh 		ios->timing = new_ios->timing;
2211*a94a63f0SWarner Losh 		slot_printf(slot, "Timing => %d\n", ios->timing);
2212*a94a63f0SWarner Losh 	}
2213*a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BM) {
2214*a94a63f0SWarner Losh 		ios->bus_mode = new_ios->bus_mode;
2215*a94a63f0SWarner Losh 		slot_printf(slot, "Bus mode => %d\n", ios->bus_mode);
2216*a94a63f0SWarner Losh 	}
2217*a94a63f0SWarner Losh 
2218*a94a63f0SWarner Losh         /* XXX Provide a way to call a chip-specific IOS update, required for TI */
2219*a94a63f0SWarner Losh 	return (sdhci_cam_update_ios(slot));
2220*a94a63f0SWarner Losh }
2221*a94a63f0SWarner Losh 
2222*a94a63f0SWarner Losh int
2223*a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot)
2224*a94a63f0SWarner Losh {
2225*a94a63f0SWarner Losh 	struct mmc_ios *ios = &slot->host.ios;
2226*a94a63f0SWarner Losh 
2227*a94a63f0SWarner Losh 	slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n",
2228*a94a63f0SWarner Losh 		    __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing);
2229*a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2230*a94a63f0SWarner Losh 	/* Do full reset on bus power down to clear from any state. */
2231*a94a63f0SWarner Losh 	if (ios->power_mode == power_off) {
2232*a94a63f0SWarner Losh 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
2233*a94a63f0SWarner Losh 		sdhci_init(slot);
2234*a94a63f0SWarner Losh 	}
2235*a94a63f0SWarner Losh 	/* Configure the bus. */
2236*a94a63f0SWarner Losh 	sdhci_set_clock(slot, ios->clock);
2237*a94a63f0SWarner Losh 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
2238*a94a63f0SWarner Losh 	if (ios->bus_width == bus_width_8) {
2239*a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
2240*a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2241*a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_4) {
2242*a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2243*a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
2244*a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_1) {
2245*a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2246*a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2247*a94a63f0SWarner Losh 	} else {
2248*a94a63f0SWarner Losh 		panic("Invalid bus width: %d", ios->bus_width);
2249*a94a63f0SWarner Losh 	}
2250*a94a63f0SWarner Losh 	if (ios->timing == bus_timing_hs &&
2251*a94a63f0SWarner Losh 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
2252*a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_HISPD;
2253*a94a63f0SWarner Losh 	else
2254*a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
2255*a94a63f0SWarner Losh 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
2256*a94a63f0SWarner Losh 	/* Some controllers like reset after bus changes. */
2257*a94a63f0SWarner Losh 	if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
2258*a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2259*a94a63f0SWarner Losh 
2260*a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2261*a94a63f0SWarner Losh 	return (0);
2262*a94a63f0SWarner Losh }
2263*a94a63f0SWarner Losh 
2264*a94a63f0SWarner Losh int
2265*a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb)
2266*a94a63f0SWarner Losh {
2267*a94a63f0SWarner Losh 	struct ccb_mmcio *mmcio;
2268*a94a63f0SWarner Losh 
2269*a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
2270*a94a63f0SWarner Losh 
2271*a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2272*a94a63f0SWarner Losh /*	if (slot->req != NULL) {
2273*a94a63f0SWarner Losh 		SDHCI_UNLOCK(slot);
2274*a94a63f0SWarner Losh 		return (EBUSY);
2275*a94a63f0SWarner Losh 	}
2276*a94a63f0SWarner Losh */
2277*a94a63f0SWarner Losh 	if (sdhci_debug > 1) {
2278*a94a63f0SWarner Losh 		slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
2279*a94a63f0SWarner Losh 			    mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags,
2280*a94a63f0SWarner Losh 			    mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0,
2281*a94a63f0SWarner Losh 			    mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0);
2282*a94a63f0SWarner Losh 	}
2283*a94a63f0SWarner Losh 	if (mmcio->cmd.data != NULL) {
2284*a94a63f0SWarner Losh 		if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0)
2285*a94a63f0SWarner Losh 			panic("data->len = %d, data->flags = %d -- something is b0rked",
2286*a94a63f0SWarner Losh 			      (int)mmcio->cmd.data->len, mmcio->cmd.data->flags);
2287*a94a63f0SWarner Losh 	}
2288*a94a63f0SWarner Losh 	slot->ccb = ccb;
2289*a94a63f0SWarner Losh 	slot->flags = 0;
2290*a94a63f0SWarner Losh 	sdhci_start(slot);
2291*a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2292*a94a63f0SWarner Losh 	if (dumping) {
2293*a94a63f0SWarner Losh 		while (slot->ccb != NULL) {
2294*a94a63f0SWarner Losh 			sdhci_generic_intr(slot);
2295*a94a63f0SWarner Losh 			DELAY(10);
2296*a94a63f0SWarner Losh 		}
2297*a94a63f0SWarner Losh 	}
2298*a94a63f0SWarner Losh 	return (0);
2299*a94a63f0SWarner Losh }
2300*a94a63f0SWarner Losh 
2301d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1);
2302