xref: /freebsd/sys/dev/sdhci/sdhci.c (revision 7b86593f0f6cbfba2883058c6c9d2389f1c581aa)
1831f5dcfSAlexander Motin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4831f5dcfSAlexander Motin  * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
5aca38eabSMarius Strobl  * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
6831f5dcfSAlexander Motin  * All rights reserved.
7831f5dcfSAlexander Motin  *
8831f5dcfSAlexander Motin  * Redistribution and use in source and binary forms, with or without
9831f5dcfSAlexander Motin  * modification, are permitted provided that the following conditions
10831f5dcfSAlexander Motin  * are met:
11831f5dcfSAlexander Motin  * 1. Redistributions of source code must retain the above copyright
12831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer.
13831f5dcfSAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
14831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
15831f5dcfSAlexander Motin  *    documentation and/or other materials provided with the distribution.
16831f5dcfSAlexander Motin  *
17831f5dcfSAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18831f5dcfSAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19831f5dcfSAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20831f5dcfSAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21831f5dcfSAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22831f5dcfSAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23831f5dcfSAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24831f5dcfSAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25831f5dcfSAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26831f5dcfSAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27831f5dcfSAlexander Motin  */
28831f5dcfSAlexander Motin 
29831f5dcfSAlexander Motin #include <sys/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>
35aca38eabSMarius Strobl #include <sys/kobj.h>
36ab00a509SMarius Strobl #include <sys/libkern.h>
37831f5dcfSAlexander Motin #include <sys/lock.h>
38aca38eabSMarius Strobl #include <sys/malloc.h>
39831f5dcfSAlexander Motin #include <sys/module.h>
40831f5dcfSAlexander Motin #include <sys/mutex.h>
41831f5dcfSAlexander Motin #include <sys/resource.h>
42831f5dcfSAlexander Motin #include <sys/rman.h>
435b69a497SAlexander Motin #include <sys/sysctl.h>
44831f5dcfSAlexander Motin #include <sys/taskqueue.h>
45d00c1f7fSBartlomiej Grzesik #include <sys/sbuf.h>
46831f5dcfSAlexander Motin 
47831f5dcfSAlexander Motin #include <machine/bus.h>
48831f5dcfSAlexander Motin #include <machine/resource.h>
49831f5dcfSAlexander Motin #include <machine/stdarg.h>
50831f5dcfSAlexander Motin 
51831f5dcfSAlexander Motin #include <dev/mmc/bridge.h>
52831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h>
53831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h>
54831f5dcfSAlexander Motin 
55aca38eabSMarius Strobl #include <dev/sdhci/sdhci.h>
56aca38eabSMarius Strobl 
57a94a63f0SWarner Losh #include <cam/cam.h>
58a94a63f0SWarner Losh #include <cam/cam_ccb.h>
59a94a63f0SWarner Losh #include <cam/cam_debug.h>
60a94a63f0SWarner Losh #include <cam/cam_sim.h>
61a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h>
62a94a63f0SWarner Losh 
63831f5dcfSAlexander Motin #include "mmcbr_if.h"
64d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h"
65831f5dcfSAlexander Motin 
66a94a63f0SWarner Losh #include "opt_mmccam.h"
67a94a63f0SWarner Losh 
687029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
697029da5cSPawel Biernacki     "sdhci driver");
70831f5dcfSAlexander Motin 
71a94a63f0SWarner Losh static int sdhci_debug = 0;
727e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0,
737e6ccea3SMarius Strobl     "Debug level");
740f34084fSMarius Strobl u_int sdhci_quirk_clear = 0;
75*7b86593fSJustin Hibbits SYSCTL_UINT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear,
760f34084fSMarius Strobl     0, "Mask of quirks to clear");
770f34084fSMarius Strobl u_int sdhci_quirk_set = 0;
78*7b86593fSJustin Hibbits SYSCTL_UINT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0,
790f34084fSMarius Strobl     "Mask of quirks to set");
805b69a497SAlexander Motin 
81d6b3aaf8SOleksandr Tymoshenko #define	RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
82d6b3aaf8SOleksandr Tymoshenko #define	RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
83d6b3aaf8SOleksandr Tymoshenko #define	RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
84d6b3aaf8SOleksandr Tymoshenko #define	RD_MULTI_4(slot, off, ptr, count)	\
85d6b3aaf8SOleksandr Tymoshenko     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
86831f5dcfSAlexander Motin 
87d6b3aaf8SOleksandr Tymoshenko #define	WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
88d6b3aaf8SOleksandr Tymoshenko #define	WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
89d6b3aaf8SOleksandr Tymoshenko #define	WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
90d6b3aaf8SOleksandr Tymoshenko #define	WR_MULTI_4(slot, off, ptr, count)	\
91d6b3aaf8SOleksandr Tymoshenko     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
92831f5dcfSAlexander Motin 
936dea80e6SMarius Strobl static void sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err);
94aca38eabSMarius Strobl static void sdhci_card_poll(void *arg);
95aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending);
966dea80e6SMarius Strobl static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask);
976dea80e6SMarius Strobl static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask);
98aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset);
996dea80e6SMarius Strobl static void sdhci_handle_card_present_locked(struct sdhci_slot *slot,
1006dea80e6SMarius Strobl     bool is_present);
1016dea80e6SMarius Strobl static void sdhci_finish_command(struct sdhci_slot *slot);
1026dea80e6SMarius Strobl static void sdhci_init(struct sdhci_slot *slot);
1036dea80e6SMarius Strobl static void sdhci_read_block_pio(struct sdhci_slot *slot);
1046dea80e6SMarius Strobl static void sdhci_req_done(struct sdhci_slot *slot);
105aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req);
106aca38eabSMarius Strobl static void sdhci_retune(void *arg);
107831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
1086dea80e6SMarius Strobl static void sdhci_set_power(struct sdhci_slot *slot, u_char power);
1096dea80e6SMarius Strobl static void sdhci_set_transfer_mode(struct sdhci_slot *slot,
110ab00a509SMarius Strobl    const struct mmc_data *data);
111831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot);
1126dea80e6SMarius Strobl static void sdhci_timeout(void *arg);
1136dea80e6SMarius Strobl static void sdhci_start_command(struct sdhci_slot *slot,
1146dea80e6SMarius Strobl    struct mmc_command *cmd);
115ab00a509SMarius Strobl static void sdhci_start_data(struct sdhci_slot *slot,
116ab00a509SMarius Strobl    const struct mmc_data *data);
1176dea80e6SMarius Strobl static void sdhci_write_block_pio(struct sdhci_slot *slot);
1186dea80e6SMarius Strobl static void sdhci_transfer_pio(struct sdhci_slot *slot);
119831f5dcfSAlexander Motin 
12015c440e1SWarner Losh #ifdef MMCCAM
121a94a63f0SWarner Losh /* CAM-related */
122a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb);
123ab00a509SMarius Strobl static int sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot,
1246dea80e6SMarius Strobl     int proposed_clock);
125a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim);
1266dea80e6SMarius Strobl static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb);
127a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb);
1286dea80e6SMarius Strobl static int sdhci_cam_update_ios(struct sdhci_slot *slot);
12915c440e1SWarner Losh #endif
130a94a63f0SWarner Losh 
131831f5dcfSAlexander Motin /* helper routines */
132016f9657SMarcin Wojtas static int sdhci_dma_alloc(struct sdhci_slot *slot);
133ab00a509SMarius Strobl static void sdhci_dma_free(struct sdhci_slot *slot);
134d00c1f7fSBartlomiej Grzesik static void sdhci_dumpcaps(struct sdhci_slot *slot);
135d00c1f7fSBartlomiej Grzesik static void sdhci_dumpcaps_buf(struct sdhci_slot *slot, struct sbuf *s);
1360f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot);
137d00c1f7fSBartlomiej Grzesik static void sdhci_dumpregs_buf(struct sdhci_slot *slot, struct sbuf *s);
138d00c1f7fSBartlomiej Grzesik static int sdhci_syctl_dumpcaps(SYSCTL_HANDLER_ARGS);
139d00c1f7fSBartlomiej Grzesik static int sdhci_syctl_dumpregs(SYSCTL_HANDLER_ARGS);
1406dea80e6SMarius Strobl static void sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs,
1416dea80e6SMarius Strobl     int error);
142ab00a509SMarius Strobl static int slot_printf(const struct sdhci_slot *slot, const char * fmt, ...)
1430f34084fSMarius Strobl     __printflike(2, 3);
144d00c1f7fSBartlomiej Grzesik static int slot_sprintf(const struct sdhci_slot *slot, struct sbuf *s,
145d00c1f7fSBartlomiej Grzesik     const char * fmt, ...) __printflike(3, 4);
146ab00a509SMarius Strobl static uint32_t sdhci_tuning_intmask(const struct sdhci_slot *slot);
1470f34084fSMarius Strobl 
148831f5dcfSAlexander Motin #define	SDHCI_LOCK(_slot)		mtx_lock(&(_slot)->mtx)
149831f5dcfSAlexander Motin #define	SDHCI_UNLOCK(_slot)		mtx_unlock(&(_slot)->mtx)
150831f5dcfSAlexander Motin #define	SDHCI_LOCK_INIT(_slot) \
151831f5dcfSAlexander Motin 	mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
152831f5dcfSAlexander Motin #define	SDHCI_LOCK_DESTROY(_slot)	mtx_destroy(&_slot->mtx);
153831f5dcfSAlexander Motin #define	SDHCI_ASSERT_LOCKED(_slot)	mtx_assert(&_slot->mtx, MA_OWNED);
154831f5dcfSAlexander Motin #define	SDHCI_ASSERT_UNLOCKED(_slot)	mtx_assert(&_slot->mtx, MA_NOTOWNED);
155831f5dcfSAlexander Motin 
15633aad34dSOleksandr Tymoshenko #define	SDHCI_DEFAULT_MAX_FREQ	50
15733aad34dSOleksandr Tymoshenko 
15857677a3aSOleksandr Tymoshenko #define	SDHCI_200_MAX_DIVIDER	256
15957677a3aSOleksandr Tymoshenko #define	SDHCI_300_MAX_DIVIDER	2046
16057677a3aSOleksandr Tymoshenko 
161639f59f0SIan Lepore #define	SDHCI_CARD_PRESENT_TICKS	(hz / 5)
162639f59f0SIan Lepore #define	SDHCI_INSERT_DELAY_TICKS	(hz / 2)
163639f59f0SIan Lepore 
16493efdc63SAdrian Chadd /*
16593efdc63SAdrian Chadd  * Broadcom BCM577xx Controller Constants
16693efdc63SAdrian Chadd  */
1671bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */
1681bacf3beSMarius Strobl #define	BCM577XX_DEFAULT_MAX_DIVIDER	256
1691bacf3beSMarius Strobl /* Alternative clock's base frequency. */
1701bacf3beSMarius Strobl #define	BCM577XX_ALT_CLOCK_BASE		63000000
17193efdc63SAdrian Chadd 
17293efdc63SAdrian Chadd #define	BCM577XX_HOST_CONTROL		0x198
17393efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_MASK	0xFFFFCFFF
17493efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_SHIFT	12
17593efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_DEFAULT	0x0
17693efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_64MHZ	0x3
17793efdc63SAdrian Chadd 
178831f5dcfSAlexander Motin static void
179831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
180831f5dcfSAlexander Motin {
1817e6ccea3SMarius Strobl 
182831f5dcfSAlexander Motin 	if (error != 0) {
183831f5dcfSAlexander Motin 		printf("getaddr: error %d\n", error);
184831f5dcfSAlexander Motin 		return;
185831f5dcfSAlexander Motin 	}
186831f5dcfSAlexander Motin 	*(bus_addr_t *)arg = segs[0].ds_addr;
187831f5dcfSAlexander Motin }
188831f5dcfSAlexander Motin 
189d6b3aaf8SOleksandr Tymoshenko static int
190ab00a509SMarius Strobl slot_printf(const struct sdhci_slot *slot, const char * fmt, ...)
191d6b3aaf8SOleksandr Tymoshenko {
19227d72fe1SBjoern A. Zeeb 	char buf[128];
193d6b3aaf8SOleksandr Tymoshenko 	va_list ap;
194d6b3aaf8SOleksandr Tymoshenko 	int retval;
195d6b3aaf8SOleksandr Tymoshenko 
19627d72fe1SBjoern A. Zeeb 	/*
19727d72fe1SBjoern A. Zeeb 	 * Make sure we print a single line all together rather than in two
19827d72fe1SBjoern A. Zeeb 	 * halves to avoid console gibberish bingo.
19927d72fe1SBjoern A. Zeeb 	 */
200d6b3aaf8SOleksandr Tymoshenko 	va_start(ap, fmt);
20127d72fe1SBjoern A. Zeeb 	retval = vsnprintf(buf, sizeof(buf), fmt, ap);
202d6b3aaf8SOleksandr Tymoshenko 	va_end(ap);
20327d72fe1SBjoern A. Zeeb 
20427d72fe1SBjoern A. Zeeb 	retval += printf("%s-slot%d: %s",
20527d72fe1SBjoern A. Zeeb 	    device_get_nameunit(slot->bus), slot->num, buf);
206d6b3aaf8SOleksandr Tymoshenko 	return (retval);
207d6b3aaf8SOleksandr Tymoshenko }
208d6b3aaf8SOleksandr Tymoshenko 
209d00c1f7fSBartlomiej Grzesik static int
210d00c1f7fSBartlomiej Grzesik slot_sprintf(const struct sdhci_slot *slot, struct sbuf *s,
211d00c1f7fSBartlomiej Grzesik     const char * fmt, ...)
212d00c1f7fSBartlomiej Grzesik {
213d00c1f7fSBartlomiej Grzesik 	va_list ap;
214d00c1f7fSBartlomiej Grzesik 	int retval;
215d00c1f7fSBartlomiej Grzesik 
216d00c1f7fSBartlomiej Grzesik 	retval = sbuf_printf(s, "%s-slot%d: ", device_get_nameunit(slot->bus), slot->num);
217d00c1f7fSBartlomiej Grzesik 
218d00c1f7fSBartlomiej Grzesik 	va_start(ap, fmt);
219d00c1f7fSBartlomiej Grzesik 	retval += sbuf_vprintf(s, fmt, ap);
220d00c1f7fSBartlomiej Grzesik 	va_end(ap);
221d00c1f7fSBartlomiej Grzesik 
222d00c1f7fSBartlomiej Grzesik 	return (retval);
223d00c1f7fSBartlomiej Grzesik }
224d00c1f7fSBartlomiej Grzesik 
225d00c1f7fSBartlomiej Grzesik static void
226d00c1f7fSBartlomiej Grzesik sdhci_dumpregs_buf(struct sdhci_slot *slot, struct sbuf *s)
227d00c1f7fSBartlomiej Grzesik {
228d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "============== REGISTER DUMP ==============\n");
229d00c1f7fSBartlomiej Grzesik 
230d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Sys addr: 0x%08x | Version:  0x%08x\n",
231d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
232d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
233d00c1f7fSBartlomiej Grzesik 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
234d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Argument: 0x%08x | Trn mode: 0x%08x\n",
235d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
236d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Present:  0x%08x | Host ctl: 0x%08x\n",
237d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
238d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Power:    0x%08x | Blk gap:  0x%08x\n",
239d00c1f7fSBartlomiej Grzesik 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
240d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Wake-up:  0x%08x | Clock:    0x%08x\n",
241d00c1f7fSBartlomiej Grzesik 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
242d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Timeout:  0x%08x | Int stat: 0x%08x\n",
243d00c1f7fSBartlomiej Grzesik 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
244d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Int enab: 0x%08x | Sig enab: 0x%08x\n",
245d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
246d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "AC12 err: 0x%08x | Host ctl2:0x%08x\n",
247d00c1f7fSBartlomiej Grzesik 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2));
248d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Caps:     0x%08x | Caps2:    0x%08x\n",
249d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2));
250d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Max curr: 0x%08x | ADMA err: 0x%08x\n",
251d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR));
252d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "ADMA addr:0x%08x | Slot int: 0x%08x\n",
253d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS));
254d00c1f7fSBartlomiej Grzesik 
255d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "===========================================\n");
256d00c1f7fSBartlomiej Grzesik }
257d00c1f7fSBartlomiej Grzesik 
258831f5dcfSAlexander Motin static void
259831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot)
260831f5dcfSAlexander Motin {
261d00c1f7fSBartlomiej Grzesik 	struct sbuf s;
2627e6ccea3SMarius Strobl 
2633ac5012eSBartlomiej Grzesik 	if (sbuf_new(&s, NULL, 1024, SBUF_NOWAIT | SBUF_AUTOEXTEND) == NULL) {
2643ac5012eSBartlomiej Grzesik 		slot_printf(slot, "sdhci_dumpregs: Failed to allocate memory for sbuf\n");
2653ac5012eSBartlomiej Grzesik 		return;
2663ac5012eSBartlomiej Grzesik 	}
2673ac5012eSBartlomiej Grzesik 
268d00c1f7fSBartlomiej Grzesik 	sbuf_set_drain(&s, &sbuf_printf_drain, NULL);
269d00c1f7fSBartlomiej Grzesik 	sdhci_dumpregs_buf(slot, &s);
270d00c1f7fSBartlomiej Grzesik 	sbuf_finish(&s);
271d00c1f7fSBartlomiej Grzesik 	sbuf_delete(&s);
272d00c1f7fSBartlomiej Grzesik }
273831f5dcfSAlexander Motin 
274d00c1f7fSBartlomiej Grzesik static int
275d00c1f7fSBartlomiej Grzesik sdhci_syctl_dumpregs(SYSCTL_HANDLER_ARGS)
276d00c1f7fSBartlomiej Grzesik {
277d00c1f7fSBartlomiej Grzesik 	struct sdhci_slot *slot = arg1;
278d00c1f7fSBartlomiej Grzesik 	struct sbuf s;
279831f5dcfSAlexander Motin 
280d00c1f7fSBartlomiej Grzesik 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
281d00c1f7fSBartlomiej Grzesik 	sbuf_putc(&s, '\n');
282d00c1f7fSBartlomiej Grzesik 	sdhci_dumpregs_buf(slot, &s);
283d00c1f7fSBartlomiej Grzesik 	sbuf_finish(&s);
284d00c1f7fSBartlomiej Grzesik 	sbuf_delete(&s);
285d00c1f7fSBartlomiej Grzesik 
286d00c1f7fSBartlomiej Grzesik 	return (0);
287d00c1f7fSBartlomiej Grzesik }
288d00c1f7fSBartlomiej Grzesik 
289d00c1f7fSBartlomiej Grzesik static void
290d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps_buf(struct sdhci_slot *slot, struct sbuf *s)
291d00c1f7fSBartlomiej Grzesik {
292d00c1f7fSBartlomiej Grzesik 	int host_caps = slot->host.caps;
293d00c1f7fSBartlomiej Grzesik 	int caps = slot->caps;
294d00c1f7fSBartlomiej Grzesik 
295d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,
296d00c1f7fSBartlomiej Grzesik 	    "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n",
297d00c1f7fSBartlomiej Grzesik 	    slot->max_clk / 1000000,
298d00c1f7fSBartlomiej Grzesik 	    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
299d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
300d00c1f7fSBartlomiej Grzesik 	    ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
301d00c1f7fSBartlomiej Grzesik 	    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
302d00c1f7fSBartlomiej Grzesik 	    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
303d00c1f7fSBartlomiej Grzesik 	    ((caps & SDHCI_CAN_VDD_180) &&
304d00c1f7fSBartlomiej Grzesik 	    (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "",
305d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "",
306d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "",
307d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "",
308d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "",
309d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "",
310d00c1f7fSBartlomiej Grzesik 	    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO",
311d00c1f7fSBartlomiej Grzesik 	    (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" :
312d00c1f7fSBartlomiej Grzesik 	    (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" :
313d00c1f7fSBartlomiej Grzesik 	    "removable");
314d00c1f7fSBartlomiej Grzesik 	if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 |
315d00c1f7fSBartlomiej Grzesik 	    MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE))
316d00c1f7fSBartlomiej Grzesik 		slot_sprintf(slot, s, "eMMC:%s%s%s%s\n",
317d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "",
318d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "",
319d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "",
320d00c1f7fSBartlomiej Grzesik 		    ((host_caps &
321d00c1f7fSBartlomiej Grzesik 		    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ==
322d00c1f7fSBartlomiej Grzesik 		    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ?
323d00c1f7fSBartlomiej Grzesik 		    " HS400ES" : "");
324d00c1f7fSBartlomiej Grzesik 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
325d00c1f7fSBartlomiej Grzesik 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104))
326d00c1f7fSBartlomiej Grzesik 		slot_sprintf(slot, s, "UHS-I:%s%s%s%s%s\n",
327d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "",
328d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "",
329d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "",
330d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "",
331d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : "");
332d00c1f7fSBartlomiej Grzesik 	if (slot->opt & SDHCI_TUNING_SUPPORTED)
333d00c1f7fSBartlomiej Grzesik 		slot_sprintf(slot, s,
334d00c1f7fSBartlomiej Grzesik 		    "Re-tuning count %d secs, mode %d\n",
335d00c1f7fSBartlomiej Grzesik 		    slot->retune_count, slot->retune_mode + 1);
336d00c1f7fSBartlomiej Grzesik }
337d00c1f7fSBartlomiej Grzesik 
338d00c1f7fSBartlomiej Grzesik static void
339d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps(struct sdhci_slot *slot)
340d00c1f7fSBartlomiej Grzesik {
341d00c1f7fSBartlomiej Grzesik 	struct sbuf s;
342d00c1f7fSBartlomiej Grzesik 
3433ac5012eSBartlomiej Grzesik 	if (sbuf_new(&s, NULL, 1024, SBUF_NOWAIT | SBUF_AUTOEXTEND) == NULL) {
3443ac5012eSBartlomiej Grzesik 		slot_printf(slot, "sdhci_dumpcaps: Failed to allocate memory for sbuf\n");
3453ac5012eSBartlomiej Grzesik 		return;
3463ac5012eSBartlomiej Grzesik 	}
3473ac5012eSBartlomiej Grzesik 
348d00c1f7fSBartlomiej Grzesik 	sbuf_set_drain(&s, &sbuf_printf_drain, NULL);
349d00c1f7fSBartlomiej Grzesik 	sdhci_dumpcaps_buf(slot, &s);
350d00c1f7fSBartlomiej Grzesik 	sbuf_finish(&s);
351d00c1f7fSBartlomiej Grzesik 	sbuf_delete(&s);
352d00c1f7fSBartlomiej Grzesik }
353d00c1f7fSBartlomiej Grzesik 
354d00c1f7fSBartlomiej Grzesik static int
355d00c1f7fSBartlomiej Grzesik sdhci_syctl_dumpcaps(SYSCTL_HANDLER_ARGS)
356d00c1f7fSBartlomiej Grzesik {
357d00c1f7fSBartlomiej Grzesik 	struct sdhci_slot *slot = arg1;
358d00c1f7fSBartlomiej Grzesik 	struct sbuf s;
359d00c1f7fSBartlomiej Grzesik 
360d00c1f7fSBartlomiej Grzesik 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
361d00c1f7fSBartlomiej Grzesik 	sbuf_putc(&s, '\n');
362d00c1f7fSBartlomiej Grzesik 	sdhci_dumpcaps_buf(slot, &s);
363d00c1f7fSBartlomiej Grzesik 	sbuf_finish(&s);
364d00c1f7fSBartlomiej Grzesik 	sbuf_delete(&s);
365d00c1f7fSBartlomiej Grzesik 
366d00c1f7fSBartlomiej Grzesik 	return (0);
367831f5dcfSAlexander Motin }
368831f5dcfSAlexander Motin 
369aca38eabSMarius Strobl static uint32_t
370ab00a509SMarius Strobl sdhci_tuning_intmask(const struct sdhci_slot *slot)
371aca38eabSMarius Strobl {
372aca38eabSMarius Strobl 	uint32_t intmask;
373aca38eabSMarius Strobl 
374aca38eabSMarius Strobl 	intmask = 0;
37578f8baa8SMarius Strobl 	if (slot->opt & SDHCI_TUNING_ENABLED) {
376aca38eabSMarius Strobl 		intmask |= SDHCI_INT_TUNEERR;
377aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_2 ||
378aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_3)
379aca38eabSMarius Strobl 			intmask |= SDHCI_INT_RETUNE;
380aca38eabSMarius Strobl 	}
381aca38eabSMarius Strobl 	return (intmask);
382aca38eabSMarius Strobl }
383aca38eabSMarius Strobl 
384831f5dcfSAlexander Motin static void
385831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot)
386831f5dcfSAlexander Motin {
387831f5dcfSAlexander Motin 
388b8f94506SArtur Rojek 	SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL);
389831f5dcfSAlexander Motin 
390831f5dcfSAlexander Motin 	/* Enable interrupts. */
391831f5dcfSAlexander Motin 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
392831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
393831f5dcfSAlexander Motin 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
394831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
395831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
3963685b398SWarner Losh 	    SDHCI_INT_ACMD12ERR;
397639f59f0SIan Lepore 
398639f59f0SIan Lepore 	if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
399639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
400639f59f0SIan Lepore 		slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
401639f59f0SIan Lepore 	}
402639f59f0SIan Lepore 
403cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
404831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
405831f5dcfSAlexander Motin }
406831f5dcfSAlexander Motin 
407831f5dcfSAlexander Motin static void
408831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
409831f5dcfSAlexander Motin {
41093efdc63SAdrian Chadd 	uint32_t clk_base;
41193efdc63SAdrian Chadd 	uint32_t clk_sel;
412831f5dcfSAlexander Motin 	uint32_t res;
413831f5dcfSAlexander Motin 	uint16_t clk;
4148f3b7d56SOleksandr Tymoshenko 	uint16_t div;
415831f5dcfSAlexander Motin 	int timeout;
416831f5dcfSAlexander Motin 
417831f5dcfSAlexander Motin 	if (clock == slot->clock)
418831f5dcfSAlexander Motin 		return;
419e00774a9SSøren Schmidt 	clock = SDHCI_SET_CLOCK(slot->bus, slot, clock);
420831f5dcfSAlexander Motin 	slot->clock = clock;
421831f5dcfSAlexander Motin 
422831f5dcfSAlexander Motin 	/* Turn off the clock. */
4234ddc0172SIan Lepore 	clk = RD2(slot, SDHCI_CLOCK_CONTROL);
4244ddc0172SIan Lepore 	WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
425b440e965SMarius Strobl 	/* If no clock requested - leave it so. */
426831f5dcfSAlexander Motin 	if (clock == 0)
427831f5dcfSAlexander Motin 		return;
428ceb9e9f7SIan Lepore 
42993efdc63SAdrian Chadd 	/* Determine the clock base frequency */
43093efdc63SAdrian Chadd 	clk_base = slot->max_clk;
43193efdc63SAdrian Chadd 	if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
4321bacf3beSMarius Strobl 		clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) &
4331bacf3beSMarius Strobl 		    BCM577XX_CTRL_CLKSEL_MASK;
43493efdc63SAdrian Chadd 
4351bacf3beSMarius Strobl 		/*
4361bacf3beSMarius Strobl 		 * Select clock source appropriate for the requested frequency.
4371bacf3beSMarius Strobl 		 */
43893efdc63SAdrian Chadd 		if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
43993efdc63SAdrian Chadd 			clk_base = BCM577XX_ALT_CLOCK_BASE;
4401bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ <<
4411bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
44293efdc63SAdrian Chadd 		} else {
4431bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT <<
4441bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
44593efdc63SAdrian Chadd 		}
44693efdc63SAdrian Chadd 
44793efdc63SAdrian Chadd 		WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
44893efdc63SAdrian Chadd 	}
44993efdc63SAdrian Chadd 
450ceb9e9f7SIan Lepore 	/* Recalculate timeout clock frequency based on the new sd clock. */
451ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
452ceb9e9f7SIan Lepore 		slot->timeout_clk = slot->clock / 1000;
453ceb9e9f7SIan Lepore 
4548f3b7d56SOleksandr Tymoshenko 	if (slot->version < SDHCI_SPEC_300) {
455831f5dcfSAlexander Motin 		/* Looking for highest freq <= clock. */
45693efdc63SAdrian Chadd 		res = clk_base;
45757677a3aSOleksandr Tymoshenko 		for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
458831f5dcfSAlexander Motin 			if (res <= clock)
459831f5dcfSAlexander Motin 				break;
460831f5dcfSAlexander Motin 			res >>= 1;
461831f5dcfSAlexander Motin 		}
462831f5dcfSAlexander Motin 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
4638f3b7d56SOleksandr Tymoshenko 		div >>= 1;
464c11bbc7dSMarius Strobl 	} else {
4658f3b7d56SOleksandr Tymoshenko 		/* Version 3.0 divisors are multiples of two up to 1023 * 2 */
46693efdc63SAdrian Chadd 		if (clock >= clk_base)
46757677a3aSOleksandr Tymoshenko 			div = 0;
4688f3b7d56SOleksandr Tymoshenko 		else {
46957677a3aSOleksandr Tymoshenko 			for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
47093efdc63SAdrian Chadd 				if ((clk_base / div) <= clock)
4718f3b7d56SOleksandr Tymoshenko 					break;
4728f3b7d56SOleksandr Tymoshenko 			}
4738f3b7d56SOleksandr Tymoshenko 		}
4748f3b7d56SOleksandr Tymoshenko 		div >>= 1;
4758f3b7d56SOleksandr Tymoshenko 	}
4768f3b7d56SOleksandr Tymoshenko 
4778f3b7d56SOleksandr Tymoshenko 	if (bootverbose || sdhci_debug)
47893efdc63SAdrian Chadd 		slot_printf(slot, "Divider %d for freq %d (base %d)\n",
47993efdc63SAdrian Chadd 			div, clock, clk_base);
4808f3b7d56SOleksandr Tymoshenko 
481831f5dcfSAlexander Motin 	/* Now we have got divider, set it. */
4828f3b7d56SOleksandr Tymoshenko 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
4838f3b7d56SOleksandr Tymoshenko 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
4848f3b7d56SOleksandr Tymoshenko 		<< SDHCI_DIVIDER_HI_SHIFT;
4858f3b7d56SOleksandr Tymoshenko 
486831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
487831f5dcfSAlexander Motin 	/* Enable clock. */
488831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_INT_EN;
489831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
490831f5dcfSAlexander Motin 	/* Wait up to 10 ms until it stabilize. */
491831f5dcfSAlexander Motin 	timeout = 10;
492831f5dcfSAlexander Motin 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
493831f5dcfSAlexander Motin 		& SDHCI_CLOCK_INT_STABLE)) {
494831f5dcfSAlexander Motin 		if (timeout == 0) {
495831f5dcfSAlexander Motin 			slot_printf(slot,
496831f5dcfSAlexander Motin 			    "Internal clock never stabilised.\n");
497831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
498831f5dcfSAlexander Motin 			return;
499831f5dcfSAlexander Motin 		}
500831f5dcfSAlexander Motin 		timeout--;
501831f5dcfSAlexander Motin 		DELAY(1000);
502831f5dcfSAlexander Motin 	}
503831f5dcfSAlexander Motin 	/* Pass clock signal to the bus. */
504831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_CARD_EN;
505831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
506831f5dcfSAlexander Motin }
507831f5dcfSAlexander Motin 
508831f5dcfSAlexander Motin static void
509831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power)
510831f5dcfSAlexander Motin {
51185083a80SMarius Strobl 	int i;
512831f5dcfSAlexander Motin 	uint8_t pwr;
513831f5dcfSAlexander Motin 
514831f5dcfSAlexander Motin 	if (slot->power == power)
515831f5dcfSAlexander Motin 		return;
516d6b3aaf8SOleksandr Tymoshenko 
517831f5dcfSAlexander Motin 	slot->power = power;
518831f5dcfSAlexander Motin 
519831f5dcfSAlexander Motin 	/* Turn off the power. */
520831f5dcfSAlexander Motin 	pwr = 0;
521831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
522b440e965SMarius Strobl 	/* If power down requested - leave it so. */
523831f5dcfSAlexander Motin 	if (power == 0)
524831f5dcfSAlexander Motin 		return;
525831f5dcfSAlexander Motin 	/* Set voltage. */
526831f5dcfSAlexander Motin 	switch (1 << power) {
527831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
528831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
529831f5dcfSAlexander Motin 		break;
530831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
531831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
532831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
533831f5dcfSAlexander Motin 		break;
534831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
535831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
536831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
537831f5dcfSAlexander Motin 		break;
538831f5dcfSAlexander Motin 	}
539831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
54085083a80SMarius Strobl 	/*
54185083a80SMarius Strobl 	 * Turn on VDD1 power.  Note that at least some Intel controllers can
54285083a80SMarius Strobl 	 * fail to enable bus power on the first try after transiting from D3
5438022c8ebSMarius Strobl 	 * to D0, so we give them up to 2 ms.
54485083a80SMarius Strobl 	 */
545831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
54685083a80SMarius Strobl 	for (i = 0; i < 20; i++) {
547831f5dcfSAlexander Motin 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
54885083a80SMarius Strobl 		if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)
54985083a80SMarius Strobl 			break;
55085083a80SMarius Strobl 		DELAY(100);
55185083a80SMarius Strobl 	}
55285083a80SMarius Strobl 	if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON))
5534d52f81dSIan Lepore 		slot_printf(slot, "Bus power failed to enable\n");
554a2832f9fSMarius Strobl 
555a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) {
556a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10);
557a2832f9fSMarius Strobl 		DELAY(10);
558a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
559a2832f9fSMarius Strobl 		DELAY(300);
560a2832f9fSMarius Strobl 	}
561831f5dcfSAlexander Motin }
562831f5dcfSAlexander Motin 
563831f5dcfSAlexander Motin static void
564831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
565831f5dcfSAlexander Motin {
566831f5dcfSAlexander Motin 	uint32_t data;
567831f5dcfSAlexander Motin 	char *buffer;
568831f5dcfSAlexander Motin 	size_t left;
569831f5dcfSAlexander Motin 
570831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
571831f5dcfSAlexander Motin 	buffer += slot->offset;
572831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
5735d5ae066SIlya Bakulin #ifdef MMCCAM
5745d5ae066SIlya Bakulin 	if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE)
5755d5ae066SIlya Bakulin 		left = min(slot->curcmd->data->block_size,
5765d5ae066SIlya Bakulin 		    slot->curcmd->data->len - slot->offset);
5775d5ae066SIlya Bakulin 	else
5785d5ae066SIlya Bakulin #endif
579831f5dcfSAlexander Motin 		left = min(512, slot->curcmd->data->len - slot->offset);
580831f5dcfSAlexander Motin 	slot->offset += left;
581831f5dcfSAlexander Motin 
582831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
583d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
584831f5dcfSAlexander Motin 		DELAY(10);
585ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
586831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
587831f5dcfSAlexander Motin 		while (left > 3) {
588831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
589831f5dcfSAlexander Motin 			buffer[0] = data;
590831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
591831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
592831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
593831f5dcfSAlexander Motin 			buffer += 4;
594831f5dcfSAlexander Motin 			left -= 4;
595831f5dcfSAlexander Motin 		}
596831f5dcfSAlexander Motin 	} else {
597d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
598831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
599831f5dcfSAlexander Motin 		left &= 3;
600831f5dcfSAlexander Motin 	}
601831f5dcfSAlexander Motin 	/* Handle uneven size case. */
602831f5dcfSAlexander Motin 	if (left > 0) {
603831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
604831f5dcfSAlexander Motin 		while (left > 0) {
605831f5dcfSAlexander Motin 			*(buffer++) = data;
606831f5dcfSAlexander Motin 			data >>= 8;
607831f5dcfSAlexander Motin 			left--;
608831f5dcfSAlexander Motin 		}
609831f5dcfSAlexander Motin 	}
610831f5dcfSAlexander Motin }
611831f5dcfSAlexander Motin 
612831f5dcfSAlexander Motin static void
613831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
614831f5dcfSAlexander Motin {
615831f5dcfSAlexander Motin 	uint32_t data = 0;
616831f5dcfSAlexander Motin 	char *buffer;
617831f5dcfSAlexander Motin 	size_t left;
618831f5dcfSAlexander Motin 
619831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
620831f5dcfSAlexander Motin 	buffer += slot->offset;
621831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
6225d5ae066SIlya Bakulin #ifdef MMCCAM
6235d5ae066SIlya Bakulin 	if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) {
6245d5ae066SIlya Bakulin 		left = min(slot->curcmd->data->block_size,
6255d5ae066SIlya Bakulin 		    slot->curcmd->data->len - slot->offset);
6265d5ae066SIlya Bakulin 	} else
6275d5ae066SIlya Bakulin #endif
628831f5dcfSAlexander Motin 		left = min(512, slot->curcmd->data->len - slot->offset);
629831f5dcfSAlexander Motin 	slot->offset += left;
630831f5dcfSAlexander Motin 
631ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
632831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
633831f5dcfSAlexander Motin 		while (left > 3) {
634831f5dcfSAlexander Motin 			data = buffer[0] +
635831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
636831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
637831f5dcfSAlexander Motin 			    (buffer[3] << 24);
638831f5dcfSAlexander Motin 			left -= 4;
639831f5dcfSAlexander Motin 			buffer += 4;
640831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
641831f5dcfSAlexander Motin 		}
642831f5dcfSAlexander Motin 	} else {
643d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
644831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
645831f5dcfSAlexander Motin 		left &= 3;
646831f5dcfSAlexander Motin 	}
647831f5dcfSAlexander Motin 	/* Handle uneven size case. */
648831f5dcfSAlexander Motin 	if (left > 0) {
649831f5dcfSAlexander Motin 		while (left > 0) {
650831f5dcfSAlexander Motin 			data <<= 8;
651831f5dcfSAlexander Motin 			data += *(buffer++);
652831f5dcfSAlexander Motin 			left--;
653831f5dcfSAlexander Motin 		}
654831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
655831f5dcfSAlexander Motin 	}
656831f5dcfSAlexander Motin }
657831f5dcfSAlexander Motin 
658831f5dcfSAlexander Motin static void
659831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
660831f5dcfSAlexander Motin {
661831f5dcfSAlexander Motin 
662831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
663831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
664831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
665831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
666831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
667831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
668831f5dcfSAlexander Motin 				break;
669831f5dcfSAlexander Motin 		}
670831f5dcfSAlexander Motin 	} else {
671831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
672831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
673831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
674831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
675831f5dcfSAlexander Motin 				break;
676831f5dcfSAlexander Motin 		}
677831f5dcfSAlexander Motin 	}
678831f5dcfSAlexander Motin }
679831f5dcfSAlexander Motin 
680831f5dcfSAlexander Motin static void
6817e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused)
682831f5dcfSAlexander Motin {
683831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
68403d49ffcSJohn Baldwin #ifndef MMCCAM
6857e6ccea3SMarius Strobl 	device_t d;
68603d49ffcSJohn Baldwin #endif
687831f5dcfSAlexander Motin 
688831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
6896e37fb2bSIan Lepore 	if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
690a94a63f0SWarner Losh #ifdef MMCCAM
691a94a63f0SWarner Losh 		if (slot->card_present == 0) {
692a94a63f0SWarner Losh #else
693831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
694a94a63f0SWarner Losh #endif
695831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
696639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
697639f59f0SIan Lepore 				slot_printf(slot, "Card inserted\n");
698a94a63f0SWarner Losh #ifdef MMCCAM
699a94a63f0SWarner Losh 			slot->card_present = 1;
700c7a49948SEmmanuel Vadot 			mmccam_start_discovery(slot->sim);
701a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
702a94a63f0SWarner Losh #else
7035b56413dSWarner Losh 			d = slot->dev = device_add_child(slot->bus, "mmc", DEVICE_UNIT_ANY);
704831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
705aca38eabSMarius Strobl 			if (d) {
706aca38eabSMarius Strobl 				device_set_ivars(d, slot);
707aca38eabSMarius Strobl 				(void)device_probe_and_attach(d);
708aca38eabSMarius Strobl 			}
709a94a63f0SWarner Losh #endif
710831f5dcfSAlexander Motin 		} else
711831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
712831f5dcfSAlexander Motin 	} else {
713a94a63f0SWarner Losh #ifdef MMCCAM
714a94a63f0SWarner Losh 		if (slot->card_present == 1) {
715a94a63f0SWarner Losh #else
716831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
71703d49ffcSJohn Baldwin 			d = slot->dev;
718a94a63f0SWarner Losh #endif
719831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
720639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
721639f59f0SIan Lepore 				slot_printf(slot, "Card removed\n");
722831f5dcfSAlexander Motin 			slot->dev = NULL;
723a94a63f0SWarner Losh #ifdef MMCCAM
724a94a63f0SWarner Losh 			slot->card_present = 0;
725c7a49948SEmmanuel Vadot 			mmccam_start_discovery(slot->sim);
726a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
727a94a63f0SWarner Losh #else
728aca38eabSMarius Strobl 			slot->intmask &= ~sdhci_tuning_intmask(slot);
729cc22204bSMarius Strobl 			WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
730aca38eabSMarius Strobl 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
731aca38eabSMarius Strobl 			slot->opt &= ~SDHCI_TUNING_ENABLED;
732831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
733aca38eabSMarius Strobl 			callout_drain(&slot->retune_callout);
734d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
735a94a63f0SWarner Losh #endif
736831f5dcfSAlexander Motin 		} else
737831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
738831f5dcfSAlexander Motin 	}
739831f5dcfSAlexander Motin }
740831f5dcfSAlexander Motin 
741b8bf08b1SIan Lepore static void
742b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
743639f59f0SIan Lepore {
744639f59f0SIan Lepore 	bool was_present;
745639f59f0SIan Lepore 
746639f59f0SIan Lepore 	/*
747639f59f0SIan Lepore 	 * If there was no card and now there is one, schedule the task to
748639f59f0SIan Lepore 	 * create the child device after a short delay.  The delay is to
749639f59f0SIan Lepore 	 * debounce the card insert (sometimes the card detect pin stabilizes
750639f59f0SIan Lepore 	 * before the other pins have made good contact).
751639f59f0SIan Lepore 	 *
752639f59f0SIan Lepore 	 * If there was a card present and now it's gone, immediately schedule
753639f59f0SIan Lepore 	 * the task to delete the child device.  No debouncing -- gone is gone,
754639f59f0SIan Lepore 	 * because once power is removed, a full card re-init is needed, and
755639f59f0SIan Lepore 	 * that happens by deleting and recreating the child device.
756639f59f0SIan Lepore 	 */
757a94a63f0SWarner Losh #ifdef MMCCAM
758a94a63f0SWarner Losh 	was_present = slot->card_present;
759a94a63f0SWarner Losh #else
760639f59f0SIan Lepore 	was_present = slot->dev != NULL;
761a94a63f0SWarner Losh #endif
762639f59f0SIan Lepore 	if (!was_present && is_present) {
763639f59f0SIan Lepore 		taskqueue_enqueue_timeout(taskqueue_swi_giant,
764639f59f0SIan Lepore 		    &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
765639f59f0SIan Lepore 	} else if (was_present && !is_present) {
766639f59f0SIan Lepore 		taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
767639f59f0SIan Lepore 	}
768b8bf08b1SIan Lepore }
769b8bf08b1SIan Lepore 
770b8bf08b1SIan Lepore void
771b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
772b8bf08b1SIan Lepore {
773b8bf08b1SIan Lepore 
774b8bf08b1SIan Lepore 	SDHCI_LOCK(slot);
775b8bf08b1SIan Lepore 	sdhci_handle_card_present_locked(slot, is_present);
776639f59f0SIan Lepore 	SDHCI_UNLOCK(slot);
777639f59f0SIan Lepore }
778639f59f0SIan Lepore 
779639f59f0SIan Lepore static void
780639f59f0SIan Lepore sdhci_card_poll(void *arg)
781639f59f0SIan Lepore {
782639f59f0SIan Lepore 	struct sdhci_slot *slot = arg;
783639f59f0SIan Lepore 
784639f59f0SIan Lepore 	sdhci_handle_card_present(slot,
785639f59f0SIan Lepore 	    SDHCI_GET_CARD_PRESENT(slot->bus, slot));
786639f59f0SIan Lepore 	callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
787639f59f0SIan Lepore 	    sdhci_card_poll, slot);
788639f59f0SIan Lepore }
789639f59f0SIan Lepore 
790ab00a509SMarius Strobl static int
791016f9657SMarcin Wojtas sdhci_dma_alloc(struct sdhci_slot *slot)
792ab00a509SMarius Strobl {
793ab00a509SMarius Strobl 	int err;
794ab00a509SMarius Strobl 
795ab00a509SMarius Strobl 	if (!(slot->quirks & SDHCI_QUIRK_BROKEN_SDMA_BOUNDARY)) {
796cd853791SKonstantin Belousov 		if (maxphys <= 1024 * 4)
797ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_4K;
798cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 8)
799ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_8K;
800cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 16)
801ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_16K;
802cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 32)
803ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_32K;
804cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 64)
805ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_64K;
806cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 128)
807ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_128K;
808cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 256)
809ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_256K;
810ab00a509SMarius Strobl 		else
811ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_512K;
812ab00a509SMarius Strobl 	}
813ab00a509SMarius Strobl 	slot->sdma_bbufsz = SDHCI_SDMA_BNDRY_TO_BBUFSZ(slot->sdma_boundary);
814ab00a509SMarius Strobl 
815ab00a509SMarius Strobl 	/*
816ab00a509SMarius Strobl 	 * Allocate the DMA tag for an SDMA bounce buffer.
817ab00a509SMarius Strobl 	 * Note that the SDHCI specification doesn't state any alignment
818ab00a509SMarius Strobl 	 * constraint for the SDMA system address.  However, controllers
819ab00a509SMarius Strobl 	 * typically ignore the SDMA boundary bits in SDHCI_DMA_ADDRESS when
820ab00a509SMarius Strobl 	 * forming the actual address of data, requiring the SDMA buffer to
821ab00a509SMarius Strobl 	 * be aligned to the SDMA boundary.
822ab00a509SMarius Strobl 	 */
823ab00a509SMarius Strobl 	err = bus_dma_tag_create(bus_get_dma_tag(slot->bus), slot->sdma_bbufsz,
824016f9657SMarcin Wojtas 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
825ab00a509SMarius Strobl 	    slot->sdma_bbufsz, 1, slot->sdma_bbufsz, BUS_DMA_ALLOCNOW,
826ab00a509SMarius Strobl 	    NULL, NULL, &slot->dmatag);
827ab00a509SMarius Strobl 	if (err != 0) {
828ab00a509SMarius Strobl 		slot_printf(slot, "Can't create DMA tag for SDMA\n");
829ab00a509SMarius Strobl 		return (err);
830ab00a509SMarius Strobl 	}
831ab00a509SMarius Strobl 	/* Allocate DMA memory for the SDMA bounce buffer. */
832ab00a509SMarius Strobl 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
833ab00a509SMarius Strobl 	    BUS_DMA_NOWAIT, &slot->dmamap);
834ab00a509SMarius Strobl 	if (err != 0) {
835ab00a509SMarius Strobl 		slot_printf(slot, "Can't alloc DMA memory for SDMA\n");
836ab00a509SMarius Strobl 		bus_dma_tag_destroy(slot->dmatag);
837ab00a509SMarius Strobl 		return (err);
838ab00a509SMarius Strobl 	}
839ab00a509SMarius Strobl 	/* Map the memory of the SDMA bounce buffer. */
840ab00a509SMarius Strobl 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
841ab00a509SMarius Strobl 	    (void *)slot->dmamem, slot->sdma_bbufsz, sdhci_getaddr,
842ab00a509SMarius Strobl 	    &slot->paddr, 0);
843ab00a509SMarius Strobl 	if (err != 0 || slot->paddr == 0) {
844ab00a509SMarius Strobl 		slot_printf(slot, "Can't load DMA memory for SDMA\n");
845ab00a509SMarius Strobl 		bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
846ab00a509SMarius Strobl 		bus_dma_tag_destroy(slot->dmatag);
847ab00a509SMarius Strobl 		if (err)
848ab00a509SMarius Strobl 			return (err);
849ab00a509SMarius Strobl 		else
850ab00a509SMarius Strobl 			return (EFAULT);
851ab00a509SMarius Strobl 	}
852ab00a509SMarius Strobl 
853ab00a509SMarius Strobl 	return (0);
854ab00a509SMarius Strobl }
855ab00a509SMarius Strobl 
856ab00a509SMarius Strobl static void
857ab00a509SMarius Strobl sdhci_dma_free(struct sdhci_slot *slot)
858ab00a509SMarius Strobl {
859ab00a509SMarius Strobl 
860ab00a509SMarius Strobl 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
861ab00a509SMarius Strobl 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
862ab00a509SMarius Strobl 	bus_dma_tag_destroy(slot->dmatag);
863ab00a509SMarius Strobl }
864ab00a509SMarius Strobl 
865d6b3aaf8SOleksandr Tymoshenko int
866d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
867831f5dcfSAlexander Motin {
868aca38eabSMarius Strobl 	kobjop_desc_t kobj_desc;
869aca38eabSMarius Strobl 	kobj_method_t *kobj_method;
8700f34084fSMarius Strobl 	uint32_t caps, caps2, freq, host_caps;
871d6b3aaf8SOleksandr Tymoshenko 	int err;
872d00c1f7fSBartlomiej Grzesik 	char node_name[8];
873d00c1f7fSBartlomiej Grzesik 	struct sysctl_oid *node_oid;
874831f5dcfSAlexander Motin 
875831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
876a94a63f0SWarner Losh 
877d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
878d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
879d6b3aaf8SOleksandr Tymoshenko 
880d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
881d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
8820f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) {
8838f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
8840f34084fSMarius Strobl 		caps2 = slot->caps2;
8850f34084fSMarius Strobl 	} else {
886831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
8870f34084fSMarius Strobl 		if (slot->version >= SDHCI_SPEC_300)
8880f34084fSMarius Strobl 			caps2 = RD4(slot, SDHCI_CAPABILITIES2);
8890f34084fSMarius Strobl 		else
8900f34084fSMarius Strobl 			caps2 = 0;
8910f34084fSMarius Strobl 	}
8927fcf4780SMarius Strobl 	if (slot->version >= SDHCI_SPEC_300) {
8937fcf4780SMarius Strobl 		if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE &&
8947fcf4780SMarius Strobl 		    (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) {
895ab00a509SMarius Strobl 			slot_printf(slot,
8967fcf4780SMarius Strobl 			    "Driver doesn't support shared bus slots\n");
8977fcf4780SMarius Strobl 			SDHCI_LOCK_DESTROY(slot);
8987fcf4780SMarius Strobl 			return (ENXIO);
8997fcf4780SMarius Strobl 		} else if ((caps & SDHCI_SLOTTYPE_MASK) ==
9007fcf4780SMarius Strobl 		    SDHCI_SLOTTYPE_EMBEDDED) {
9017fcf4780SMarius Strobl 			slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE;
9027fcf4780SMarius Strobl 		}
9037fcf4780SMarius Strobl 	}
904831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
90533aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
90687a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
90787a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
90833aad34dSOleksandr Tymoshenko 	else
90987a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
91087a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
91187a6a871SIan Lepore 	if (freq != 0)
91287a6a871SIan Lepore 		slot->max_clk = freq * 1000000;
91387a6a871SIan Lepore 	/*
91487a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
91587a6a871SIan Lepore 	 * hasn't already set max_clk we're probably not going to work right
91687a6a871SIan Lepore 	 * with an assumption, so complain about it.
91787a6a871SIan Lepore 	 */
918831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
91987a6a871SIan Lepore 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
920ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't specify base clock "
9211bacf3beSMarius Strobl 		    "frequency, using %dMHz as default.\n",
9221bacf3beSMarius Strobl 		    SDHCI_DEFAULT_MAX_FREQ);
923831f5dcfSAlexander Motin 	}
924a2832f9fSMarius Strobl 	/* Calculate/set timeout clock frequency. */
9258f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
9268f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
927a2832f9fSMarius Strobl 	} else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) {
928a2832f9fSMarius Strobl 		slot->timeout_clk = 1000;
9298f3b7d56SOleksandr Tymoshenko 	} else {
9301bacf3beSMarius Strobl 		slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >>
9311bacf3beSMarius Strobl 		    SDHCI_TIMEOUT_CLK_SHIFT;
9328f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
9338f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
9348f3b7d56SOleksandr Tymoshenko 	}
93587a6a871SIan Lepore 	/*
93687a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
93787a6a871SIan Lepore 	 * hasn't already set timeout_clk we'll probably work okay using the
93887a6a871SIan Lepore 	 * max timeout, but still mention it.
93987a6a871SIan Lepore 	 */
940831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
941ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't specify timeout clock "
942ceb9e9f7SIan Lepore 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
943ceb9e9f7SIan Lepore 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
944831f5dcfSAlexander Motin 	}
945831f5dcfSAlexander Motin 
94657677a3aSOleksandr Tymoshenko 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
947831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
948831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
949831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
950831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
951831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
952831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
95349dfdf63SIan Lepore 	/*
95449dfdf63SIan Lepore 	 * 1.8V VDD is not supposed to be used for removable cards.  Hardware
95549dfdf63SIan Lepore 	 * prior to v3.0 had no way to indicate embedded slots, but did
95649dfdf63SIan Lepore 	 * sometimes support 1.8v for non-removable devices.
95749dfdf63SIan Lepore 	 */
95849dfdf63SIan Lepore 	if ((caps & SDHCI_CAN_VDD_180) && (slot->version < SDHCI_SPEC_300 ||
95949dfdf63SIan Lepore 	    (slot->opt & SDHCI_SLOT_EMBEDDED)))
960831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
961831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
962ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't report any "
963831f5dcfSAlexander Motin 		    "support voltages.\n");
964831f5dcfSAlexander Motin 	}
965aca38eabSMarius Strobl 
9665652be30SMarcin Wojtas 	host_caps = slot->host.caps;
9675652be30SMarcin Wojtas 	host_caps |= MMC_CAP_4_BIT_DATA;
9682d1731b8SIan Lepore 	if (caps & SDHCI_CAN_DO_8BITBUS)
9690f34084fSMarius Strobl 		host_caps |= MMC_CAP_8_BIT_DATA;
970831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
9710f34084fSMarius Strobl 		host_caps |= MMC_CAP_HSPEED;
97272dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC)
9730f34084fSMarius Strobl 		host_caps |= MMC_CAP_BOOT_NOACC;
97472dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY)
9750f34084fSMarius Strobl 		host_caps |= MMC_CAP_WAIT_WHILE_BUSY;
976aca38eabSMarius Strobl 
977aca38eabSMarius Strobl 	/* Determine supported UHS-I and eMMC modes. */
9780f34084fSMarius Strobl 	if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50))
9790f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
9800f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_SDR104) {
9810f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
9820f34084fSMarius Strobl 		if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200))
9830f34084fSMarius Strobl 			host_caps |= MMC_CAP_MMC_HS200;
9840f34084fSMarius Strobl 	} else if (caps2 & SDHCI_CAN_SDR50)
9850f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR50;
9860f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_DDR50 &&
9870f34084fSMarius Strobl 	    !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50))
9880f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_DDR50;
9890f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_DDR52)
9900f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_DDR52;
9910f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 &&
9920f34084fSMarius Strobl 	    caps2 & SDHCI_CAN_MMC_HS400)
9930f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
994835998c2SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 &&
995835998c2SMarius Strobl 	    caps2 & SDHCI_CAN_SDR104)
996835998c2SMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
997aca38eabSMarius Strobl 
998aca38eabSMarius Strobl 	/*
999aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes if the set_uhs_timing method is the
1000aca38eabSMarius Strobl 	 * default NULL implementation.
1001aca38eabSMarius Strobl 	 */
1002aca38eabSMarius Strobl 	kobj_desc = &sdhci_set_uhs_timing_desc;
1003aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
1004aca38eabSMarius Strobl 	    kobj_desc);
1005aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
1006aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
1007aca38eabSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
1008aca38eabSMarius Strobl 		    MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400);
1009aca38eabSMarius Strobl 
1010aca38eabSMarius Strobl #define	SDHCI_CAP_MODES_TUNING(caps2)					\
1011aca38eabSMarius Strobl     (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) |		\
1012aca38eabSMarius Strobl     MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 |	\
1013aca38eabSMarius Strobl     MMC_CAP_MMC_HS400)
1014aca38eabSMarius Strobl 
1015aca38eabSMarius Strobl 	/*
1016aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes that require (re-)tuning if either
1017aca38eabSMarius Strobl 	 * the tune or re-tune method is the default NULL implementation.
1018aca38eabSMarius Strobl 	 */
1019aca38eabSMarius Strobl 	kobj_desc = &mmcbr_tune_desc;
1020aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
1021aca38eabSMarius Strobl 	    kobj_desc);
1022aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
1023aca38eabSMarius Strobl 		goto no_tuning;
1024aca38eabSMarius Strobl 	kobj_desc = &mmcbr_retune_desc;
1025aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
1026aca38eabSMarius Strobl 	    kobj_desc);
1027aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt) {
1028aca38eabSMarius Strobl no_tuning:
1029aca38eabSMarius Strobl 		host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2));
1030aca38eabSMarius Strobl 	}
1031aca38eabSMarius Strobl 
1032aca38eabSMarius Strobl 	/* Allocate tuning structures and determine tuning parameters. */
1033aca38eabSMarius Strobl 	if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) {
1034aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_SUPPORTED;
1035aca38eabSMarius Strobl 		slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF,
1036aca38eabSMarius Strobl 		    M_WAITOK);
1037aca38eabSMarius Strobl 		slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF,
1038aca38eabSMarius Strobl 		    M_WAITOK);
1039aca38eabSMarius Strobl 		slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF,
1040aca38eabSMarius Strobl 		    M_WAITOK);
1041aca38eabSMarius Strobl 		if (caps2 & SDHCI_TUNE_SDR50)
1042aca38eabSMarius Strobl 			slot->opt |= SDHCI_SDR50_NEEDS_TUNING;
1043aca38eabSMarius Strobl 		slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >>
1044aca38eabSMarius Strobl 		    SDHCI_RETUNE_MODES_SHIFT;
1045aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_1) {
1046aca38eabSMarius Strobl 			slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >>
1047aca38eabSMarius Strobl 			    SDHCI_RETUNE_CNT_SHIFT;
1048aca38eabSMarius Strobl 			if (slot->retune_count > 0xb) {
1049ab00a509SMarius Strobl 				slot_printf(slot, "Unknown re-tuning count "
1050aca38eabSMarius Strobl 				    "%x, using 1 sec\n", slot->retune_count);
1051aca38eabSMarius Strobl 				slot->retune_count = 1;
1052aca38eabSMarius Strobl 			} else if (slot->retune_count != 0)
1053aca38eabSMarius Strobl 				slot->retune_count =
1054aca38eabSMarius Strobl 				    1 << (slot->retune_count - 1);
1055aca38eabSMarius Strobl 		}
1056aca38eabSMarius Strobl 	}
1057aca38eabSMarius Strobl 
1058aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING
1059aca38eabSMarius Strobl 
1060aca38eabSMarius Strobl 	/* Determine supported VCCQ signaling levels. */
10610f34084fSMarius Strobl 	host_caps |= MMC_CAP_SIGNALING_330;
10620f34084fSMarius Strobl 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
1063aca38eabSMarius Strobl 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
10640f34084fSMarius Strobl 	    MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 |
10650f34084fSMarius Strobl 	    MMC_CAP_MMC_HS400_180))
1066aca38eabSMarius Strobl 		host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180;
1067aca38eabSMarius Strobl 
1068aca38eabSMarius Strobl 	/*
1069aca38eabSMarius Strobl 	 * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the
1070aca38eabSMarius Strobl 	 * default NULL implementation.  Disable 1.2 V support if it's the
1071aca38eabSMarius Strobl 	 * generic SDHCI implementation.
1072aca38eabSMarius Strobl 	 */
1073aca38eabSMarius Strobl 	kobj_desc = &mmcbr_switch_vccq_desc;
1074aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
1075aca38eabSMarius Strobl 	    kobj_desc);
1076aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
1077aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180);
1078aca38eabSMarius Strobl 	else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq)
1079aca38eabSMarius Strobl 		host_caps &= ~MMC_CAP_SIGNALING_120;
1080aca38eabSMarius Strobl 
1081aca38eabSMarius Strobl 	/* Determine supported driver types (type B is always mandatory). */
1082f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_A)
10830f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_A;
1084f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_C)
10850f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_C;
1086f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_D)
10870f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_D;
10880f34084fSMarius Strobl 	slot->host.caps = host_caps;
10890f34084fSMarius Strobl 
1090831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
1091831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
1092831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
1093d6b3aaf8SOleksandr Tymoshenko 
1094d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
1095831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
1096d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
1097831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
1098a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE)
1099a2832f9fSMarius Strobl 		slot->opt |= SDHCI_NON_REMOVABLE;
1100831f5dcfSAlexander Motin 
1101c3a0f75aSOleksandr Tymoshenko 	/*
1102c3a0f75aSOleksandr Tymoshenko 	 * Use platform-provided transfer backend
1103c3a0f75aSOleksandr Tymoshenko 	 * with PIO as a fallback mechanism
1104c3a0f75aSOleksandr Tymoshenko 	 */
1105c3a0f75aSOleksandr Tymoshenko 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
1106c3a0f75aSOleksandr Tymoshenko 		slot->opt &= ~SDHCI_HAVE_DMA;
1107c3a0f75aSOleksandr Tymoshenko 
1108ab00a509SMarius Strobl 	if (slot->opt & SDHCI_HAVE_DMA) {
1109016f9657SMarcin Wojtas 		err = sdhci_dma_alloc(slot);
1110ab00a509SMarius Strobl 		if (err != 0) {
1111ab00a509SMarius Strobl 			if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1112ab00a509SMarius Strobl 				free(slot->tune_req, M_DEVBUF);
1113ab00a509SMarius Strobl 				free(slot->tune_cmd, M_DEVBUF);
1114ab00a509SMarius Strobl 				free(slot->tune_data, M_DEVBUF);
1115ab00a509SMarius Strobl 			}
1116ab00a509SMarius Strobl 			SDHCI_LOCK_DESTROY(slot);
1117ab00a509SMarius Strobl 			return (err);
1118ab00a509SMarius Strobl 		}
1119ab00a509SMarius Strobl 	}
1120ab00a509SMarius Strobl 
11215b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
1122d00c1f7fSBartlomiej Grzesik 		sdhci_dumpcaps(slot);
1123831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1124831f5dcfSAlexander Motin 	}
1125831f5dcfSAlexander Motin 
1126ba6fc1c7SLuiz Otavio O Souza 	slot->timeout = 10;
1127ba6fc1c7SLuiz Otavio O Souza 	SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
1128ba6fc1c7SLuiz Otavio O Souza 	    SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
11294d52f81dSIan Lepore 	    "timeout", CTLFLAG_RWTUN, &slot->timeout, 0,
1130ba6fc1c7SLuiz Otavio O Souza 	    "Maximum timeout for SDHCI transfers (in secs)");
1131831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
1132639f59f0SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
1133639f59f0SIan Lepore 		sdhci_card_task, slot);
1134639f59f0SIan Lepore 	callout_init(&slot->card_poll_callout, 1);
1135e64f01a9SIan Lepore 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
1136aca38eabSMarius Strobl 	callout_init_mtx(&slot->retune_callout, &slot->mtx, 0);
1137ba6fc1c7SLuiz Otavio O Souza 
1138639f59f0SIan Lepore 	if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
1139639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
1140639f59f0SIan Lepore 		callout_reset(&slot->card_poll_callout,
1141639f59f0SIan Lepore 		    SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
1142639f59f0SIan Lepore 	}
1143639f59f0SIan Lepore 
1144aca38eabSMarius Strobl 	sdhci_init(slot);
1145aca38eabSMarius Strobl 
1146d00c1f7fSBartlomiej Grzesik 	snprintf(node_name, sizeof(node_name), "slot%d", slot->num);
1147d00c1f7fSBartlomiej Grzesik 
1148d00c1f7fSBartlomiej Grzesik 	node_oid = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev),
1149d00c1f7fSBartlomiej Grzesik 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1150d00c1f7fSBartlomiej Grzesik 	    OID_AUTO, node_name, CTLFLAG_RW, 0, "slot specific node");
1151d00c1f7fSBartlomiej Grzesik 
1152d00c1f7fSBartlomiej Grzesik 	node_oid = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev),
1153d00c1f7fSBartlomiej Grzesik 	    SYSCTL_CHILDREN(node_oid), OID_AUTO, "debug", CTLFLAG_RW, 0,
1154d00c1f7fSBartlomiej Grzesik 	    "Debugging node");
1155d00c1f7fSBartlomiej Grzesik 
1156d00c1f7fSBartlomiej Grzesik 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid),
1157d00c1f7fSBartlomiej Grzesik 	    OID_AUTO, "dumpregs", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1158d00c1f7fSBartlomiej Grzesik 	    slot, 0, &sdhci_syctl_dumpregs,
1159d00c1f7fSBartlomiej Grzesik 	    "A", "Dump SDHCI registers");
1160d00c1f7fSBartlomiej Grzesik 
1161d00c1f7fSBartlomiej Grzesik 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid),
1162d00c1f7fSBartlomiej Grzesik 	    OID_AUTO, "dumpcaps", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1163d00c1f7fSBartlomiej Grzesik 	    slot, 0, &sdhci_syctl_dumpcaps,
1164d00c1f7fSBartlomiej Grzesik 	    "A", "Dump SDHCI capabilites");
1165d00c1f7fSBartlomiej Grzesik 
1166831f5dcfSAlexander Motin 	return (0);
1167831f5dcfSAlexander Motin }
1168831f5dcfSAlexander Motin 
1169d91f1a10SIlya Bakulin #ifndef MMCCAM
1170d6b3aaf8SOleksandr Tymoshenko void
1171d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
1172831f5dcfSAlexander Motin {
11737e6ccea3SMarius Strobl 
1174d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
1175d6b3aaf8SOleksandr Tymoshenko }
1176d91f1a10SIlya Bakulin #endif
1177831f5dcfSAlexander Motin 
1178d6b3aaf8SOleksandr Tymoshenko int
1179d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
1180d6b3aaf8SOleksandr Tymoshenko {
1181831f5dcfSAlexander Motin 	device_t d;
1182831f5dcfSAlexander Motin 
1183e64f01a9SIan Lepore 	callout_drain(&slot->timeout_callout);
1184639f59f0SIan Lepore 	callout_drain(&slot->card_poll_callout);
1185aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1186831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
1187639f59f0SIan Lepore 	taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
1188831f5dcfSAlexander Motin 
1189831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1190831f5dcfSAlexander Motin 	d = slot->dev;
1191831f5dcfSAlexander Motin 	slot->dev = NULL;
1192831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1193831f5dcfSAlexander Motin 	if (d != NULL)
1194d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
1195831f5dcfSAlexander Motin 
1196831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1197b8f94506SArtur Rojek 	SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL);
1198831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1199ab00a509SMarius Strobl 	if (slot->opt & SDHCI_HAVE_DMA)
1200ab00a509SMarius Strobl 		sdhci_dma_free(slot);
1201aca38eabSMarius Strobl 	if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1202aca38eabSMarius Strobl 		free(slot->tune_req, M_DEVBUF);
1203aca38eabSMarius Strobl 		free(slot->tune_cmd, M_DEVBUF);
1204aca38eabSMarius Strobl 		free(slot->tune_data, M_DEVBUF);
1205aca38eabSMarius Strobl 	}
1206d6b3aaf8SOleksandr Tymoshenko 
1207831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
1208d6b3aaf8SOleksandr Tymoshenko 
1209831f5dcfSAlexander Motin 	return (0);
1210831f5dcfSAlexander Motin }
1211831f5dcfSAlexander Motin 
1212d6b3aaf8SOleksandr Tymoshenko int
1213d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
121492bf0e27SAlexander Motin {
12157e6ccea3SMarius Strobl 
1216aca38eabSMarius Strobl 	/*
1217aca38eabSMarius Strobl 	 * We expect the MMC layer to issue initial tuning after resume.
1218aca38eabSMarius Strobl 	 * Otherwise, we'd need to indicate re-tuning including circuit reset
1219aca38eabSMarius Strobl 	 * being required at least for re-tuning modes 1 and 2 ourselves.
1220aca38eabSMarius Strobl 	 */
1221aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1222aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1223aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1224b8f94506SArtur Rojek 	SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL);
1225aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
122692bf0e27SAlexander Motin 
122792bf0e27SAlexander Motin 	return (0);
122892bf0e27SAlexander Motin }
122992bf0e27SAlexander Motin 
1230d6b3aaf8SOleksandr Tymoshenko int
1231d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
123292bf0e27SAlexander Motin {
12337e6ccea3SMarius Strobl 
1234aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1235d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
1236aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
123792bf0e27SAlexander Motin 
1238d6b3aaf8SOleksandr Tymoshenko 	return (0);
123992bf0e27SAlexander Motin }
124092bf0e27SAlexander Motin 
1241b8f94506SArtur Rojek void
1242b8f94506SArtur Rojek sdhci_generic_reset(device_t brdev __unused, struct sdhci_slot *slot,
1243b8f94506SArtur Rojek     uint8_t mask)
1244b8f94506SArtur Rojek {
1245b8f94506SArtur Rojek 	int timeout;
1246b8f94506SArtur Rojek 	uint32_t clock;
1247b8f94506SArtur Rojek 
1248b8f94506SArtur Rojek 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
1249b8f94506SArtur Rojek 		if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
1250b8f94506SArtur Rojek 			return;
1251b8f94506SArtur Rojek 	}
1252b8f94506SArtur Rojek 
1253b8f94506SArtur Rojek 	/* Some controllers need this kick or reset won't work. */
1254b8f94506SArtur Rojek 	if ((mask & SDHCI_RESET_ALL) == 0 &&
1255b8f94506SArtur Rojek 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
1256b8f94506SArtur Rojek 		/* This is to force an update */
1257b8f94506SArtur Rojek 		clock = slot->clock;
1258b8f94506SArtur Rojek 		slot->clock = 0;
1259b8f94506SArtur Rojek 		sdhci_set_clock(slot, clock);
1260b8f94506SArtur Rojek 	}
1261b8f94506SArtur Rojek 
1262b8f94506SArtur Rojek 	if (mask & SDHCI_RESET_ALL) {
1263b8f94506SArtur Rojek 		slot->clock = 0;
1264b8f94506SArtur Rojek 		slot->power = 0;
1265b8f94506SArtur Rojek 	}
1266b8f94506SArtur Rojek 
1267b8f94506SArtur Rojek 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
1268b8f94506SArtur Rojek 
1269b8f94506SArtur Rojek 	if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
1270b8f94506SArtur Rojek 		/*
1271b8f94506SArtur Rojek 		 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
1272b8f94506SArtur Rojek 		 * specification.  The reset bit has internal propagation delay,
1273b8f94506SArtur Rojek 		 * so a fast read after write returns 0 even if reset process is
1274b8f94506SArtur Rojek 		 * in progress.  The workaround is to poll for 1 before polling
1275b8f94506SArtur Rojek 		 * for 0.  In the worst case, if we miss seeing it asserted the
1276b8f94506SArtur Rojek 		 * time we spent waiting is enough to ensure the reset finishes.
1277b8f94506SArtur Rojek 		 */
1278b8f94506SArtur Rojek 		timeout = 10000;
1279b8f94506SArtur Rojek 		while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
1280b8f94506SArtur Rojek 			if (timeout <= 0)
1281b8f94506SArtur Rojek 				break;
1282b8f94506SArtur Rojek 			timeout--;
1283b8f94506SArtur Rojek 			DELAY(1);
1284b8f94506SArtur Rojek 		}
1285b8f94506SArtur Rojek 	}
1286b8f94506SArtur Rojek 
1287b8f94506SArtur Rojek 	/* Wait max 100 ms */
1288b8f94506SArtur Rojek 	timeout = 10000;
1289b8f94506SArtur Rojek 	/* Controller clears the bits when it's done */
1290b8f94506SArtur Rojek 	while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
1291b8f94506SArtur Rojek 		if (timeout <= 0) {
1292b8f94506SArtur Rojek 			slot_printf(slot, "Reset 0x%x never completed.\n",
1293b8f94506SArtur Rojek 			    mask);
1294b8f94506SArtur Rojek 			sdhci_dumpregs(slot);
1295b8f94506SArtur Rojek 			return;
1296b8f94506SArtur Rojek 		}
1297b8f94506SArtur Rojek 		timeout--;
1298b8f94506SArtur Rojek 		DELAY(10);
1299b8f94506SArtur Rojek 	}
1300b8f94506SArtur Rojek }
1301b8f94506SArtur Rojek 
130257677a3aSOleksandr Tymoshenko uint32_t
1303b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot)
130457677a3aSOleksandr Tymoshenko {
13057e6ccea3SMarius Strobl 
130657677a3aSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
130757677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
130857677a3aSOleksandr Tymoshenko 	else
130957677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
131057677a3aSOleksandr Tymoshenko }
131157677a3aSOleksandr Tymoshenko 
13126e37fb2bSIan Lepore bool
1313b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot)
13146e37fb2bSIan Lepore {
13156e37fb2bSIan Lepore 
1316639f59f0SIan Lepore 	if (slot->opt & SDHCI_NON_REMOVABLE)
1317639f59f0SIan Lepore 		return true;
1318639f59f0SIan Lepore 
13196e37fb2bSIan Lepore 	return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
13206e37fb2bSIan Lepore }
13216e37fb2bSIan Lepore 
13220f34084fSMarius Strobl void
13230f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot)
13240f34084fSMarius Strobl {
1325ab00a509SMarius Strobl 	const struct mmc_ios *ios;
13260f34084fSMarius Strobl 	uint16_t hostctrl2;
13270f34084fSMarius Strobl 
13280f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
13290f34084fSMarius Strobl 		return;
13300f34084fSMarius Strobl 
1331aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
13320f34084fSMarius Strobl 	ios = &slot->host.ios;
13330f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
13340f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
13350f34084fSMarius Strobl 	hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK;
1336aca38eabSMarius Strobl 	if (ios->clock > SD_SDR50_MAX) {
13370f34084fSMarius Strobl 		if (ios->timing == bus_timing_mmc_hs400 ||
13380f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_hs400es)
13390f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_MMC_HS400;
1340aca38eabSMarius Strobl 		else
13410f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR104;
1342aca38eabSMarius Strobl 	}
13430f34084fSMarius Strobl 	else if (ios->clock > SD_SDR25_MAX)
13440f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR50;
13450f34084fSMarius Strobl 	else if (ios->clock > SD_SDR12_MAX) {
13460f34084fSMarius Strobl 		if (ios->timing == bus_timing_uhs_ddr50 ||
13470f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_ddr52)
13480f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_DDR50;
13490f34084fSMarius Strobl 		else
13500f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR25;
13510f34084fSMarius Strobl 	} else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY)
13520f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR12;
13530f34084fSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
13540f34084fSMarius Strobl 	sdhci_set_clock(slot, ios->clock);
13550f34084fSMarius Strobl }
13560f34084fSMarius Strobl 
1357d6b3aaf8SOleksandr Tymoshenko int
1358d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
1359831f5dcfSAlexander Motin {
1360831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1361831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
1362831f5dcfSAlexander Motin 
1363831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1364831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
1365831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
1366831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
1367831f5dcfSAlexander Motin 		sdhci_init(slot);
1368831f5dcfSAlexander Motin 	}
1369831f5dcfSAlexander Motin 	/* Configure the bus. */
1370831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
1371831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
13722d1731b8SIan Lepore 	if (ios->bus_width == bus_width_8) {
13732d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
1374831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
13752d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_4) {
13762d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
13772d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
13782d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_1) {
13792d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
13802d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
13812d1731b8SIan Lepore 	} else {
13822d1731b8SIan Lepore 		panic("Invalid bus width: %d", ios->bus_width);
13832d1731b8SIan Lepore 	}
13840f34084fSMarius Strobl 	if (ios->clock > SD_SDR12_MAX &&
1385bba987dcSIan Lepore 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
1386831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
1387831f5dcfSAlexander Motin 	else
1388831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
1389831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
13900f34084fSMarius Strobl 	SDHCI_SET_UHS_TIMING(brdev, slot);
1391831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
1392d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
1393b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot,
1394b8f94506SArtur Rojek 		    SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1395831f5dcfSAlexander Motin 
1396831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1397831f5dcfSAlexander Motin 	return (0);
1398831f5dcfSAlexander Motin }
1399831f5dcfSAlexander Motin 
14000f34084fSMarius Strobl int
14010f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev)
14020f34084fSMarius Strobl {
14030f34084fSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
14040f34084fSMarius Strobl 	enum mmc_vccq vccq;
14050f34084fSMarius Strobl 	int err;
14060f34084fSMarius Strobl 	uint16_t hostctrl2;
14070f34084fSMarius Strobl 
14080f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
14090f34084fSMarius Strobl 		return (0);
14100f34084fSMarius Strobl 
14110f34084fSMarius Strobl 	err = 0;
14120f34084fSMarius Strobl 	vccq = slot->host.ios.vccq;
14130f34084fSMarius Strobl 	SDHCI_LOCK(slot);
14140f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
14150f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
14160f34084fSMarius Strobl 	switch (vccq) {
14170f34084fSMarius Strobl 	case vccq_330:
14180f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
14190f34084fSMarius Strobl 			goto done;
14200f34084fSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE;
14210f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
14220f34084fSMarius Strobl 		DELAY(5000);
14230f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
14240f34084fSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
14250f34084fSMarius Strobl 			goto done;
14260f34084fSMarius Strobl 		err = EAGAIN;
14270f34084fSMarius Strobl 		break;
14280f34084fSMarius Strobl 	case vccq_180:
14290f34084fSMarius Strobl 		if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) {
14300f34084fSMarius Strobl 			err = EINVAL;
14310f34084fSMarius Strobl 			goto done;
14320f34084fSMarius Strobl 		}
14330f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
14340f34084fSMarius Strobl 			goto done;
14350f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_S18_ENABLE;
14360f34084fSMarius Strobl 		WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
14370f34084fSMarius Strobl 		DELAY(5000);
14380f34084fSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
14390f34084fSMarius Strobl 		if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
14400f34084fSMarius Strobl 			goto done;
14410f34084fSMarius Strobl 		err = EAGAIN;
14420f34084fSMarius Strobl 		break;
14430f34084fSMarius Strobl 	default:
14440f34084fSMarius Strobl 		slot_printf(slot,
14450f34084fSMarius Strobl 		    "Attempt to set unsupported signaling voltage\n");
14460f34084fSMarius Strobl 		err = EINVAL;
14470f34084fSMarius Strobl 		break;
14480f34084fSMarius Strobl 	}
14490f34084fSMarius Strobl done:
14500f34084fSMarius Strobl 	sdhci_set_clock(slot, slot->host.ios.clock);
14510f34084fSMarius Strobl 	SDHCI_UNLOCK(slot);
14520f34084fSMarius Strobl 	return (err);
14530f34084fSMarius Strobl }
14540f34084fSMarius Strobl 
1455aca38eabSMarius Strobl int
1456aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400)
1457aca38eabSMarius Strobl {
1458aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1459ab00a509SMarius Strobl 	const struct mmc_ios *ios = &slot->host.ios;
1460aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1461aca38eabSMarius Strobl 	struct mmc_data *tune_data;
1462aca38eabSMarius Strobl 	uint32_t opcode;
1463aca38eabSMarius Strobl 	int err;
1464aca38eabSMarius Strobl 
1465aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_SUPPORTED))
1466aca38eabSMarius Strobl 		return (0);
1467aca38eabSMarius Strobl 
1468aca38eabSMarius Strobl 	slot->retune_ticks = slot->retune_count * hz;
1469aca38eabSMarius Strobl 	opcode = MMC_SEND_TUNING_BLOCK;
1470aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1471aca38eabSMarius Strobl 	switch (ios->timing) {
1472aca38eabSMarius Strobl 	case bus_timing_mmc_hs400:
1473aca38eabSMarius Strobl 		slot_printf(slot, "HS400 must be tuned in HS200 mode\n");
1474aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1475aca38eabSMarius Strobl 		return (EINVAL);
1476aca38eabSMarius Strobl 	case bus_timing_mmc_hs200:
1477aca38eabSMarius Strobl 		/*
1478aca38eabSMarius Strobl 		 * In HS400 mode, controllers use the data strobe line to
1479aca38eabSMarius Strobl 		 * latch data from the devices so periodic re-tuning isn't
1480aca38eabSMarius Strobl 		 * expected to be required.
1481aca38eabSMarius Strobl 		 */
1482aca38eabSMarius Strobl 		if (hs400)
1483aca38eabSMarius Strobl 			slot->retune_ticks = 0;
1484aca38eabSMarius Strobl 		opcode = MMC_SEND_TUNING_BLOCK_HS200;
1485aca38eabSMarius Strobl 		break;
1486aca38eabSMarius Strobl 	case bus_timing_uhs_ddr50:
1487aca38eabSMarius Strobl 	case bus_timing_uhs_sdr104:
1488aca38eabSMarius Strobl 		break;
1489aca38eabSMarius Strobl 	case bus_timing_uhs_sdr50:
1490aca38eabSMarius Strobl 		if (slot->opt & SDHCI_SDR50_NEEDS_TUNING)
1491aca38eabSMarius Strobl 			break;
1492aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1493aca38eabSMarius Strobl 		return (0);
1494bd15d31cSMarius Strobl 	default:
1495bd15d31cSMarius Strobl 		slot_printf(slot, "Tuning requested but not required.\n");
1496bd15d31cSMarius Strobl 		SDHCI_UNLOCK(slot);
1497bd15d31cSMarius Strobl 		return (EINVAL);
1498aca38eabSMarius Strobl 	}
1499aca38eabSMarius Strobl 
1500aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1501aca38eabSMarius Strobl 	memset(tune_cmd, 0, sizeof(*tune_cmd));
1502aca38eabSMarius Strobl 	tune_cmd->opcode = opcode;
1503aca38eabSMarius Strobl 	tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1504aca38eabSMarius Strobl 	tune_data = tune_cmd->data = slot->tune_data;
1505aca38eabSMarius Strobl 	memset(tune_data, 0, sizeof(*tune_data));
1506aca38eabSMarius Strobl 	tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
1507aca38eabSMarius Strobl 	    ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 :
1508aca38eabSMarius Strobl 	    MMC_TUNING_LEN;
1509aca38eabSMarius Strobl 	tune_data->flags = MMC_DATA_READ;
1510aca38eabSMarius Strobl 	tune_data->mrq = tune_cmd->mrq = slot->tune_req;
1511aca38eabSMarius Strobl 
1512aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1513aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, true);
1514aca38eabSMarius Strobl 	if (err == 0) {
1515aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_ENABLED;
1516aca38eabSMarius Strobl 		slot->intmask |= sdhci_tuning_intmask(slot);
1517cc22204bSMarius Strobl 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
1518aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1519aca38eabSMarius Strobl 		if (slot->retune_ticks) {
1520aca38eabSMarius Strobl 			callout_reset(&slot->retune_callout, slot->retune_ticks,
1521aca38eabSMarius Strobl 			    sdhci_retune, slot);
1522aca38eabSMarius Strobl 		}
1523aca38eabSMarius Strobl 	}
1524aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1525aca38eabSMarius Strobl 	return (err);
1526aca38eabSMarius Strobl }
1527aca38eabSMarius Strobl 
1528aca38eabSMarius Strobl int
1529aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset)
1530aca38eabSMarius Strobl {
1531aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1532aca38eabSMarius Strobl 	int err;
1533aca38eabSMarius Strobl 
1534aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_ENABLED))
1535aca38eabSMarius Strobl 		return (0);
1536aca38eabSMarius Strobl 
1537aca38eabSMarius Strobl 	/* HS400 must be tuned in HS200 mode. */
1538aca38eabSMarius Strobl 	if (slot->host.ios.timing == bus_timing_mmc_hs400)
1539aca38eabSMarius Strobl 		return (EINVAL);
1540aca38eabSMarius Strobl 
1541aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1542aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, reset);
1543aca38eabSMarius Strobl 	/*
1544aca38eabSMarius Strobl 	 * There are two ways sdhci_exec_tuning() can fail:
1545aca38eabSMarius Strobl 	 * EBUSY should not actually happen when requests are only issued
1546aca38eabSMarius Strobl 	 *	 with the host properly acquired, and
1547aca38eabSMarius Strobl 	 * EIO   re-tuning failed (but it did work initially).
1548aca38eabSMarius Strobl 	 *
1549aca38eabSMarius Strobl 	 * In both cases, we should retry at later point if periodic re-tuning
1550aca38eabSMarius Strobl 	 * is enabled.  Note that due to slot->retune_req not being cleared in
1551aca38eabSMarius Strobl 	 * these failure cases, the MMC layer should trigger another attempt at
1552aca38eabSMarius Strobl 	 * re-tuning with the next request anyway, though.
1553aca38eabSMarius Strobl 	 */
1554aca38eabSMarius Strobl 	if (slot->retune_ticks) {
1555aca38eabSMarius Strobl 		callout_reset(&slot->retune_callout, slot->retune_ticks,
1556aca38eabSMarius Strobl 		    sdhci_retune, slot);
1557aca38eabSMarius Strobl 	}
1558aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1559aca38eabSMarius Strobl 	return (err);
1560aca38eabSMarius Strobl }
1561aca38eabSMarius Strobl 
1562aca38eabSMarius Strobl static int
1563aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset)
1564aca38eabSMarius Strobl {
1565aca38eabSMarius Strobl 	struct mmc_request *tune_req;
1566aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1567aca38eabSMarius Strobl 	int i;
1568aca38eabSMarius Strobl 	uint32_t intmask;
1569aca38eabSMarius Strobl 	uint16_t hostctrl2;
1570aca38eabSMarius Strobl 	u_char opt;
1571aca38eabSMarius Strobl 
1572aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
1573aca38eabSMarius Strobl 	if (slot->req != NULL)
1574aca38eabSMarius Strobl 		return (EBUSY);
1575aca38eabSMarius Strobl 
1576aca38eabSMarius Strobl 	/* Tuning doesn't work with DMA enabled. */
1577aca38eabSMarius Strobl 	opt = slot->opt;
1578aca38eabSMarius Strobl 	slot->opt = opt & ~SDHCI_HAVE_DMA;
1579aca38eabSMarius Strobl 
1580aca38eabSMarius Strobl 	/*
1581aca38eabSMarius Strobl 	 * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only
1582aca38eabSMarius Strobl 	 * kind of interrupt we receive in response to a tuning request.
1583aca38eabSMarius Strobl 	 */
1584aca38eabSMarius Strobl 	intmask = slot->intmask;
1585aca38eabSMarius Strobl 	slot->intmask = SDHCI_INT_DATA_AVAIL;
1586cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL);
1587aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL);
1588aca38eabSMarius Strobl 
1589aca38eabSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1590aca38eabSMarius Strobl 	if (reset)
1591aca38eabSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK;
1592aca38eabSMarius Strobl 	else
1593aca38eabSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK;
1594aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING);
1595aca38eabSMarius Strobl 
1596aca38eabSMarius Strobl 	tune_req = slot->tune_req;
1597aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1598aca38eabSMarius Strobl 	for (i = 0; i < MMC_TUNING_MAX; i++) {
1599aca38eabSMarius Strobl 		memset(tune_req, 0, sizeof(*tune_req));
1600aca38eabSMarius Strobl 		tune_req->cmd = tune_cmd;
1601aca38eabSMarius Strobl 		tune_req->done = sdhci_req_wakeup;
1602aca38eabSMarius Strobl 		tune_req->done_data = slot;
1603aca38eabSMarius Strobl 		slot->req = tune_req;
1604aca38eabSMarius Strobl 		slot->flags = 0;
1605aca38eabSMarius Strobl 		sdhci_start(slot);
1606aca38eabSMarius Strobl 		while (!(tune_req->flags & MMC_REQ_DONE))
1607aca38eabSMarius Strobl 			msleep(tune_req, &slot->mtx, 0, "sdhciet", 0);
1608aca38eabSMarius Strobl 		if (!(tune_req->flags & MMC_TUNE_DONE))
1609aca38eabSMarius Strobl 			break;
1610aca38eabSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1611aca38eabSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING))
1612aca38eabSMarius Strobl 			break;
1613aca38eabSMarius Strobl 		if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK)
1614aca38eabSMarius Strobl 			DELAY(1000);
1615aca38eabSMarius Strobl 	}
1616aca38eabSMarius Strobl 
161778f8baa8SMarius Strobl 	/*
161878f8baa8SMarius Strobl 	 * Restore DMA usage and interrupts.
161978f8baa8SMarius Strobl 	 * Note that the interrupt aggregation code might have cleared
162078f8baa8SMarius Strobl 	 * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask
162178f8baa8SMarius Strobl 	 * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE
162278f8baa8SMarius Strobl 	 * doesn't lose these.
162378f8baa8SMarius Strobl 	 */
1624aca38eabSMarius Strobl 	slot->opt = opt;
1625aca38eabSMarius Strobl 	slot->intmask = intmask;
162678f8baa8SMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END |
162778f8baa8SMarius Strobl 	    SDHCI_INT_RESPONSE);
1628aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, intmask);
1629aca38eabSMarius Strobl 
1630aca38eabSMarius Strobl 	if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING |
1631aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) {
1632aca38eabSMarius Strobl 		slot->retune_req = 0;
1633aca38eabSMarius Strobl 		return (0);
1634aca38eabSMarius Strobl 	}
1635aca38eabSMarius Strobl 
1636aca38eabSMarius Strobl 	slot_printf(slot, "Tuning failed, using fixed sampling clock\n");
1637aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING |
1638aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK));
1639b8f94506SArtur Rojek 	SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1640aca38eabSMarius Strobl 	return (EIO);
1641aca38eabSMarius Strobl }
1642aca38eabSMarius Strobl 
1643aca38eabSMarius Strobl static void
1644aca38eabSMarius Strobl sdhci_retune(void *arg)
1645aca38eabSMarius Strobl {
1646aca38eabSMarius Strobl 	struct sdhci_slot *slot = arg;
1647aca38eabSMarius Strobl 
1648aca38eabSMarius Strobl 	slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
1649aca38eabSMarius Strobl }
1650aca38eabSMarius Strobl 
1651a94a63f0SWarner Losh #ifdef MMCCAM
1652a94a63f0SWarner Losh static void
1653a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot)
1654a94a63f0SWarner Losh {
1655a94a63f0SWarner Losh 	union ccb *ccb;
165615c440e1SWarner Losh 
1657aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
165815c440e1SWarner Losh 		slot_printf(slot, "%s\n", __func__);
1659a94a63f0SWarner Losh 	if (slot->ccb != NULL && slot->curcmd != NULL) {
1660a94a63f0SWarner Losh 		callout_stop(&slot->timeout_callout);
1661a94a63f0SWarner Losh 		ccb = slot->ccb;
1662a94a63f0SWarner Losh 		slot->ccb = NULL;
1663a94a63f0SWarner Losh 		slot->curcmd = NULL;
1664a94a63f0SWarner Losh 
1665a94a63f0SWarner Losh 		/* Tell CAM the request is finished */
1666a94a63f0SWarner Losh 		struct ccb_mmcio *mmcio;
1667a94a63f0SWarner Losh 		mmcio = &ccb->mmcio;
1668a94a63f0SWarner Losh 
1669a94a63f0SWarner Losh 		ccb->ccb_h.status =
1670a94a63f0SWarner Losh 		    (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR);
1671a94a63f0SWarner Losh 		xpt_done(ccb);
1672a94a63f0SWarner Losh 	}
1673a94a63f0SWarner Losh }
1674a94a63f0SWarner Losh #else
1675831f5dcfSAlexander Motin static void
1676e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot)
1677e64f01a9SIan Lepore {
1678e64f01a9SIan Lepore 	struct mmc_request *req;
1679e64f01a9SIan Lepore 
1680e64f01a9SIan Lepore 	if (slot->req != NULL && slot->curcmd != NULL) {
1681e64f01a9SIan Lepore 		callout_stop(&slot->timeout_callout);
1682e64f01a9SIan Lepore 		req = slot->req;
1683e64f01a9SIan Lepore 		slot->req = NULL;
1684e64f01a9SIan Lepore 		slot->curcmd = NULL;
1685e64f01a9SIan Lepore 		req->done(req);
1686e64f01a9SIan Lepore 	}
1687e64f01a9SIan Lepore }
1688a94a63f0SWarner Losh #endif
1689e64f01a9SIan Lepore 
1690e64f01a9SIan Lepore static void
1691aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req)
1692aca38eabSMarius Strobl {
1693aca38eabSMarius Strobl 
1694aca38eabSMarius Strobl 	req->flags |= MMC_REQ_DONE;
1695aca38eabSMarius Strobl 	wakeup(req);
1696aca38eabSMarius Strobl }
1697aca38eabSMarius Strobl 
1698aca38eabSMarius Strobl static void
1699e64f01a9SIan Lepore sdhci_timeout(void *arg)
1700e64f01a9SIan Lepore {
1701e64f01a9SIan Lepore 	struct sdhci_slot *slot = arg;
1702e64f01a9SIan Lepore 
1703e64f01a9SIan Lepore 	if (slot->curcmd != NULL) {
17047e586643SIan Lepore 		slot_printf(slot, "Controller timeout\n");
17057e586643SIan Lepore 		sdhci_dumpregs(slot);
1706b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot,
1707b8f94506SArtur Rojek 		    SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1708e64f01a9SIan Lepore 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1709e64f01a9SIan Lepore 		sdhci_req_done(slot);
17107e586643SIan Lepore 	} else {
17117e586643SIan Lepore 		slot_printf(slot, "Spurious timeout - no active command\n");
1712e64f01a9SIan Lepore 	}
1713e64f01a9SIan Lepore }
1714e64f01a9SIan Lepore 
1715e64f01a9SIan Lepore static void
1716ab00a509SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data)
1717831f5dcfSAlexander Motin {
1718831f5dcfSAlexander Motin 	uint16_t mode;
1719831f5dcfSAlexander Motin 
1720831f5dcfSAlexander Motin 	if (data == NULL)
1721831f5dcfSAlexander Motin 		return;
1722831f5dcfSAlexander Motin 
1723831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
17245d5ae066SIlya Bakulin 	if (data->len > 512 || data->block_count > 1) {
1725831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
17265d5ae066SIlya Bakulin 		if (data->block_count == 0 && __predict_true(
17276dea80e6SMarius Strobl #ifdef MMCCAM
17286dea80e6SMarius Strobl 		    slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION &&
17296dea80e6SMarius Strobl #else
17300519c933SMarius Strobl 		    slot->req->stop != NULL &&
17316dea80e6SMarius Strobl #endif
17326dea80e6SMarius Strobl 		    !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)))
17336dea80e6SMarius Strobl 			mode |= SDHCI_TRNS_ACMD12;
17346dea80e6SMarius Strobl 	}
1735831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
1736831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
1737831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
1738831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
1739831f5dcfSAlexander Motin 
1740831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
1741831f5dcfSAlexander Motin }
1742831f5dcfSAlexander Motin 
1743831f5dcfSAlexander Motin static void
1744831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
1745831f5dcfSAlexander Motin {
1746831f5dcfSAlexander Motin 	int flags, timeout;
174790993663SIan Lepore 	uint32_t mask;
1748831f5dcfSAlexander Motin 
1749831f5dcfSAlexander Motin 	slot->curcmd = cmd;
1750831f5dcfSAlexander Motin 	slot->cmd_done = 0;
1751831f5dcfSAlexander Motin 
1752831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
1753831f5dcfSAlexander Motin 
1754831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
1755831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1756831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
1757831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1758e64f01a9SIan Lepore 		sdhci_req_done(slot);
1759831f5dcfSAlexander Motin 		return;
1760831f5dcfSAlexander Motin 	}
1761831f5dcfSAlexander Motin 
1762b440e965SMarius Strobl 	/*
1763b440e965SMarius Strobl 	 * Do not issue command if there is no card, clock or power.
1764b440e965SMarius Strobl 	 * Controller will not detect timeout without clock active.
1765b440e965SMarius Strobl 	 */
17666e37fb2bSIan Lepore 	if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
1767d8208d9eSAlexander Motin 	    slot->power == 0 ||
1768d8208d9eSAlexander Motin 	    slot->clock == 0) {
1769a94a63f0SWarner Losh 		slot_printf(slot,
1770440c645bSMitchell Horne 			    "Cannot issue a command (power=%d clock=%d)\n",
1771a94a63f0SWarner Losh 			    slot->power, slot->clock);
1772831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1773e64f01a9SIan Lepore 		sdhci_req_done(slot);
1774831f5dcfSAlexander Motin 		return;
1775831f5dcfSAlexander Motin 	}
1776831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
1777831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
1778831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
1779a94a63f0SWarner Losh 	if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY))
1780831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
1781aca38eabSMarius Strobl 	/*
1782aca38eabSMarius Strobl 	 * We shouldn't wait for DAT for stop commands or CMD19/CMD21.  Note
1783aca38eabSMarius Strobl 	 * that these latter are also special in that SDHCI_CMD_DATA should
1784aca38eabSMarius Strobl 	 * be set below but no actual data is ever read from the controller.
1785aca38eabSMarius Strobl 	*/
1786a94a63f0SWarner Losh #ifdef MMCCAM
1787aca38eabSMarius Strobl 	if (cmd == &slot->ccb->mmcio.stop ||
1788a94a63f0SWarner Losh #else
1789aca38eabSMarius Strobl 	if (cmd == slot->req->stop ||
1790a94a63f0SWarner Losh #endif
1791aca38eabSMarius Strobl 	    __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1792aca38eabSMarius Strobl 	    cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))
1793aca38eabSMarius Strobl 		mask &= ~SDHCI_DAT_INHIBIT;
17948775ab45SIan Lepore 	/*
17958775ab45SIan Lepore 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
17968775ab45SIan Lepore 	 *  here at all, but when writing a crash dump we may be bypassing the
17978775ab45SIan Lepore 	 *  host platform's interrupt handler, and in some cases that handler
17988775ab45SIan Lepore 	 *  may be working around hardware quirks such as not respecting r1b
17998775ab45SIan Lepore 	 *  busy indications.  In those cases, this wait-loop serves the purpose
18008775ab45SIan Lepore 	 *  of waiting for the prior command and data transfers to be done, and
18018775ab45SIan Lepore 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
18028775ab45SIan Lepore 	 *  (It's usually more like 20-30ms in the real world.)
18038775ab45SIan Lepore 	 */
18048775ab45SIan Lepore 	timeout = 250;
180590993663SIan Lepore 	while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
1806831f5dcfSAlexander Motin 		if (timeout == 0) {
1807831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
1808831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
1809831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
1810831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
1811e64f01a9SIan Lepore 			sdhci_req_done(slot);
1812831f5dcfSAlexander Motin 			return;
1813831f5dcfSAlexander Motin 		}
1814831f5dcfSAlexander Motin 		timeout--;
1815831f5dcfSAlexander Motin 		DELAY(1000);
1816831f5dcfSAlexander Motin 	}
1817831f5dcfSAlexander Motin 
1818831f5dcfSAlexander Motin 	/* Prepare command flags. */
1819831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
1820831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
1821831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
1822831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
1823831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
1824831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
1825831f5dcfSAlexander Motin 	else
1826831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
1827831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
1828831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
1829831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
1830831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
1831a94a63f0SWarner Losh 	if (cmd->data != NULL)
1832831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
1833831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
1834831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
1835831f5dcfSAlexander Motin 	/* Prepare data. */
1836831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
1837831f5dcfSAlexander Motin 	/*
1838831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
1839831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
1840831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
1841831f5dcfSAlexander Motin 	 */
1842831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
1843831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1844831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
1845831f5dcfSAlexander Motin 	}
1846831f5dcfSAlexander Motin 	/* Set command argument. */
1847831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
1848831f5dcfSAlexander Motin 	/* Set data transfer mode. */
1849831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
1850aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
18518adf4202SBjoern A. Zeeb 		slot_printf(slot, "Starting command opcode %#04x flags %#04x\n",
18528adf4202SBjoern A. Zeeb 		    cmd->opcode, flags);
18538adf4202SBjoern A. Zeeb 
1854831f5dcfSAlexander Motin 	/* Start command. */
1855d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
1856a6873fd1SIan Lepore 	/* Start timeout callout. */
1857ba6fc1c7SLuiz Otavio O Souza 	callout_reset(&slot->timeout_callout, slot->timeout * hz,
1858ba6fc1c7SLuiz Otavio O Souza 	    sdhci_timeout, slot);
1859831f5dcfSAlexander Motin }
1860831f5dcfSAlexander Motin 
1861831f5dcfSAlexander Motin static void
1862831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
1863831f5dcfSAlexander Motin {
1864831f5dcfSAlexander Motin 	int i;
18651bacf3beSMarius Strobl 	uint32_t val;
18661bacf3beSMarius Strobl 	uint8_t extra;
1867831f5dcfSAlexander Motin 
1868aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
18698adf4202SBjoern A. Zeeb 		slot_printf(slot, "%s: called, err %d flags %#04x\n",
1870a94a63f0SWarner Losh 		    __func__, slot->curcmd->error, slot->curcmd->flags);
1871831f5dcfSAlexander Motin 	slot->cmd_done = 1;
187272dec079SMarius Strobl 	/*
187372dec079SMarius Strobl 	 * Interrupt aggregation: Restore command interrupt.
1874831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
187572dec079SMarius Strobl 	 * happened first.
187672dec079SMarius Strobl 	 */
1877aca38eabSMarius Strobl 	if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK &&
1878aca38eabSMarius Strobl 	    slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200))
1879aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |=
1880aca38eabSMarius Strobl 		    SDHCI_INT_RESPONSE);
1881831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
1882831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1883aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
1884aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1885b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD);
1886b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA);
1887831f5dcfSAlexander Motin 		sdhci_start(slot);
1888831f5dcfSAlexander Motin 		return;
1889831f5dcfSAlexander Motin 	}
1890831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
1891831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1892831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
1893831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
18941bacf3beSMarius Strobl 			extra = 0;
1895831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
18961bacf3beSMarius Strobl 				val = RD4(slot, SDHCI_RESPONSE + i * 4);
18971bacf3beSMarius Strobl 				if (slot->quirks &
18981bacf3beSMarius Strobl 				    SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1899677ee494SIan Lepore 					slot->curcmd->resp[3 - i] = val;
1900677ee494SIan Lepore 				else {
1901677ee494SIan Lepore 					slot->curcmd->resp[3 - i] =
1902677ee494SIan Lepore 					    (val << 8) | extra;
1903831f5dcfSAlexander Motin 					extra = val >> 24;
1904831f5dcfSAlexander Motin 				}
1905677ee494SIan Lepore 			}
1906831f5dcfSAlexander Motin 		} else
1907831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1908831f5dcfSAlexander Motin 	}
1909aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
19108adf4202SBjoern A. Zeeb 		slot_printf(slot, "Resp: %#04x %#04x %#04x %#04x\n",
1911a94a63f0SWarner Losh 		    slot->curcmd->resp[0], slot->curcmd->resp[1],
1912a94a63f0SWarner Losh 		    slot->curcmd->resp[2], slot->curcmd->resp[3]);
1913a94a63f0SWarner Losh 
1914831f5dcfSAlexander Motin 	/* If data ready - finish. */
1915831f5dcfSAlexander Motin 	if (slot->data_done)
1916831f5dcfSAlexander Motin 		sdhci_start(slot);
1917831f5dcfSAlexander Motin }
1918831f5dcfSAlexander Motin 
1919831f5dcfSAlexander Motin static void
1920ab00a509SMarius Strobl sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data)
1921831f5dcfSAlexander Motin {
1922ab00a509SMarius Strobl 	uint32_t blkcnt, blksz, current_timeout, sdma_bbufsz, target_timeout;
1923831f5dcfSAlexander Motin 	uint8_t div;
1924831f5dcfSAlexander Motin 
1925831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1926831f5dcfSAlexander Motin 		slot->data_done = 1;
1927831f5dcfSAlexander Motin 		return;
1928831f5dcfSAlexander Motin 	}
1929831f5dcfSAlexander Motin 
1930831f5dcfSAlexander Motin 	slot->data_done = 0;
1931831f5dcfSAlexander Motin 
1932831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
1933831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
1934ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1935ceb9e9f7SIan Lepore 		div = 0xE;
1936ceb9e9f7SIan Lepore 	} else {
1937831f5dcfSAlexander Motin 		target_timeout = 1000000;
1938831f5dcfSAlexander Motin 		div = 0;
1939831f5dcfSAlexander Motin 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1940ceb9e9f7SIan Lepore 		while (current_timeout < target_timeout && div < 0xE) {
1941ceb9e9f7SIan Lepore 			++div;
1942831f5dcfSAlexander Motin 			current_timeout <<= 1;
1943831f5dcfSAlexander Motin 		}
1944831f5dcfSAlexander Motin 		/* Compensate for an off-by-one error in the CaFe chip.*/
1945ceb9e9f7SIan Lepore 		if (div < 0xE &&
1946ceb9e9f7SIan Lepore 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1947ceb9e9f7SIan Lepore 			++div;
1948831f5dcfSAlexander Motin 		}
1949ceb9e9f7SIan Lepore 	}
1950831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1951831f5dcfSAlexander Motin 
1952831f5dcfSAlexander Motin 	if (data == NULL)
1953831f5dcfSAlexander Motin 		return;
1954831f5dcfSAlexander Motin 
1955831f5dcfSAlexander Motin 	/* Use DMA if possible. */
1956831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
1957831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
1958ab00a509SMarius Strobl 	/* If data is small, broken DMA may return zeroes instead of data. */
1959d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1960831f5dcfSAlexander Motin 	    (data->len <= 512))
1961831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1962831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
1963d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1964831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
1965831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1966831f5dcfSAlexander Motin 	/* Load DMA buffer. */
1967831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
1968ab00a509SMarius Strobl 		sdma_bbufsz = slot->sdma_bbufsz;
1969831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
1970ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1971ecc2d997SRui Paulo 			    BUS_DMASYNC_PREREAD);
1972831f5dcfSAlexander Motin 		else {
1973ab00a509SMarius Strobl 			memcpy(slot->dmamem, data->data, ulmin(data->len,
1974ab00a509SMarius Strobl 			    sdma_bbufsz));
1975ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1976ecc2d997SRui Paulo 			    BUS_DMASYNC_PREWRITE);
1977831f5dcfSAlexander Motin 		}
1978831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1979ab00a509SMarius Strobl 		/*
1980ab00a509SMarius Strobl 		 * Interrupt aggregation: Mask border interrupt for the last
1981ab00a509SMarius Strobl 		 * bounce buffer and unmask otherwise.
1982ab00a509SMarius Strobl 		 */
1983ab00a509SMarius Strobl 		if (data->len == sdma_bbufsz)
1984831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1985831f5dcfSAlexander Motin 		else
1986831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
1987831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1988831f5dcfSAlexander Motin 	}
1989831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
1990831f5dcfSAlexander Motin 	slot->offset = 0;
19915d5ae066SIlya Bakulin #ifdef MMCCAM
19925d5ae066SIlya Bakulin 	if (data->flags & MMC_DATA_BLOCK_SIZE) {
19935d5ae066SIlya Bakulin 		/* Set block size and request border interrupts on the SDMA boundary. */
19945d5ae066SIlya Bakulin 		blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, data->block_size);
19955d5ae066SIlya Bakulin 		blkcnt = data->block_count;
19965d5ae066SIlya Bakulin 		if (__predict_false(sdhci_debug > 0))
19975d5ae066SIlya Bakulin 			slot_printf(slot, "SDIO Custom block params: blksz: "
19985d5ae066SIlya Bakulin 			    "%#10x, blk cnt: %#10x\n", blksz, blkcnt);
19995d5ae066SIlya Bakulin 	} else
20005d5ae066SIlya Bakulin #endif
20015d5ae066SIlya Bakulin 	{
2002ab00a509SMarius Strobl 		/* Set block size and request border interrupts on the SDMA boundary. */
2003ab00a509SMarius Strobl 		blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, ulmin(data->len, 512));
2004ab00a509SMarius Strobl 		blkcnt = howmany(data->len, 512);
20055d5ae066SIlya Bakulin 	}
20065d5ae066SIlya Bakulin 
20075d5ae066SIlya Bakulin 	WR2(slot, SDHCI_BLOCK_SIZE, blksz);
2008ab00a509SMarius Strobl 	WR2(slot, SDHCI_BLOCK_COUNT, blkcnt);
2009aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
2010ab00a509SMarius Strobl 		slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
2011ab00a509SMarius Strobl 		    blksz, blkcnt);
2012831f5dcfSAlexander Motin }
2013831f5dcfSAlexander Motin 
2014c3a0f75aSOleksandr Tymoshenko void
2015831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
2016831f5dcfSAlexander Motin {
2017831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
20187e6ccea3SMarius Strobl 	size_t left;
2019831f5dcfSAlexander Motin 
2020831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
2021ecc2d997SRui Paulo 	 * Auxiliary restore point for the case when data interrupt
2022831f5dcfSAlexander Motin 	 * happened first. */
2023831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
2024831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
2025831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
2026831f5dcfSAlexander Motin 	}
2027831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
2028915780d7SLuiz Otavio O Souza 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) &&
2029915780d7SLuiz Otavio O Souza 	    slot->curcmd->data != NULL) {
2030831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
20317e6ccea3SMarius Strobl 			left = data->len - slot->offset;
2032ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2033ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTREAD);
2034831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
2035ab00a509SMarius Strobl 			    ulmin(left, slot->sdma_bbufsz));
2036831f5dcfSAlexander Motin 		} else
2037ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2038ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTWRITE);
2039831f5dcfSAlexander Motin 	}
2040a98788edSIan Lepore 	slot->data_done = 1;
2041831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
2042831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
2043aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
2044aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
2045b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD);
2046b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA);
2047831f5dcfSAlexander Motin 		sdhci_start(slot);
2048831f5dcfSAlexander Motin 		return;
2049831f5dcfSAlexander Motin 	}
2050831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
2051831f5dcfSAlexander Motin 	if (slot->cmd_done)
2052831f5dcfSAlexander Motin 		sdhci_start(slot);
2053831f5dcfSAlexander Motin }
2054831f5dcfSAlexander Motin 
2055a94a63f0SWarner Losh #ifdef MMCCAM
2056a94a63f0SWarner Losh static void
2057a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot)
2058a94a63f0SWarner Losh {
2059a94a63f0SWarner Losh 	union ccb *ccb;
2060ab00a509SMarius Strobl 	struct ccb_mmcio *mmcio;
2061a94a63f0SWarner Losh 
2062a94a63f0SWarner Losh 	ccb = slot->ccb;
2063a94a63f0SWarner Losh 	if (ccb == NULL)
2064a94a63f0SWarner Losh 		return;
2065a94a63f0SWarner Losh 
2066a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
2067a94a63f0SWarner Losh 	if (!(slot->flags & CMD_STARTED)) {
2068a94a63f0SWarner Losh 		slot->flags |= CMD_STARTED;
2069a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->cmd);
2070a94a63f0SWarner Losh 		return;
2071a94a63f0SWarner Losh 	}
2072a94a63f0SWarner Losh 
2073a94a63f0SWarner Losh 	/*
2074a94a63f0SWarner Losh 	 * Old stack doesn't use this!
2075a94a63f0SWarner Losh 	 * Enabling this code causes significant performance degradation
2076a94a63f0SWarner Losh 	 * and IRQ storms on BBB, Wandboard behaves fine.
2077a94a63f0SWarner Losh 	 * Not using this code does no harm...
2078a94a63f0SWarner Losh 	if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) {
2079a94a63f0SWarner Losh 		slot->flags |= STOP_STARTED;
2080a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->stop);
2081a94a63f0SWarner Losh 		return;
2082a94a63f0SWarner Losh 	}
2083a94a63f0SWarner Losh 	*/
2084aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
2085a94a63f0SWarner Losh 		slot_printf(slot, "result: %d\n", mmcio->cmd.error);
2086a94a63f0SWarner Losh 	if (mmcio->cmd.error == 0 &&
2087a94a63f0SWarner Losh 	    (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
2088b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD);
2089b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA);
2090a94a63f0SWarner Losh 	}
2091a94a63f0SWarner Losh 
2092a94a63f0SWarner Losh 	sdhci_req_done(slot);
2093a94a63f0SWarner Losh }
2094a94a63f0SWarner Losh #else
2095831f5dcfSAlexander Motin static void
2096831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
2097831f5dcfSAlexander Motin {
2098ab00a509SMarius Strobl 	const struct mmc_request *req;
2099831f5dcfSAlexander Motin 
2100831f5dcfSAlexander Motin 	req = slot->req;
2101831f5dcfSAlexander Motin 	if (req == NULL)
2102831f5dcfSAlexander Motin 		return;
2103831f5dcfSAlexander Motin 
2104831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
2105831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
2106831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
2107831f5dcfSAlexander Motin 		return;
2108831f5dcfSAlexander Motin 	}
2109915780d7SLuiz Otavio O Souza 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) &&
2110915780d7SLuiz Otavio O Souza 	    !(slot->flags & STOP_STARTED) && req->stop) {
2111831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
2112831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
2113831f5dcfSAlexander Motin 		return;
2114831f5dcfSAlexander Motin 	}
2115aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
21165b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
21175b69a497SAlexander Motin 	if (!req->cmd->error &&
2118915780d7SLuiz Otavio O Souza 	    ((slot->curcmd == req->stop &&
2119915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) ||
2120915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2121b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD);
2122b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA);
2123831f5dcfSAlexander Motin 	}
2124831f5dcfSAlexander Motin 
2125e64f01a9SIan Lepore 	sdhci_req_done(slot);
2126831f5dcfSAlexander Motin }
2127a94a63f0SWarner Losh #endif
2128831f5dcfSAlexander Motin 
2129d6b3aaf8SOleksandr Tymoshenko int
2130b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev,
2131b440e965SMarius Strobl     struct mmc_request *req)
2132831f5dcfSAlexander Motin {
2133831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2134831f5dcfSAlexander Motin 
2135831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2136831f5dcfSAlexander Motin 	if (slot->req != NULL) {
2137831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
2138831f5dcfSAlexander Motin 		return (EBUSY);
2139831f5dcfSAlexander Motin 	}
2140aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
21411bacf3beSMarius Strobl 		slot_printf(slot,
21421bacf3beSMarius Strobl 		    "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
2143831f5dcfSAlexander Motin 		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
21445b69a497SAlexander Motin 		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
21455b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
21465b69a497SAlexander Motin 	}
2147831f5dcfSAlexander Motin 	slot->req = req;
2148831f5dcfSAlexander Motin 	slot->flags = 0;
2149831f5dcfSAlexander Motin 	sdhci_start(slot);
2150831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2151bea2dca2SAlexander Motin 	if (dumping) {
2152bea2dca2SAlexander Motin 		while (slot->req != NULL) {
2153d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
2154bea2dca2SAlexander Motin 			DELAY(10);
2155bea2dca2SAlexander Motin 		}
2156bea2dca2SAlexander Motin 	}
2157831f5dcfSAlexander Motin 	return (0);
2158831f5dcfSAlexander Motin }
2159831f5dcfSAlexander Motin 
2160d6b3aaf8SOleksandr Tymoshenko int
2161b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev)
2162831f5dcfSAlexander Motin {
2163831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2164831f5dcfSAlexander Motin 	uint32_t val;
2165831f5dcfSAlexander Motin 
2166831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2167831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
2168831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2169831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
2170831f5dcfSAlexander Motin }
2171831f5dcfSAlexander Motin 
2172d6b3aaf8SOleksandr Tymoshenko int
2173b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev)
2174831f5dcfSAlexander Motin {
2175831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2176831f5dcfSAlexander Motin 	int err = 0;
2177831f5dcfSAlexander Motin 
2178831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2179831f5dcfSAlexander Motin 	while (slot->bus_busy)
2180d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
2181831f5dcfSAlexander Motin 	slot->bus_busy++;
2182831f5dcfSAlexander Motin 	/* Activate led. */
2183831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
2184831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2185831f5dcfSAlexander Motin 	return (err);
2186831f5dcfSAlexander Motin }
2187831f5dcfSAlexander Motin 
2188d6b3aaf8SOleksandr Tymoshenko int
2189b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev)
2190831f5dcfSAlexander Motin {
2191831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2192831f5dcfSAlexander Motin 
2193831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2194831f5dcfSAlexander Motin 	/* Deactivate led. */
2195831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
2196831f5dcfSAlexander Motin 	slot->bus_busy--;
2197d493985aSAlexander Motin 	wakeup(slot);
219835547df5SScott Long 	SDHCI_UNLOCK(slot);
2199831f5dcfSAlexander Motin 	return (0);
2200831f5dcfSAlexander Motin }
2201831f5dcfSAlexander Motin 
2202831f5dcfSAlexander Motin static void
2203831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
2204831f5dcfSAlexander Motin {
2205831f5dcfSAlexander Motin 
2206831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2207831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
2208831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2209831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2210831f5dcfSAlexander Motin 		return;
2211831f5dcfSAlexander Motin 	}
2212831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
2213831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2214831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
2215831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2216831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
2217831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
2218831f5dcfSAlexander Motin 
2219831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
2220831f5dcfSAlexander Motin }
2221831f5dcfSAlexander Motin 
2222831f5dcfSAlexander Motin static void
2223831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
2224831f5dcfSAlexander Motin {
22251bacf3beSMarius Strobl 	struct mmc_data *data;
222615c440e1SWarner Losh 	size_t left;
2227ab00a509SMarius Strobl 	uint32_t sdma_bbufsz;
2228831f5dcfSAlexander Motin 
2229831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2230831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2231831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2232831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2233831f5dcfSAlexander Motin 		return;
2234831f5dcfSAlexander Motin 	}
2235831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2236831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
2237831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2238831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
2239831f5dcfSAlexander Motin 		    intmask);
2240831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2241831f5dcfSAlexander Motin 		return;
2242831f5dcfSAlexander Motin 	}
2243831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
2244831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2245acbaa69fSOleksandr Tymoshenko 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
2246831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2247831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2248831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
2249831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
2250831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2251831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
2252831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2253831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
2254831f5dcfSAlexander Motin 	}
2255831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
2256831f5dcfSAlexander Motin 		/* No need to continue after any error. */
2257a98788edSIan Lepore 		goto done;
2258831f5dcfSAlexander Motin 	}
2259831f5dcfSAlexander Motin 
2260aca38eabSMarius Strobl 	/* Handle tuning completion interrupt. */
2261aca38eabSMarius Strobl 	if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) &&
2262aca38eabSMarius Strobl 	    (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK ||
2263aca38eabSMarius Strobl 	    slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) {
2264aca38eabSMarius Strobl 		slot->req->flags |= MMC_TUNE_DONE;
2265aca38eabSMarius Strobl 		sdhci_finish_command(slot);
2266aca38eabSMarius Strobl 		sdhci_finish_data(slot);
2267aca38eabSMarius Strobl 		return;
2268aca38eabSMarius Strobl 	}
2269831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
2270c3a0f75aSOleksandr Tymoshenko 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
2271c3a0f75aSOleksandr Tymoshenko 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
2272c3a0f75aSOleksandr Tymoshenko 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
22731bacf3beSMarius Strobl 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot,
22741bacf3beSMarius Strobl 			    &intmask);
2275c3a0f75aSOleksandr Tymoshenko 			slot->flags |= PLATFORM_DATA_STARTED;
2276c3a0f75aSOleksandr Tymoshenko 		} else
2277831f5dcfSAlexander Motin 			sdhci_transfer_pio(slot);
2278c3a0f75aSOleksandr Tymoshenko 	}
2279831f5dcfSAlexander Motin 	/* Handle DMA border. */
2280831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
22811bacf3beSMarius Strobl 		data = slot->curcmd->data;
2282ab00a509SMarius Strobl 		sdma_bbufsz = slot->sdma_bbufsz;
2283831f5dcfSAlexander Motin 
2284831f5dcfSAlexander Motin 		/* Unload DMA buffer ... */
2285831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2286831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2287831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2288831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
2289831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
2290ab00a509SMarius Strobl 			    ulmin(left, sdma_bbufsz));
2291831f5dcfSAlexander Motin 		} else {
2292831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2293831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
2294831f5dcfSAlexander Motin 		}
2295831f5dcfSAlexander Motin 		/* ... and reload it again. */
2296ab00a509SMarius Strobl 		slot->offset += sdma_bbufsz;
2297831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2298831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2299831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2300831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREREAD);
2301831f5dcfSAlexander Motin 		} else {
2302831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
2303ab00a509SMarius Strobl 			    ulmin(left, sdma_bbufsz));
2304831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2305831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
2306831f5dcfSAlexander Motin 		}
2307ab00a509SMarius Strobl 		/*
2308ab00a509SMarius Strobl 		 * Interrupt aggregation: Mask border interrupt for the last
2309ab00a509SMarius Strobl 		 * bounce buffer.
2310ab00a509SMarius Strobl 		 */
2311ab00a509SMarius Strobl 		if (left == sdma_bbufsz) {
2312831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
2313831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2314831f5dcfSAlexander Motin 		}
2315831f5dcfSAlexander Motin 		/* Restart DMA. */
2316831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
2317831f5dcfSAlexander Motin 	}
2318831f5dcfSAlexander Motin 	/* We have got all data. */
2319c3a0f75aSOleksandr Tymoshenko 	if (intmask & SDHCI_INT_DATA_END) {
2320c3a0f75aSOleksandr Tymoshenko 		if (slot->flags & PLATFORM_DATA_STARTED) {
2321c3a0f75aSOleksandr Tymoshenko 			slot->flags &= ~PLATFORM_DATA_STARTED;
2322c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2323c3a0f75aSOleksandr Tymoshenko 		} else
2324831f5dcfSAlexander Motin 			sdhci_finish_data(slot);
2325831f5dcfSAlexander Motin 	}
2326a98788edSIan Lepore done:
2327a98788edSIan Lepore 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
2328a98788edSIan Lepore 		if (slot->flags & PLATFORM_DATA_STARTED) {
2329a98788edSIan Lepore 			slot->flags &= ~PLATFORM_DATA_STARTED;
2330a98788edSIan Lepore 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2331a98788edSIan Lepore 		} else
2332a98788edSIan Lepore 			sdhci_finish_data(slot);
2333a98788edSIan Lepore 	}
2334c3a0f75aSOleksandr Tymoshenko }
2335831f5dcfSAlexander Motin 
2336831f5dcfSAlexander Motin static void
23376dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err)
2338831f5dcfSAlexander Motin {
2339831f5dcfSAlexander Motin 
2340831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2341831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
23426dea80e6SMarius Strobl 		    "there is no active command.\n", acmd_err);
2343831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2344831f5dcfSAlexander Motin 		return;
2345831f5dcfSAlexander Motin 	}
23466dea80e6SMarius Strobl 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err);
2347b8f94506SArtur Rojek 	SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD);
2348831f5dcfSAlexander Motin }
2349831f5dcfSAlexander Motin 
2350d6b3aaf8SOleksandr Tymoshenko void
2351d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
2352831f5dcfSAlexander Motin {
23532b96b955SJustin Hibbits 	uint32_t intmask, present;
23546dea80e6SMarius Strobl 	uint16_t val16;
2355831f5dcfSAlexander Motin 
2356831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2357831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
2358831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
2359831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
2360831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
2361d6b3aaf8SOleksandr Tymoshenko 		return;
2362831f5dcfSAlexander Motin 	}
2363aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 2))
23645b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
23655b69a497SAlexander Motin 
2366aca38eabSMarius Strobl 	/* Handle tuning error interrupt. */
2367aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_TUNEERR)) {
23686dea80e6SMarius Strobl 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR);
2369aca38eabSMarius Strobl 		slot_printf(slot, "Tuning error indicated\n");
2370aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
2371aca38eabSMarius Strobl 		if (slot->curcmd) {
2372aca38eabSMarius Strobl 			slot->curcmd->error = MMC_ERR_BADCRC;
2373aca38eabSMarius Strobl 			sdhci_finish_command(slot);
2374aca38eabSMarius Strobl 		}
2375aca38eabSMarius Strobl 	}
2376aca38eabSMarius Strobl 	/* Handle re-tuning interrupt. */
2377aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_RETUNE))
2378aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
2379831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
2380831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2381639f59f0SIan Lepore 		present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
23822b96b955SJustin Hibbits 		slot->intmask &=
23832b96b955SJustin Hibbits 		    ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
23842b96b955SJustin Hibbits 		slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
23852b96b955SJustin Hibbits 		    SDHCI_INT_CARD_INSERT;
23862b96b955SJustin Hibbits 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
23872b96b955SJustin Hibbits 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2388831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
2389831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
2390b8bf08b1SIan Lepore 		sdhci_handle_card_present_locked(slot, present);
2391831f5dcfSAlexander Motin 	}
2392831f5dcfSAlexander Motin 	/* Handle command interrupts. */
2393831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
2394831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
2395831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
2396831f5dcfSAlexander Motin 	}
2397831f5dcfSAlexander Motin 	/* Handle data interrupts. */
2398831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
2399831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
24007e6ccea3SMarius Strobl 		/* Don't call data_irq in case of errored command. */
24017e586643SIan Lepore 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
2402831f5dcfSAlexander Motin 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
2403831f5dcfSAlexander Motin 	}
2404831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
2405831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
24066dea80e6SMarius Strobl 		/* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */
24076dea80e6SMarius Strobl 		val16 = RD2(slot, SDHCI_ACMD12_ERR);
2408831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
24096dea80e6SMarius Strobl 		sdhci_acmd_irq(slot, val16);
2410831f5dcfSAlexander Motin 	}
2411831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
2412831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
2413831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
2414aca38eabSMarius Strobl 		slot_printf(slot, "Card is consuming too much power!\n");
2415831f5dcfSAlexander Motin 	}
2416aca38eabSMarius Strobl 	intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE |
2417aca38eabSMarius Strobl 	    SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK |
2418aca38eabSMarius Strobl 	    SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER);
2419831f5dcfSAlexander Motin 	/* The rest is unknown. */
2420831f5dcfSAlexander Motin 	if (intmask) {
2421831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
2422831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
2423831f5dcfSAlexander Motin 		    intmask);
2424831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2425831f5dcfSAlexander Motin 	}
2426831f5dcfSAlexander Motin 
2427831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2428831f5dcfSAlexander Motin }
2429831f5dcfSAlexander Motin 
2430d6b3aaf8SOleksandr Tymoshenko int
24311bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which,
24321bacf3beSMarius Strobl     uintptr_t *result)
2433831f5dcfSAlexander Motin {
2434ab00a509SMarius Strobl 	const struct sdhci_slot *slot = device_get_ivars(child);
2435831f5dcfSAlexander Motin 
2436831f5dcfSAlexander Motin 	switch (which) {
2437831f5dcfSAlexander Motin 	default:
2438831f5dcfSAlexander Motin 		return (EINVAL);
2439831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2440bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
2441831f5dcfSAlexander Motin 		break;
2442831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2443bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
2444831f5dcfSAlexander Motin 		break;
2445831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2446bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
2447831f5dcfSAlexander Motin 		break;
2448831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2449bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
2450831f5dcfSAlexander Motin 		break;
2451831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2452bcd91d25SJayachandran C. 		*result = slot->host.f_min;
2453831f5dcfSAlexander Motin 		break;
2454831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
2455bcd91d25SJayachandran C. 		*result = slot->host.f_max;
2456831f5dcfSAlexander Motin 		break;
2457831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2458bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
2459831f5dcfSAlexander Motin 		break;
2460831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2461bcd91d25SJayachandran C. 		*result = slot->host.mode;
2462831f5dcfSAlexander Motin 		break;
2463831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2464bcd91d25SJayachandran C. 		*result = slot->host.ocr;
2465831f5dcfSAlexander Motin 		break;
2466831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2467bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
2468831f5dcfSAlexander Motin 		break;
2469831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2470bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
2471831f5dcfSAlexander Motin 		break;
2472aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2473aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED) {
2474aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) {
2475aca38eabSMarius Strobl 				*result = retune_req_reset;
2476aca38eabSMarius Strobl 				break;
2477aca38eabSMarius Strobl 			}
2478aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) {
2479aca38eabSMarius Strobl 				*result = retune_req_normal;
2480aca38eabSMarius Strobl 				break;
2481aca38eabSMarius Strobl 			}
2482aca38eabSMarius Strobl 		}
2483aca38eabSMarius Strobl 		*result = retune_req_none;
2484aca38eabSMarius Strobl 		break;
24850f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
24860f34084fSMarius Strobl 		*result = slot->host.ios.vccq;
24870f34084fSMarius Strobl 		break;
2488831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2489bcd91d25SJayachandran C. 		*result = slot->host.caps;
2490831f5dcfSAlexander Motin 		break;
2491831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2492bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
2493831f5dcfSAlexander Motin 		break;
24943a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2495aca38eabSMarius Strobl 		/*
2496aca38eabSMarius Strobl 		 * Re-tuning modes 1 and 2 restrict the maximum data length
2497aca38eabSMarius Strobl 		 * per read/write command to 4 MiB.
2498aca38eabSMarius Strobl 		 */
2499aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED &&
2500aca38eabSMarius Strobl 		    (slot->retune_mode == SDHCI_RETUNE_MODE_1 ||
2501aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_2)) {
2502aca38eabSMarius Strobl 			*result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE;
2503aca38eabSMarius Strobl 			break;
2504aca38eabSMarius Strobl 		}
2505bcd91d25SJayachandran C. 		*result = 65535;
25063a4a2557SAlexander Motin 		break;
250772dec079SMarius Strobl 	case MMCBR_IVAR_MAX_BUSY_TIMEOUT:
250872dec079SMarius Strobl 		/*
250972dec079SMarius Strobl 		 * Currently, sdhci_start_data() hardcodes 1 s for all CMDs.
251072dec079SMarius Strobl 		 */
251172dec079SMarius Strobl 		*result = 1000000;
251272dec079SMarius Strobl 		break;
2513831f5dcfSAlexander Motin 	}
2514831f5dcfSAlexander Motin 	return (0);
2515831f5dcfSAlexander Motin }
2516831f5dcfSAlexander Motin 
2517d6b3aaf8SOleksandr Tymoshenko int
25181bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which,
25191bacf3beSMarius Strobl     uintptr_t value)
2520831f5dcfSAlexander Motin {
2521831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
2522b440e965SMarius Strobl 	uint32_t clock, max_clock;
2523b440e965SMarius Strobl 	int i;
2524831f5dcfSAlexander Motin 
252515c440e1SWarner Losh 	if (sdhci_debug > 1)
252615c440e1SWarner Losh 		slot_printf(slot, "%s: var=%d\n", __func__, which);
2527831f5dcfSAlexander Motin 	switch (which) {
2528831f5dcfSAlexander Motin 	default:
2529831f5dcfSAlexander Motin 		return (EINVAL);
2530831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2531831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
2532831f5dcfSAlexander Motin 		break;
2533831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2534831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
2535831f5dcfSAlexander Motin 		break;
2536831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2537831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
2538831f5dcfSAlexander Motin 		break;
2539831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2540831f5dcfSAlexander Motin 		if (value > 0) {
254157677a3aSOleksandr Tymoshenko 			max_clock = slot->max_clk;
254257677a3aSOleksandr Tymoshenko 			clock = max_clock;
254357677a3aSOleksandr Tymoshenko 
254457677a3aSOleksandr Tymoshenko 			if (slot->version < SDHCI_SPEC_300) {
254557677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
254657677a3aSOleksandr Tymoshenko 				    i <<= 1) {
2547831f5dcfSAlexander Motin 					if (clock <= value)
2548831f5dcfSAlexander Motin 						break;
2549831f5dcfSAlexander Motin 					clock >>= 1;
2550831f5dcfSAlexander Motin 				}
2551b440e965SMarius Strobl 			} else {
255257677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
255357677a3aSOleksandr Tymoshenko 				    i += 2) {
255457677a3aSOleksandr Tymoshenko 					if (clock <= value)
255557677a3aSOleksandr Tymoshenko 						break;
255657677a3aSOleksandr Tymoshenko 					clock = max_clock / (i + 2);
255757677a3aSOleksandr Tymoshenko 				}
255857677a3aSOleksandr Tymoshenko 			}
255957677a3aSOleksandr Tymoshenko 
2560831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
2561831f5dcfSAlexander Motin 		} else
2562831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
2563831f5dcfSAlexander Motin 		break;
2564831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2565831f5dcfSAlexander Motin 		slot->host.mode = value;
2566831f5dcfSAlexander Motin 		break;
2567831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2568831f5dcfSAlexander Motin 		slot->host.ocr = value;
2569831f5dcfSAlexander Motin 		break;
2570831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2571831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
2572831f5dcfSAlexander Motin 		break;
2573831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2574831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
2575831f5dcfSAlexander Motin 		break;
25760f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
25770f34084fSMarius Strobl 		slot->host.ios.vccq = value;
25780f34084fSMarius Strobl 		break;
2579831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2580831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
2581831f5dcfSAlexander Motin 		break;
2582831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2583831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2584831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2585831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
25863a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2587aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2588831f5dcfSAlexander Motin 		return (EINVAL);
2589831f5dcfSAlexander Motin 	}
2590831f5dcfSAlexander Motin 	return (0);
2591831f5dcfSAlexander Motin }
2592831f5dcfSAlexander Motin 
259315c440e1SWarner Losh #ifdef MMCCAM
2594a94a63f0SWarner Losh void
2595d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot)
2596a94a63f0SWarner Losh {
2597ab00a509SMarius Strobl 
2598505f6a0cSBjoern A. Zeeb 	if ((slot->devq = cam_simq_alloc(1)) == NULL)
2599a94a63f0SWarner Losh 		goto fail;
2600a94a63f0SWarner Losh 
2601a94a63f0SWarner Losh 	mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF);
2602aeb04e88SWarner Losh 	slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll,
2603da2f833fSBjoern A. Zeeb 	    "sdhci_slot", slot, device_get_unit(slot->bus),
2604a94a63f0SWarner Losh 	    &slot->sim_mtx, 1, 1, slot->devq);
2605a94a63f0SWarner Losh 
2606a94a63f0SWarner Losh 	if (slot->sim == NULL) {
2607a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2608a94a63f0SWarner Losh 		slot_printf(slot, "cannot allocate CAM SIM\n");
2609a94a63f0SWarner Losh 		goto fail;
2610a94a63f0SWarner Losh 	}
2611a94a63f0SWarner Losh 
2612a94a63f0SWarner Losh 	mtx_lock(&slot->sim_mtx);
2613a94a63f0SWarner Losh 	if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) {
2614505f6a0cSBjoern A. Zeeb 		slot_printf(slot, "cannot register SCSI pass-through bus\n");
2615a94a63f0SWarner Losh 		cam_sim_free(slot->sim, FALSE);
2616a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2617a94a63f0SWarner Losh 		mtx_unlock(&slot->sim_mtx);
2618a94a63f0SWarner Losh 		goto fail;
2619a94a63f0SWarner Losh 	}
2620a94a63f0SWarner Losh 	mtx_unlock(&slot->sim_mtx);
2621505f6a0cSBjoern A. Zeeb 
2622a94a63f0SWarner Losh 	/* End CAM-specific init */
2623a94a63f0SWarner Losh 	slot->card_present = 0;
2624a94a63f0SWarner Losh 	sdhci_card_task(slot, 0);
2625a94a63f0SWarner Losh 	return;
2626a94a63f0SWarner Losh 
2627a94a63f0SWarner Losh fail:
2628a94a63f0SWarner Losh 	if (slot->sim != NULL) {
2629a94a63f0SWarner Losh 		mtx_lock(&slot->sim_mtx);
2630a94a63f0SWarner Losh 		xpt_bus_deregister(cam_sim_path(slot->sim));
2631a94a63f0SWarner Losh 		cam_sim_free(slot->sim, FALSE);
2632a94a63f0SWarner Losh 		mtx_unlock(&slot->sim_mtx);
2633a94a63f0SWarner Losh 	}
2634a94a63f0SWarner Losh 
2635a94a63f0SWarner Losh 	if (slot->devq != NULL)
2636a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2637a94a63f0SWarner Losh }
2638a94a63f0SWarner Losh 
2639a94a63f0SWarner Losh void
2640a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb)
2641a94a63f0SWarner Losh {
2642a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2643a94a63f0SWarner Losh 
2644a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2645a94a63f0SWarner Losh 	if (slot == NULL) {
2646a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2647a94a63f0SWarner Losh 		xpt_done(ccb);
2648a94a63f0SWarner Losh 		return;
2649a94a63f0SWarner Losh 	}
2650a94a63f0SWarner Losh 
2651a94a63f0SWarner Losh 	mtx_assert(&slot->sim_mtx, MA_OWNED);
2652a94a63f0SWarner Losh 
2653a94a63f0SWarner Losh 	switch (ccb->ccb_h.func_code) {
2654a94a63f0SWarner Losh 	case XPT_PATH_INQ:
2655cd853791SKonstantin Belousov 		mmc_path_inq(&ccb->cpi, "Deglitch Networks", sim, maxphys);
2656a94a63f0SWarner Losh 		break;
26578c7cd14aSWarner Losh 
2658af2253f6SEmmanuel Vadot 	case XPT_MMC_GET_TRAN_SETTINGS:
2659a94a63f0SWarner Losh 	case XPT_GET_TRAN_SETTINGS:
2660a94a63f0SWarner Losh 	{
2661a94a63f0SWarner Losh 		struct ccb_trans_settings *cts = &ccb->cts;
26625d20e651SIlya Bakulin 		uint32_t max_data;
2663a94a63f0SWarner Losh 
2664a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2665a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n");
2666a94a63f0SWarner Losh 
2667a94a63f0SWarner Losh 		cts->protocol = PROTO_MMCSD;
2668a94a63f0SWarner Losh 		cts->protocol_version = 1;
2669a94a63f0SWarner Losh 		cts->transport = XPORT_MMCSD;
2670a94a63f0SWarner Losh 		cts->transport_version = 1;
2671a94a63f0SWarner Losh 		cts->xport_specific.valid = 0;
2672a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_ocr = slot->host.host_ocr;
2673a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_min = slot->host.f_min;
2674a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_max = slot->host.f_max;
2675a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_caps = slot->host.caps;
26765d20e651SIlya Bakulin 		/*
26775d20e651SIlya Bakulin 		 * Re-tuning modes 1 and 2 restrict the maximum data length
26785d20e651SIlya Bakulin 		 * per read/write command to 4 MiB.
26795d20e651SIlya Bakulin 		 */
26805d20e651SIlya Bakulin 		if (slot->opt & SDHCI_TUNING_ENABLED &&
26815d20e651SIlya Bakulin 		    (slot->retune_mode == SDHCI_RETUNE_MODE_1 ||
26825d20e651SIlya Bakulin 		    slot->retune_mode == SDHCI_RETUNE_MODE_2)) {
26835d20e651SIlya Bakulin 			max_data = 4 * 1024 * 1024 / MMC_SECTOR_SIZE;
26845d20e651SIlya Bakulin 		} else {
26855d20e651SIlya Bakulin 			max_data = 65535;
26865d20e651SIlya Bakulin 		}
26875d20e651SIlya Bakulin 		cts->proto_specific.mmc.host_max_data = max_data;
26885d20e651SIlya Bakulin 
2689a94a63f0SWarner Losh 		memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios));
2690a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2691a94a63f0SWarner Losh 		break;
2692a94a63f0SWarner Losh 	}
2693af2253f6SEmmanuel Vadot 	case XPT_MMC_SET_TRAN_SETTINGS:
2694a94a63f0SWarner Losh 	case XPT_SET_TRAN_SETTINGS:
2695a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2696a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n");
2697a94a63f0SWarner Losh 		sdhci_cam_settran_settings(slot, ccb);
2698a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2699a94a63f0SWarner Losh 		break;
2700a94a63f0SWarner Losh 	case XPT_RESET_BUS:
2701a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2702a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n");
2703a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2704a94a63f0SWarner Losh 		break;
2705a94a63f0SWarner Losh 	case XPT_MMC_IO:
2706a94a63f0SWarner Losh 		/*
2707a94a63f0SWarner Losh 		 * Here is the HW-dependent part of
2708a94a63f0SWarner Losh 		 * sending the command to the underlying h/w
2709a94a63f0SWarner Losh 		 * At some point in the future an interrupt comes.
2710a94a63f0SWarner Losh 		 * Then the request will be marked as completed.
2711a94a63f0SWarner Losh 		 */
2712aca38eabSMarius Strobl 		if (__predict_false(sdhci_debug > 1))
2713a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_MMC_IO\n");
2714a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INPROG;
2715a94a63f0SWarner Losh 
2716160799c6SWarner Losh 		sdhci_cam_request(cam_sim_softc(sim), ccb);
2717a94a63f0SWarner Losh 		return;
2718a94a63f0SWarner Losh 	default:
2719a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INVALID;
2720a94a63f0SWarner Losh 		break;
2721a94a63f0SWarner Losh 	}
2722a94a63f0SWarner Losh 	xpt_done(ccb);
2723a94a63f0SWarner Losh 	return;
2724a94a63f0SWarner Losh }
2725a94a63f0SWarner Losh 
2726a94a63f0SWarner Losh void
2727a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim)
2728a94a63f0SWarner Losh {
272994ff1d9cSAndriy Gapon 	sdhci_generic_intr(cam_sim_softc(sim));
2730a94a63f0SWarner Losh }
2731a94a63f0SWarner Losh 
27326dea80e6SMarius Strobl static int
2733ab00a509SMarius Strobl sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot,
2734ab00a509SMarius Strobl     int proposed_clock)
27356dea80e6SMarius Strobl {
2736a94a63f0SWarner Losh 	int max_clock, clock, i;
2737a94a63f0SWarner Losh 
2738a94a63f0SWarner Losh 	if (proposed_clock == 0)
2739a94a63f0SWarner Losh 		return 0;
2740a94a63f0SWarner Losh 	max_clock = slot->max_clk;
2741a94a63f0SWarner Losh 	clock = max_clock;
2742a94a63f0SWarner Losh 
2743a94a63f0SWarner Losh 	if (slot->version < SDHCI_SPEC_300) {
2744505f6a0cSBjoern A. Zeeb 		for (i = 0; i < SDHCI_200_MAX_DIVIDER; i <<= 1) {
2745a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2746a94a63f0SWarner Losh 				break;
2747a94a63f0SWarner Losh 			clock >>= 1;
2748a94a63f0SWarner Losh 		}
2749a94a63f0SWarner Losh 	} else {
2750505f6a0cSBjoern A. Zeeb 		for (i = 0; i < SDHCI_300_MAX_DIVIDER; i += 2) {
2751a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2752a94a63f0SWarner Losh 				break;
2753a94a63f0SWarner Losh 			clock = max_clock / (i + 2);
2754a94a63f0SWarner Losh 		}
2755a94a63f0SWarner Losh 	}
2756a94a63f0SWarner Losh 	return clock;
2757a94a63f0SWarner Losh }
2758a94a63f0SWarner Losh 
2759ab00a509SMarius Strobl static int
2760a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb)
2761a94a63f0SWarner Losh {
2762a94a63f0SWarner Losh 	struct mmc_ios *ios;
2763ab00a509SMarius Strobl 	const struct mmc_ios *new_ios;
2764ab00a509SMarius Strobl 	const struct ccb_trans_settings_mmc *cts;
2765a94a63f0SWarner Losh 
2766a94a63f0SWarner Losh 	ios = &slot->host.ios;
2767a94a63f0SWarner Losh 	cts = &ccb->cts.proto_specific.mmc;
2768a94a63f0SWarner Losh 	new_ios = &cts->ios;
2769a94a63f0SWarner Losh 
2770a94a63f0SWarner Losh 	/* Update only requested fields */
2771a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CLK) {
2772a94a63f0SWarner Losh 		ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock);
2773b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2774a94a63f0SWarner Losh 			slot_printf(slot, "Clock => %d\n", ios->clock);
2775a94a63f0SWarner Losh 	}
2776a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_VDD) {
2777a94a63f0SWarner Losh 		ios->vdd = new_ios->vdd;
2778b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2779a94a63f0SWarner Losh 			slot_printf(slot, "VDD => %d\n", ios->vdd);
2780a94a63f0SWarner Losh 	}
2781a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CS) {
2782a94a63f0SWarner Losh 		ios->chip_select = new_ios->chip_select;
2783b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2784a94a63f0SWarner Losh 			slot_printf(slot, "CS => %d\n", ios->chip_select);
2785a94a63f0SWarner Losh 	}
2786a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BW) {
2787a94a63f0SWarner Losh 		ios->bus_width = new_ios->bus_width;
2788b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2789a94a63f0SWarner Losh 			slot_printf(slot, "Bus width => %d\n", ios->bus_width);
2790a94a63f0SWarner Losh 	}
2791a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_PM) {
2792a94a63f0SWarner Losh 		ios->power_mode = new_ios->power_mode;
2793b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2794a94a63f0SWarner Losh 			slot_printf(slot, "Power mode => %d\n", ios->power_mode);
2795a94a63f0SWarner Losh 	}
2796a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BT) {
2797a94a63f0SWarner Losh 		ios->timing = new_ios->timing;
2798b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2799a94a63f0SWarner Losh 			slot_printf(slot, "Timing => %d\n", ios->timing);
2800a94a63f0SWarner Losh 	}
2801a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BM) {
2802a94a63f0SWarner Losh 		ios->bus_mode = new_ios->bus_mode;
2803b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2804a94a63f0SWarner Losh 			slot_printf(slot, "Bus mode => %d\n", ios->bus_mode);
2805a94a63f0SWarner Losh 	}
28061a96c143SEmmanuel Vadot 	if (cts->ios_valid & MMC_VCCQ) {
28071a96c143SEmmanuel Vadot 		ios->vccq = new_ios->vccq;
28081a96c143SEmmanuel Vadot 		if (sdhci_debug > 1)
28091a96c143SEmmanuel Vadot 			slot_printf(slot, "VCCQ => %d\n", ios->vccq);
28101a96c143SEmmanuel Vadot 	}
2811a94a63f0SWarner Losh 
2812a94a63f0SWarner Losh 	/* XXX Provide a way to call a chip-specific IOS update, required for TI */
2813a94a63f0SWarner Losh 	return (sdhci_cam_update_ios(slot));
2814a94a63f0SWarner Losh }
2815a94a63f0SWarner Losh 
2816ab00a509SMarius Strobl static int
2817a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot)
2818a94a63f0SWarner Losh {
2819a94a63f0SWarner Losh 	struct mmc_ios *ios = &slot->host.ios;
2820a94a63f0SWarner Losh 
2821b18f2ef4SEmmanuel Vadot 	if (sdhci_debug > 1)
2822a94a63f0SWarner Losh 		slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n",
2823a94a63f0SWarner Losh 		    __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing);
2824a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2825a94a63f0SWarner Losh 	/* Do full reset on bus power down to clear from any state. */
2826a94a63f0SWarner Losh 	if (ios->power_mode == power_off) {
2827a94a63f0SWarner Losh 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
2828a94a63f0SWarner Losh 		sdhci_init(slot);
2829a94a63f0SWarner Losh 	}
2830a94a63f0SWarner Losh 	/* Configure the bus. */
2831a94a63f0SWarner Losh 	sdhci_set_clock(slot, ios->clock);
2832a94a63f0SWarner Losh 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
2833a94a63f0SWarner Losh 	if (ios->bus_width == bus_width_8) {
2834a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
2835a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2836a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_4) {
2837a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2838a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
2839a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_1) {
2840a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2841a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2842a94a63f0SWarner Losh 	} else {
2843a94a63f0SWarner Losh 		panic("Invalid bus width: %d", ios->bus_width);
2844a94a63f0SWarner Losh 	}
2845a94a63f0SWarner Losh 	if (ios->timing == bus_timing_hs &&
2846a94a63f0SWarner Losh 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
2847a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_HISPD;
2848a94a63f0SWarner Losh 	else
2849a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
2850a94a63f0SWarner Losh 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
2851a94a63f0SWarner Losh 	/* Some controllers like reset after bus changes. */
2852a94a63f0SWarner Losh 	if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
2853b8f94506SArtur Rojek 		SDHCI_RESET(slot->bus, slot,
2854b8f94506SArtur Rojek 		    SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2855a94a63f0SWarner Losh 
2856a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2857a94a63f0SWarner Losh 	return (0);
2858a94a63f0SWarner Losh }
2859a94a63f0SWarner Losh 
2860ab00a509SMarius Strobl static int
2861a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb)
2862a94a63f0SWarner Losh {
2863ab00a509SMarius Strobl 	const struct ccb_mmcio *mmcio;
2864a94a63f0SWarner Losh 
2865a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
2866a94a63f0SWarner Losh 
2867a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2868a94a63f0SWarner Losh /*	if (slot->req != NULL) {
2869a94a63f0SWarner Losh 		SDHCI_UNLOCK(slot);
2870a94a63f0SWarner Losh 		return (EBUSY);
2871a94a63f0SWarner Losh 	}
2872a94a63f0SWarner Losh */
2873aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
28745d5ae066SIlya Bakulin 		slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x "
28755d5ae066SIlya Bakulin 		    "blksz=%zu blkcnt=%zu\n",
2876a94a63f0SWarner Losh 		    mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags,
2877a94a63f0SWarner Losh 		    mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0,
28785d5ae066SIlya Bakulin 		    mmcio->cmd.data != NULL ? mmcio->cmd.data->flags : 0,
28795d5ae066SIlya Bakulin 		    mmcio->cmd.data != NULL ? mmcio->cmd.data->block_size : 0,
28805d5ae066SIlya Bakulin 		    mmcio->cmd.data != NULL ? mmcio->cmd.data->block_count : 0);
2881a94a63f0SWarner Losh 	}
2882a94a63f0SWarner Losh 	if (mmcio->cmd.data != NULL) {
2883a94a63f0SWarner Losh 		if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0)
2884a94a63f0SWarner Losh 			panic("data->len = %d, data->flags = %d -- something is b0rked",
2885a94a63f0SWarner Losh 			    (int)mmcio->cmd.data->len, mmcio->cmd.data->flags);
2886a94a63f0SWarner Losh 	}
2887a94a63f0SWarner Losh 	slot->ccb = ccb;
2888a94a63f0SWarner Losh 	slot->flags = 0;
2889a94a63f0SWarner Losh 	sdhci_start(slot);
2890a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2891a94a63f0SWarner Losh 	return (0);
2892a94a63f0SWarner Losh }
289315c440e1SWarner Losh #endif /* MMCCAM */
2894a94a63f0SWarner Losh 
2895ab00a509SMarius Strobl MODULE_VERSION(sdhci, SDHCI_VERSION);
2896