xref: /freebsd/sys/dev/sdhci/sdhci.c (revision 3ac5012e52ee3d9abf9c18e1302d8c6851858fd2)
1831f5dcfSAlexander Motin /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4831f5dcfSAlexander Motin  * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
5aca38eabSMarius Strobl  * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
6831f5dcfSAlexander Motin  * All rights reserved.
7831f5dcfSAlexander Motin  *
8831f5dcfSAlexander Motin  * Redistribution and use in source and binary forms, with or without
9831f5dcfSAlexander Motin  * modification, are permitted provided that the following conditions
10831f5dcfSAlexander Motin  * are met:
11831f5dcfSAlexander Motin  * 1. Redistributions of source code must retain the above copyright
12831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer.
13831f5dcfSAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
14831f5dcfSAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
15831f5dcfSAlexander Motin  *    documentation and/or other materials provided with the distribution.
16831f5dcfSAlexander Motin  *
17831f5dcfSAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18831f5dcfSAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19831f5dcfSAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20831f5dcfSAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21831f5dcfSAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22831f5dcfSAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23831f5dcfSAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24831f5dcfSAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25831f5dcfSAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26831f5dcfSAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27831f5dcfSAlexander Motin  */
28831f5dcfSAlexander Motin 
29831f5dcfSAlexander Motin #include <sys/cdefs.h>
30831f5dcfSAlexander Motin __FBSDID("$FreeBSD$");
31831f5dcfSAlexander Motin 
32831f5dcfSAlexander Motin #include <sys/param.h>
33831f5dcfSAlexander Motin #include <sys/systm.h>
34831f5dcfSAlexander Motin #include <sys/bus.h>
35e64f01a9SIan Lepore #include <sys/callout.h>
36831f5dcfSAlexander Motin #include <sys/conf.h>
37831f5dcfSAlexander Motin #include <sys/kernel.h>
38aca38eabSMarius Strobl #include <sys/kobj.h>
39ab00a509SMarius Strobl #include <sys/libkern.h>
40831f5dcfSAlexander Motin #include <sys/lock.h>
41aca38eabSMarius Strobl #include <sys/malloc.h>
42831f5dcfSAlexander Motin #include <sys/module.h>
43831f5dcfSAlexander Motin #include <sys/mutex.h>
44831f5dcfSAlexander Motin #include <sys/resource.h>
45831f5dcfSAlexander Motin #include <sys/rman.h>
465b69a497SAlexander Motin #include <sys/sysctl.h>
47831f5dcfSAlexander Motin #include <sys/taskqueue.h>
48d00c1f7fSBartlomiej Grzesik #include <sys/sbuf.h>
49831f5dcfSAlexander Motin 
50831f5dcfSAlexander Motin #include <machine/bus.h>
51831f5dcfSAlexander Motin #include <machine/resource.h>
52831f5dcfSAlexander Motin #include <machine/stdarg.h>
53831f5dcfSAlexander Motin 
54831f5dcfSAlexander Motin #include <dev/mmc/bridge.h>
55831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h>
56831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h>
57831f5dcfSAlexander Motin 
58aca38eabSMarius Strobl #include <dev/sdhci/sdhci.h>
59aca38eabSMarius Strobl 
60a94a63f0SWarner Losh #include <cam/cam.h>
61a94a63f0SWarner Losh #include <cam/cam_ccb.h>
62a94a63f0SWarner Losh #include <cam/cam_debug.h>
63a94a63f0SWarner Losh #include <cam/cam_sim.h>
64a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h>
65a94a63f0SWarner Losh 
66831f5dcfSAlexander Motin #include "mmcbr_if.h"
67d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h"
68831f5dcfSAlexander Motin 
69a94a63f0SWarner Losh #include "opt_mmccam.h"
70a94a63f0SWarner Losh 
717029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
727029da5cSPawel Biernacki     "sdhci driver");
73831f5dcfSAlexander Motin 
74a94a63f0SWarner Losh static int sdhci_debug = 0;
757e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0,
767e6ccea3SMarius Strobl     "Debug level");
770f34084fSMarius Strobl u_int sdhci_quirk_clear = 0;
780f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear,
790f34084fSMarius Strobl     0, "Mask of quirks to clear");
800f34084fSMarius Strobl u_int sdhci_quirk_set = 0;
810f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0,
820f34084fSMarius Strobl     "Mask of quirks to set");
835b69a497SAlexander Motin 
84d6b3aaf8SOleksandr Tymoshenko #define	RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
85d6b3aaf8SOleksandr Tymoshenko #define	RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
86d6b3aaf8SOleksandr Tymoshenko #define	RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
87d6b3aaf8SOleksandr Tymoshenko #define	RD_MULTI_4(slot, off, ptr, count)	\
88d6b3aaf8SOleksandr Tymoshenko     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
89831f5dcfSAlexander Motin 
90d6b3aaf8SOleksandr Tymoshenko #define	WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
91d6b3aaf8SOleksandr Tymoshenko #define	WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
92d6b3aaf8SOleksandr Tymoshenko #define	WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
93d6b3aaf8SOleksandr Tymoshenko #define	WR_MULTI_4(slot, off, ptr, count)	\
94d6b3aaf8SOleksandr Tymoshenko     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
95831f5dcfSAlexander Motin 
966dea80e6SMarius Strobl static void sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err);
97aca38eabSMarius Strobl static void sdhci_card_poll(void *arg);
98aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending);
996dea80e6SMarius Strobl static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask);
1006dea80e6SMarius Strobl static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask);
101aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset);
1026dea80e6SMarius Strobl static void sdhci_handle_card_present_locked(struct sdhci_slot *slot,
1036dea80e6SMarius Strobl     bool is_present);
1046dea80e6SMarius Strobl static void sdhci_finish_command(struct sdhci_slot *slot);
1056dea80e6SMarius Strobl static void sdhci_init(struct sdhci_slot *slot);
1066dea80e6SMarius Strobl static void sdhci_read_block_pio(struct sdhci_slot *slot);
1076dea80e6SMarius Strobl static void sdhci_req_done(struct sdhci_slot *slot);
108aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req);
1096dea80e6SMarius Strobl static void sdhci_reset(struct sdhci_slot *slot, uint8_t mask);
110aca38eabSMarius Strobl static void sdhci_retune(void *arg);
111831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
1126dea80e6SMarius Strobl static void sdhci_set_power(struct sdhci_slot *slot, u_char power);
1136dea80e6SMarius Strobl static void sdhci_set_transfer_mode(struct sdhci_slot *slot,
114ab00a509SMarius Strobl    const struct mmc_data *data);
115831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot);
1166dea80e6SMarius Strobl static void sdhci_timeout(void *arg);
1176dea80e6SMarius Strobl static void sdhci_start_command(struct sdhci_slot *slot,
1186dea80e6SMarius Strobl    struct mmc_command *cmd);
119ab00a509SMarius Strobl static void sdhci_start_data(struct sdhci_slot *slot,
120ab00a509SMarius Strobl    const struct mmc_data *data);
1216dea80e6SMarius Strobl static void sdhci_write_block_pio(struct sdhci_slot *slot);
1226dea80e6SMarius Strobl static void sdhci_transfer_pio(struct sdhci_slot *slot);
123831f5dcfSAlexander Motin 
12415c440e1SWarner Losh #ifdef MMCCAM
125a94a63f0SWarner Losh /* CAM-related */
126a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb);
127ab00a509SMarius Strobl static int sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot,
1286dea80e6SMarius Strobl     int proposed_clock);
129a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim);
1306dea80e6SMarius Strobl static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb);
131a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb);
1326dea80e6SMarius Strobl static int sdhci_cam_update_ios(struct sdhci_slot *slot);
13315c440e1SWarner Losh #endif
134a94a63f0SWarner Losh 
135831f5dcfSAlexander Motin /* helper routines */
1367d8700bcSMarcin Wojtas static int sdhci_dma_alloc(struct sdhci_slot *slot, uint32_t caps);
137ab00a509SMarius Strobl static void sdhci_dma_free(struct sdhci_slot *slot);
138d00c1f7fSBartlomiej Grzesik static void sdhci_dumpcaps(struct sdhci_slot *slot);
139d00c1f7fSBartlomiej Grzesik static void sdhci_dumpcaps_buf(struct sdhci_slot *slot, struct sbuf *s);
1400f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot);
141d00c1f7fSBartlomiej Grzesik static void sdhci_dumpregs_buf(struct sdhci_slot *slot, struct sbuf *s);
142d00c1f7fSBartlomiej Grzesik static int sdhci_syctl_dumpcaps(SYSCTL_HANDLER_ARGS);
143d00c1f7fSBartlomiej Grzesik static int sdhci_syctl_dumpregs(SYSCTL_HANDLER_ARGS);
1446dea80e6SMarius Strobl static void sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs,
1456dea80e6SMarius Strobl     int error);
146ab00a509SMarius Strobl static int slot_printf(const struct sdhci_slot *slot, const char * fmt, ...)
1470f34084fSMarius Strobl     __printflike(2, 3);
148d00c1f7fSBartlomiej Grzesik static int slot_sprintf(const struct sdhci_slot *slot, struct sbuf *s,
149d00c1f7fSBartlomiej Grzesik     const char * fmt, ...) __printflike(3, 4);
150ab00a509SMarius Strobl static uint32_t sdhci_tuning_intmask(const struct sdhci_slot *slot);
1510f34084fSMarius Strobl 
152831f5dcfSAlexander Motin #define	SDHCI_LOCK(_slot)		mtx_lock(&(_slot)->mtx)
153831f5dcfSAlexander Motin #define	SDHCI_UNLOCK(_slot)		mtx_unlock(&(_slot)->mtx)
154831f5dcfSAlexander Motin #define	SDHCI_LOCK_INIT(_slot) \
155831f5dcfSAlexander Motin 	mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
156831f5dcfSAlexander Motin #define	SDHCI_LOCK_DESTROY(_slot)	mtx_destroy(&_slot->mtx);
157831f5dcfSAlexander Motin #define	SDHCI_ASSERT_LOCKED(_slot)	mtx_assert(&_slot->mtx, MA_OWNED);
158831f5dcfSAlexander Motin #define	SDHCI_ASSERT_UNLOCKED(_slot)	mtx_assert(&_slot->mtx, MA_NOTOWNED);
159831f5dcfSAlexander Motin 
16033aad34dSOleksandr Tymoshenko #define	SDHCI_DEFAULT_MAX_FREQ	50
16133aad34dSOleksandr Tymoshenko 
16257677a3aSOleksandr Tymoshenko #define	SDHCI_200_MAX_DIVIDER	256
16357677a3aSOleksandr Tymoshenko #define	SDHCI_300_MAX_DIVIDER	2046
16457677a3aSOleksandr Tymoshenko 
165639f59f0SIan Lepore #define	SDHCI_CARD_PRESENT_TICKS	(hz / 5)
166639f59f0SIan Lepore #define	SDHCI_INSERT_DELAY_TICKS	(hz / 2)
167639f59f0SIan Lepore 
16893efdc63SAdrian Chadd /*
16993efdc63SAdrian Chadd  * Broadcom BCM577xx Controller Constants
17093efdc63SAdrian Chadd  */
1711bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */
1721bacf3beSMarius Strobl #define	BCM577XX_DEFAULT_MAX_DIVIDER	256
1731bacf3beSMarius Strobl /* Alternative clock's base frequency. */
1741bacf3beSMarius Strobl #define	BCM577XX_ALT_CLOCK_BASE		63000000
17593efdc63SAdrian Chadd 
17693efdc63SAdrian Chadd #define	BCM577XX_HOST_CONTROL		0x198
17793efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_MASK	0xFFFFCFFF
17893efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_SHIFT	12
17993efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_DEFAULT	0x0
18093efdc63SAdrian Chadd #define	BCM577XX_CTRL_CLKSEL_64MHZ	0x3
18193efdc63SAdrian Chadd 
182831f5dcfSAlexander Motin static void
183831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
184831f5dcfSAlexander Motin {
1857e6ccea3SMarius Strobl 
186831f5dcfSAlexander Motin 	if (error != 0) {
187831f5dcfSAlexander Motin 		printf("getaddr: error %d\n", error);
188831f5dcfSAlexander Motin 		return;
189831f5dcfSAlexander Motin 	}
190831f5dcfSAlexander Motin 	*(bus_addr_t *)arg = segs[0].ds_addr;
191831f5dcfSAlexander Motin }
192831f5dcfSAlexander Motin 
193d6b3aaf8SOleksandr Tymoshenko static int
194ab00a509SMarius Strobl slot_printf(const struct sdhci_slot *slot, const char * fmt, ...)
195d6b3aaf8SOleksandr Tymoshenko {
19627d72fe1SBjoern A. Zeeb 	char buf[128];
197d6b3aaf8SOleksandr Tymoshenko 	va_list ap;
198d6b3aaf8SOleksandr Tymoshenko 	int retval;
199d6b3aaf8SOleksandr Tymoshenko 
20027d72fe1SBjoern A. Zeeb 	/*
20127d72fe1SBjoern A. Zeeb 	 * Make sure we print a single line all together rather than in two
20227d72fe1SBjoern A. Zeeb 	 * halves to avoid console gibberish bingo.
20327d72fe1SBjoern A. Zeeb 	 */
204d6b3aaf8SOleksandr Tymoshenko 	va_start(ap, fmt);
20527d72fe1SBjoern A. Zeeb 	retval = vsnprintf(buf, sizeof(buf), fmt, ap);
206d6b3aaf8SOleksandr Tymoshenko 	va_end(ap);
20727d72fe1SBjoern A. Zeeb 
20827d72fe1SBjoern A. Zeeb 	retval += printf("%s-slot%d: %s",
20927d72fe1SBjoern A. Zeeb 	    device_get_nameunit(slot->bus), slot->num, buf);
210d6b3aaf8SOleksandr Tymoshenko 	return (retval);
211d6b3aaf8SOleksandr Tymoshenko }
212d6b3aaf8SOleksandr Tymoshenko 
213d00c1f7fSBartlomiej Grzesik static int
214d00c1f7fSBartlomiej Grzesik slot_sprintf(const struct sdhci_slot *slot, struct sbuf *s,
215d00c1f7fSBartlomiej Grzesik     const char * fmt, ...)
216d00c1f7fSBartlomiej Grzesik {
217d00c1f7fSBartlomiej Grzesik 	va_list ap;
218d00c1f7fSBartlomiej Grzesik 	int retval;
219d00c1f7fSBartlomiej Grzesik 
220d00c1f7fSBartlomiej Grzesik 	retval = sbuf_printf(s, "%s-slot%d: ", device_get_nameunit(slot->bus), slot->num);
221d00c1f7fSBartlomiej Grzesik 
222d00c1f7fSBartlomiej Grzesik 	va_start(ap, fmt);
223d00c1f7fSBartlomiej Grzesik 	retval += sbuf_vprintf(s, fmt, ap);
224d00c1f7fSBartlomiej Grzesik 	va_end(ap);
225d00c1f7fSBartlomiej Grzesik 
226d00c1f7fSBartlomiej Grzesik 	return (retval);
227d00c1f7fSBartlomiej Grzesik }
228d00c1f7fSBartlomiej Grzesik 
229d00c1f7fSBartlomiej Grzesik static void
230d00c1f7fSBartlomiej Grzesik sdhci_dumpregs_buf(struct sdhci_slot *slot, struct sbuf *s)
231d00c1f7fSBartlomiej Grzesik {
232d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "============== REGISTER DUMP ==============\n");
233d00c1f7fSBartlomiej Grzesik 
234d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Sys addr: 0x%08x | Version:  0x%08x\n",
235d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
236d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
237d00c1f7fSBartlomiej Grzesik 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
238d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Argument: 0x%08x | Trn mode: 0x%08x\n",
239d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
240d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Present:  0x%08x | Host ctl: 0x%08x\n",
241d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
242d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Power:    0x%08x | Blk gap:  0x%08x\n",
243d00c1f7fSBartlomiej Grzesik 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
244d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Wake-up:  0x%08x | Clock:    0x%08x\n",
245d00c1f7fSBartlomiej Grzesik 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
246d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Timeout:  0x%08x | Int stat: 0x%08x\n",
247d00c1f7fSBartlomiej Grzesik 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
248d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Int enab: 0x%08x | Sig enab: 0x%08x\n",
249d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
250d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "AC12 err: 0x%08x | Host ctl2:0x%08x\n",
251d00c1f7fSBartlomiej Grzesik 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2));
252d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Caps:     0x%08x | Caps2:    0x%08x\n",
253d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2));
254d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "Max curr: 0x%08x | ADMA err: 0x%08x\n",
255d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR));
256d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "ADMA addr:0x%08x | Slot int: 0x%08x\n",
257d00c1f7fSBartlomiej Grzesik 	    RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS));
258d00c1f7fSBartlomiej Grzesik 
259d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,  "===========================================\n");
260d00c1f7fSBartlomiej Grzesik }
261d00c1f7fSBartlomiej Grzesik 
262831f5dcfSAlexander Motin static void
263831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot)
264831f5dcfSAlexander Motin {
265d00c1f7fSBartlomiej Grzesik 	struct sbuf s;
2667e6ccea3SMarius Strobl 
267*3ac5012eSBartlomiej Grzesik 	if (sbuf_new(&s, NULL, 1024, SBUF_NOWAIT | SBUF_AUTOEXTEND) == NULL) {
268*3ac5012eSBartlomiej Grzesik 		slot_printf(slot, "sdhci_dumpregs: Failed to allocate memory for sbuf\n");
269*3ac5012eSBartlomiej Grzesik 		return;
270*3ac5012eSBartlomiej Grzesik 	}
271*3ac5012eSBartlomiej Grzesik 
272d00c1f7fSBartlomiej Grzesik 	sbuf_set_drain(&s, &sbuf_printf_drain, NULL);
273d00c1f7fSBartlomiej Grzesik 	sdhci_dumpregs_buf(slot, &s);
274d00c1f7fSBartlomiej Grzesik 	sbuf_finish(&s);
275d00c1f7fSBartlomiej Grzesik 	sbuf_delete(&s);
276d00c1f7fSBartlomiej Grzesik }
277831f5dcfSAlexander Motin 
278d00c1f7fSBartlomiej Grzesik static int
279d00c1f7fSBartlomiej Grzesik sdhci_syctl_dumpregs(SYSCTL_HANDLER_ARGS)
280d00c1f7fSBartlomiej Grzesik {
281d00c1f7fSBartlomiej Grzesik 	struct sdhci_slot *slot = arg1;
282d00c1f7fSBartlomiej Grzesik 	struct sbuf s;
283831f5dcfSAlexander Motin 
284d00c1f7fSBartlomiej Grzesik 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
285d00c1f7fSBartlomiej Grzesik 	sbuf_putc(&s, '\n');
286d00c1f7fSBartlomiej Grzesik 	sdhci_dumpregs_buf(slot, &s);
287d00c1f7fSBartlomiej Grzesik 	sbuf_finish(&s);
288d00c1f7fSBartlomiej Grzesik 	sbuf_delete(&s);
289d00c1f7fSBartlomiej Grzesik 
290d00c1f7fSBartlomiej Grzesik 	return (0);
291d00c1f7fSBartlomiej Grzesik }
292d00c1f7fSBartlomiej Grzesik 
293d00c1f7fSBartlomiej Grzesik static void
294d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps_buf(struct sdhci_slot *slot, struct sbuf *s)
295d00c1f7fSBartlomiej Grzesik {
296d00c1f7fSBartlomiej Grzesik 	int host_caps = slot->host.caps;
297d00c1f7fSBartlomiej Grzesik 	int caps = slot->caps;
298d00c1f7fSBartlomiej Grzesik 
299d00c1f7fSBartlomiej Grzesik 	slot_sprintf(slot, s,
300d00c1f7fSBartlomiej Grzesik 	    "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n",
301d00c1f7fSBartlomiej Grzesik 	    slot->max_clk / 1000000,
302d00c1f7fSBartlomiej Grzesik 	    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
303d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
304d00c1f7fSBartlomiej Grzesik 	    ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
305d00c1f7fSBartlomiej Grzesik 	    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
306d00c1f7fSBartlomiej Grzesik 	    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
307d00c1f7fSBartlomiej Grzesik 	    ((caps & SDHCI_CAN_VDD_180) &&
308d00c1f7fSBartlomiej Grzesik 	    (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "",
309d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "",
310d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "",
311d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "",
312d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "",
313d00c1f7fSBartlomiej Grzesik 	    (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "",
314d00c1f7fSBartlomiej Grzesik 	    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO",
315d00c1f7fSBartlomiej Grzesik 	    (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" :
316d00c1f7fSBartlomiej Grzesik 	    (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" :
317d00c1f7fSBartlomiej Grzesik 	    "removable");
318d00c1f7fSBartlomiej Grzesik 	if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 |
319d00c1f7fSBartlomiej Grzesik 	    MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE))
320d00c1f7fSBartlomiej Grzesik 		slot_sprintf(slot, s, "eMMC:%s%s%s%s\n",
321d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "",
322d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "",
323d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "",
324d00c1f7fSBartlomiej Grzesik 		    ((host_caps &
325d00c1f7fSBartlomiej Grzesik 		    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ==
326d00c1f7fSBartlomiej Grzesik 		    (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ?
327d00c1f7fSBartlomiej Grzesik 		    " HS400ES" : "");
328d00c1f7fSBartlomiej Grzesik 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
329d00c1f7fSBartlomiej Grzesik 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104))
330d00c1f7fSBartlomiej Grzesik 		slot_sprintf(slot, s, "UHS-I:%s%s%s%s%s\n",
331d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "",
332d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "",
333d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "",
334d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "",
335d00c1f7fSBartlomiej Grzesik 		    (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : "");
336d00c1f7fSBartlomiej Grzesik 	if (slot->opt & SDHCI_TUNING_SUPPORTED)
337d00c1f7fSBartlomiej Grzesik 		slot_sprintf(slot, s,
338d00c1f7fSBartlomiej Grzesik 		    "Re-tuning count %d secs, mode %d\n",
339d00c1f7fSBartlomiej Grzesik 		    slot->retune_count, slot->retune_mode + 1);
340d00c1f7fSBartlomiej Grzesik }
341d00c1f7fSBartlomiej Grzesik 
342d00c1f7fSBartlomiej Grzesik static void
343d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps(struct sdhci_slot *slot)
344d00c1f7fSBartlomiej Grzesik {
345d00c1f7fSBartlomiej Grzesik 	struct sbuf s;
346d00c1f7fSBartlomiej Grzesik 
347*3ac5012eSBartlomiej Grzesik 	if (sbuf_new(&s, NULL, 1024, SBUF_NOWAIT | SBUF_AUTOEXTEND) == NULL) {
348*3ac5012eSBartlomiej Grzesik 		slot_printf(slot, "sdhci_dumpcaps: Failed to allocate memory for sbuf\n");
349*3ac5012eSBartlomiej Grzesik 		return;
350*3ac5012eSBartlomiej Grzesik 	}
351*3ac5012eSBartlomiej Grzesik 
352d00c1f7fSBartlomiej Grzesik 	sbuf_set_drain(&s, &sbuf_printf_drain, NULL);
353d00c1f7fSBartlomiej Grzesik 	sdhci_dumpcaps_buf(slot, &s);
354d00c1f7fSBartlomiej Grzesik 	sbuf_finish(&s);
355d00c1f7fSBartlomiej Grzesik 	sbuf_delete(&s);
356d00c1f7fSBartlomiej Grzesik }
357d00c1f7fSBartlomiej Grzesik 
358d00c1f7fSBartlomiej Grzesik static int
359d00c1f7fSBartlomiej Grzesik sdhci_syctl_dumpcaps(SYSCTL_HANDLER_ARGS)
360d00c1f7fSBartlomiej Grzesik {
361d00c1f7fSBartlomiej Grzesik 	struct sdhci_slot *slot = arg1;
362d00c1f7fSBartlomiej Grzesik 	struct sbuf s;
363d00c1f7fSBartlomiej Grzesik 
364d00c1f7fSBartlomiej Grzesik 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
365d00c1f7fSBartlomiej Grzesik 	sbuf_putc(&s, '\n');
366d00c1f7fSBartlomiej Grzesik 	sdhci_dumpcaps_buf(slot, &s);
367d00c1f7fSBartlomiej Grzesik 	sbuf_finish(&s);
368d00c1f7fSBartlomiej Grzesik 	sbuf_delete(&s);
369d00c1f7fSBartlomiej Grzesik 
370d00c1f7fSBartlomiej Grzesik 	return (0);
371831f5dcfSAlexander Motin }
372831f5dcfSAlexander Motin 
373831f5dcfSAlexander Motin static void
374831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
375831f5dcfSAlexander Motin {
376831f5dcfSAlexander Motin 	int timeout;
377b440e965SMarius Strobl 	uint32_t clock;
378831f5dcfSAlexander Motin 
379d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
3806e37fb2bSIan Lepore 		if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
381831f5dcfSAlexander Motin 			return;
382831f5dcfSAlexander Motin 	}
383831f5dcfSAlexander Motin 
384831f5dcfSAlexander Motin 	/* Some controllers need this kick or reset won't work. */
385831f5dcfSAlexander Motin 	if ((mask & SDHCI_RESET_ALL) == 0 &&
386d6b3aaf8SOleksandr Tymoshenko 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
387831f5dcfSAlexander Motin 		/* This is to force an update */
388831f5dcfSAlexander Motin 		clock = slot->clock;
389831f5dcfSAlexander Motin 		slot->clock = 0;
390831f5dcfSAlexander Motin 		sdhci_set_clock(slot, clock);
391831f5dcfSAlexander Motin 	}
392831f5dcfSAlexander Motin 
393d8208d9eSAlexander Motin 	if (mask & SDHCI_RESET_ALL) {
394831f5dcfSAlexander Motin 		slot->clock = 0;
395d8208d9eSAlexander Motin 		slot->power = 0;
396d8208d9eSAlexander Motin 	}
397831f5dcfSAlexander Motin 
39861bc42f7SIan Lepore 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
39961bc42f7SIan Lepore 
40061bc42f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
40161bc42f7SIan Lepore 		/*
40261bc42f7SIan Lepore 		 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
40361bc42f7SIan Lepore 		 * specification.  The reset bit has internal propagation delay,
40461bc42f7SIan Lepore 		 * so a fast read after write returns 0 even if reset process is
40561bc42f7SIan Lepore 		 * in progress.  The workaround is to poll for 1 before polling
40661bc42f7SIan Lepore 		 * for 0.  In the worst case, if we miss seeing it asserted the
40761bc42f7SIan Lepore 		 * time we spent waiting is enough to ensure the reset finishes.
40861bc42f7SIan Lepore 		 */
40961bc42f7SIan Lepore 		timeout = 10000;
41061bc42f7SIan Lepore 		while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
41161bc42f7SIan Lepore 			if (timeout <= 0)
41261bc42f7SIan Lepore 				break;
41361bc42f7SIan Lepore 			timeout--;
41461bc42f7SIan Lepore 			DELAY(1);
41561bc42f7SIan Lepore 		}
41661bc42f7SIan Lepore 	}
41761bc42f7SIan Lepore 
418831f5dcfSAlexander Motin 	/* Wait max 100 ms */
41961bc42f7SIan Lepore 	timeout = 10000;
420831f5dcfSAlexander Motin 	/* Controller clears the bits when it's done */
42161bc42f7SIan Lepore 	while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
42261bc42f7SIan Lepore 		if (timeout <= 0) {
42361bc42f7SIan Lepore 			slot_printf(slot, "Reset 0x%x never completed.\n",
42461bc42f7SIan Lepore 			    mask);
425831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
426831f5dcfSAlexander Motin 			return;
427831f5dcfSAlexander Motin 		}
428831f5dcfSAlexander Motin 		timeout--;
42961bc42f7SIan Lepore 		DELAY(10);
430831f5dcfSAlexander Motin 	}
431831f5dcfSAlexander Motin }
432831f5dcfSAlexander Motin 
433aca38eabSMarius Strobl static uint32_t
434ab00a509SMarius Strobl sdhci_tuning_intmask(const struct sdhci_slot *slot)
435aca38eabSMarius Strobl {
436aca38eabSMarius Strobl 	uint32_t intmask;
437aca38eabSMarius Strobl 
438aca38eabSMarius Strobl 	intmask = 0;
43978f8baa8SMarius Strobl 	if (slot->opt & SDHCI_TUNING_ENABLED) {
440aca38eabSMarius Strobl 		intmask |= SDHCI_INT_TUNEERR;
441aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_2 ||
442aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_3)
443aca38eabSMarius Strobl 			intmask |= SDHCI_INT_RETUNE;
444aca38eabSMarius Strobl 	}
445aca38eabSMarius Strobl 	return (intmask);
446aca38eabSMarius Strobl }
447aca38eabSMarius Strobl 
448831f5dcfSAlexander Motin static void
449831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot)
450831f5dcfSAlexander Motin {
451831f5dcfSAlexander Motin 
452831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
453831f5dcfSAlexander Motin 
454831f5dcfSAlexander Motin 	/* Enable interrupts. */
455831f5dcfSAlexander Motin 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
456831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
457831f5dcfSAlexander Motin 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
458831f5dcfSAlexander Motin 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
459831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
4603685b398SWarner Losh 	    SDHCI_INT_ACMD12ERR;
461639f59f0SIan Lepore 
462639f59f0SIan Lepore 	if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
463639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
464639f59f0SIan Lepore 		slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
465639f59f0SIan Lepore 	}
466639f59f0SIan Lepore 
467cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
468831f5dcfSAlexander Motin 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
469831f5dcfSAlexander Motin }
470831f5dcfSAlexander Motin 
471831f5dcfSAlexander Motin static void
472831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
473831f5dcfSAlexander Motin {
47493efdc63SAdrian Chadd 	uint32_t clk_base;
47593efdc63SAdrian Chadd 	uint32_t clk_sel;
476831f5dcfSAlexander Motin 	uint32_t res;
477831f5dcfSAlexander Motin 	uint16_t clk;
4788f3b7d56SOleksandr Tymoshenko 	uint16_t div;
479831f5dcfSAlexander Motin 	int timeout;
480831f5dcfSAlexander Motin 
481831f5dcfSAlexander Motin 	if (clock == slot->clock)
482831f5dcfSAlexander Motin 		return;
483831f5dcfSAlexander Motin 	slot->clock = clock;
484831f5dcfSAlexander Motin 
485831f5dcfSAlexander Motin 	/* Turn off the clock. */
4864ddc0172SIan Lepore 	clk = RD2(slot, SDHCI_CLOCK_CONTROL);
4874ddc0172SIan Lepore 	WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
488b440e965SMarius Strobl 	/* If no clock requested - leave it so. */
489831f5dcfSAlexander Motin 	if (clock == 0)
490831f5dcfSAlexander Motin 		return;
491ceb9e9f7SIan Lepore 
49293efdc63SAdrian Chadd 	/* Determine the clock base frequency */
49393efdc63SAdrian Chadd 	clk_base = slot->max_clk;
49493efdc63SAdrian Chadd 	if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
4951bacf3beSMarius Strobl 		clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) &
4961bacf3beSMarius Strobl 		    BCM577XX_CTRL_CLKSEL_MASK;
49793efdc63SAdrian Chadd 
4981bacf3beSMarius Strobl 		/*
4991bacf3beSMarius Strobl 		 * Select clock source appropriate for the requested frequency.
5001bacf3beSMarius Strobl 		 */
50193efdc63SAdrian Chadd 		if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
50293efdc63SAdrian Chadd 			clk_base = BCM577XX_ALT_CLOCK_BASE;
5031bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ <<
5041bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
50593efdc63SAdrian Chadd 		} else {
5061bacf3beSMarius Strobl 			clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT <<
5071bacf3beSMarius Strobl 			    BCM577XX_CTRL_CLKSEL_SHIFT);
50893efdc63SAdrian Chadd 		}
50993efdc63SAdrian Chadd 
51093efdc63SAdrian Chadd 		WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
51193efdc63SAdrian Chadd 	}
51293efdc63SAdrian Chadd 
513ceb9e9f7SIan Lepore 	/* Recalculate timeout clock frequency based on the new sd clock. */
514ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
515ceb9e9f7SIan Lepore 		slot->timeout_clk = slot->clock / 1000;
516ceb9e9f7SIan Lepore 
5178f3b7d56SOleksandr Tymoshenko 	if (slot->version < SDHCI_SPEC_300) {
518831f5dcfSAlexander Motin 		/* Looking for highest freq <= clock. */
51993efdc63SAdrian Chadd 		res = clk_base;
52057677a3aSOleksandr Tymoshenko 		for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
521831f5dcfSAlexander Motin 			if (res <= clock)
522831f5dcfSAlexander Motin 				break;
523831f5dcfSAlexander Motin 			res >>= 1;
524831f5dcfSAlexander Motin 		}
525831f5dcfSAlexander Motin 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
5268f3b7d56SOleksandr Tymoshenko 		div >>= 1;
527c11bbc7dSMarius Strobl 	} else {
5288f3b7d56SOleksandr Tymoshenko 		/* Version 3.0 divisors are multiples of two up to 1023 * 2 */
52993efdc63SAdrian Chadd 		if (clock >= clk_base)
53057677a3aSOleksandr Tymoshenko 			div = 0;
5318f3b7d56SOleksandr Tymoshenko 		else {
53257677a3aSOleksandr Tymoshenko 			for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
53393efdc63SAdrian Chadd 				if ((clk_base / div) <= clock)
5348f3b7d56SOleksandr Tymoshenko 					break;
5358f3b7d56SOleksandr Tymoshenko 			}
5368f3b7d56SOleksandr Tymoshenko 		}
5378f3b7d56SOleksandr Tymoshenko 		div >>= 1;
5388f3b7d56SOleksandr Tymoshenko 	}
5398f3b7d56SOleksandr Tymoshenko 
5408f3b7d56SOleksandr Tymoshenko 	if (bootverbose || sdhci_debug)
54193efdc63SAdrian Chadd 		slot_printf(slot, "Divider %d for freq %d (base %d)\n",
54293efdc63SAdrian Chadd 			div, clock, clk_base);
5438f3b7d56SOleksandr Tymoshenko 
544831f5dcfSAlexander Motin 	/* Now we have got divider, set it. */
5458f3b7d56SOleksandr Tymoshenko 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
5468f3b7d56SOleksandr Tymoshenko 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
5478f3b7d56SOleksandr Tymoshenko 		<< SDHCI_DIVIDER_HI_SHIFT;
5488f3b7d56SOleksandr Tymoshenko 
549831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
550831f5dcfSAlexander Motin 	/* Enable clock. */
551831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_INT_EN;
552831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
553831f5dcfSAlexander Motin 	/* Wait up to 10 ms until it stabilize. */
554831f5dcfSAlexander Motin 	timeout = 10;
555831f5dcfSAlexander Motin 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
556831f5dcfSAlexander Motin 		& SDHCI_CLOCK_INT_STABLE)) {
557831f5dcfSAlexander Motin 		if (timeout == 0) {
558831f5dcfSAlexander Motin 			slot_printf(slot,
559831f5dcfSAlexander Motin 			    "Internal clock never stabilised.\n");
560831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
561831f5dcfSAlexander Motin 			return;
562831f5dcfSAlexander Motin 		}
563831f5dcfSAlexander Motin 		timeout--;
564831f5dcfSAlexander Motin 		DELAY(1000);
565831f5dcfSAlexander Motin 	}
566831f5dcfSAlexander Motin 	/* Pass clock signal to the bus. */
567831f5dcfSAlexander Motin 	clk |= SDHCI_CLOCK_CARD_EN;
568831f5dcfSAlexander Motin 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
569831f5dcfSAlexander Motin }
570831f5dcfSAlexander Motin 
571831f5dcfSAlexander Motin static void
572831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power)
573831f5dcfSAlexander Motin {
57485083a80SMarius Strobl 	int i;
575831f5dcfSAlexander Motin 	uint8_t pwr;
576831f5dcfSAlexander Motin 
577831f5dcfSAlexander Motin 	if (slot->power == power)
578831f5dcfSAlexander Motin 		return;
579d6b3aaf8SOleksandr Tymoshenko 
580831f5dcfSAlexander Motin 	slot->power = power;
581831f5dcfSAlexander Motin 
582831f5dcfSAlexander Motin 	/* Turn off the power. */
583831f5dcfSAlexander Motin 	pwr = 0;
584831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
585b440e965SMarius Strobl 	/* If power down requested - leave it so. */
586831f5dcfSAlexander Motin 	if (power == 0)
587831f5dcfSAlexander Motin 		return;
588831f5dcfSAlexander Motin 	/* Set voltage. */
589831f5dcfSAlexander Motin 	switch (1 << power) {
590831f5dcfSAlexander Motin 	case MMC_OCR_LOW_VOLTAGE:
591831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_180;
592831f5dcfSAlexander Motin 		break;
593831f5dcfSAlexander Motin 	case MMC_OCR_290_300:
594831f5dcfSAlexander Motin 	case MMC_OCR_300_310:
595831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_300;
596831f5dcfSAlexander Motin 		break;
597831f5dcfSAlexander Motin 	case MMC_OCR_320_330:
598831f5dcfSAlexander Motin 	case MMC_OCR_330_340:
599831f5dcfSAlexander Motin 		pwr |= SDHCI_POWER_330;
600831f5dcfSAlexander Motin 		break;
601831f5dcfSAlexander Motin 	}
602831f5dcfSAlexander Motin 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
60385083a80SMarius Strobl 	/*
60485083a80SMarius Strobl 	 * Turn on VDD1 power.  Note that at least some Intel controllers can
60585083a80SMarius Strobl 	 * fail to enable bus power on the first try after transiting from D3
6068022c8ebSMarius Strobl 	 * to D0, so we give them up to 2 ms.
60785083a80SMarius Strobl 	 */
608831f5dcfSAlexander Motin 	pwr |= SDHCI_POWER_ON;
60985083a80SMarius Strobl 	for (i = 0; i < 20; i++) {
610831f5dcfSAlexander Motin 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
61185083a80SMarius Strobl 		if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)
61285083a80SMarius Strobl 			break;
61385083a80SMarius Strobl 		DELAY(100);
61485083a80SMarius Strobl 	}
61585083a80SMarius Strobl 	if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON))
6164d52f81dSIan Lepore 		slot_printf(slot, "Bus power failed to enable\n");
617a2832f9fSMarius Strobl 
618a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) {
619a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10);
620a2832f9fSMarius Strobl 		DELAY(10);
621a2832f9fSMarius Strobl 		WR1(slot, SDHCI_POWER_CONTROL, pwr);
622a2832f9fSMarius Strobl 		DELAY(300);
623a2832f9fSMarius Strobl 	}
624831f5dcfSAlexander Motin }
625831f5dcfSAlexander Motin 
626831f5dcfSAlexander Motin static void
627831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot)
628831f5dcfSAlexander Motin {
629831f5dcfSAlexander Motin 	uint32_t data;
630831f5dcfSAlexander Motin 	char *buffer;
631831f5dcfSAlexander Motin 	size_t left;
632831f5dcfSAlexander Motin 
633831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
634831f5dcfSAlexander Motin 	buffer += slot->offset;
635831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
6365d5ae066SIlya Bakulin #ifdef MMCCAM
6375d5ae066SIlya Bakulin 	if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE)
6385d5ae066SIlya Bakulin 		left = min(slot->curcmd->data->block_size,
6395d5ae066SIlya Bakulin 		    slot->curcmd->data->len - slot->offset);
6405d5ae066SIlya Bakulin 	else
6415d5ae066SIlya Bakulin #endif
642831f5dcfSAlexander Motin 		left = min(512, slot->curcmd->data->len - slot->offset);
643831f5dcfSAlexander Motin 	slot->offset += left;
644831f5dcfSAlexander Motin 
645831f5dcfSAlexander Motin 	/* If we are too fast, broken controllers return zeroes. */
646d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
647831f5dcfSAlexander Motin 		DELAY(10);
648ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
649831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
650831f5dcfSAlexander Motin 		while (left > 3) {
651831f5dcfSAlexander Motin 			data = RD4(slot, SDHCI_BUFFER);
652831f5dcfSAlexander Motin 			buffer[0] = data;
653831f5dcfSAlexander Motin 			buffer[1] = (data >> 8);
654831f5dcfSAlexander Motin 			buffer[2] = (data >> 16);
655831f5dcfSAlexander Motin 			buffer[3] = (data >> 24);
656831f5dcfSAlexander Motin 			buffer += 4;
657831f5dcfSAlexander Motin 			left -= 4;
658831f5dcfSAlexander Motin 		}
659831f5dcfSAlexander Motin 	} else {
660d6b3aaf8SOleksandr Tymoshenko 		RD_MULTI_4(slot, SDHCI_BUFFER,
661831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
662831f5dcfSAlexander Motin 		left &= 3;
663831f5dcfSAlexander Motin 	}
664831f5dcfSAlexander Motin 	/* Handle uneven size case. */
665831f5dcfSAlexander Motin 	if (left > 0) {
666831f5dcfSAlexander Motin 		data = RD4(slot, SDHCI_BUFFER);
667831f5dcfSAlexander Motin 		while (left > 0) {
668831f5dcfSAlexander Motin 			*(buffer++) = data;
669831f5dcfSAlexander Motin 			data >>= 8;
670831f5dcfSAlexander Motin 			left--;
671831f5dcfSAlexander Motin 		}
672831f5dcfSAlexander Motin 	}
673831f5dcfSAlexander Motin }
674831f5dcfSAlexander Motin 
675831f5dcfSAlexander Motin static void
676831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot)
677831f5dcfSAlexander Motin {
678831f5dcfSAlexander Motin 	uint32_t data = 0;
679831f5dcfSAlexander Motin 	char *buffer;
680831f5dcfSAlexander Motin 	size_t left;
681831f5dcfSAlexander Motin 
682831f5dcfSAlexander Motin 	buffer = slot->curcmd->data->data;
683831f5dcfSAlexander Motin 	buffer += slot->offset;
684831f5dcfSAlexander Motin 	/* Transfer one block at a time. */
6855d5ae066SIlya Bakulin #ifdef MMCCAM
6865d5ae066SIlya Bakulin 	if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) {
6875d5ae066SIlya Bakulin 		left = min(slot->curcmd->data->block_size,
6885d5ae066SIlya Bakulin 		    slot->curcmd->data->len - slot->offset);
6895d5ae066SIlya Bakulin 	} else
6905d5ae066SIlya Bakulin #endif
691831f5dcfSAlexander Motin 		left = min(512, slot->curcmd->data->len - slot->offset);
692831f5dcfSAlexander Motin 	slot->offset += left;
693831f5dcfSAlexander Motin 
694ecc2d997SRui Paulo 	/* Handle unaligned and aligned buffer cases. */
695831f5dcfSAlexander Motin 	if ((intptr_t)buffer & 3) {
696831f5dcfSAlexander Motin 		while (left > 3) {
697831f5dcfSAlexander Motin 			data = buffer[0] +
698831f5dcfSAlexander Motin 			    (buffer[1] << 8) +
699831f5dcfSAlexander Motin 			    (buffer[2] << 16) +
700831f5dcfSAlexander Motin 			    (buffer[3] << 24);
701831f5dcfSAlexander Motin 			left -= 4;
702831f5dcfSAlexander Motin 			buffer += 4;
703831f5dcfSAlexander Motin 			WR4(slot, SDHCI_BUFFER, data);
704831f5dcfSAlexander Motin 		}
705831f5dcfSAlexander Motin 	} else {
706d6b3aaf8SOleksandr Tymoshenko 		WR_MULTI_4(slot, SDHCI_BUFFER,
707831f5dcfSAlexander Motin 		    (uint32_t *)buffer, left >> 2);
708831f5dcfSAlexander Motin 		left &= 3;
709831f5dcfSAlexander Motin 	}
710831f5dcfSAlexander Motin 	/* Handle uneven size case. */
711831f5dcfSAlexander Motin 	if (left > 0) {
712831f5dcfSAlexander Motin 		while (left > 0) {
713831f5dcfSAlexander Motin 			data <<= 8;
714831f5dcfSAlexander Motin 			data += *(buffer++);
715831f5dcfSAlexander Motin 			left--;
716831f5dcfSAlexander Motin 		}
717831f5dcfSAlexander Motin 		WR4(slot, SDHCI_BUFFER, data);
718831f5dcfSAlexander Motin 	}
719831f5dcfSAlexander Motin }
720831f5dcfSAlexander Motin 
721831f5dcfSAlexander Motin static void
722831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot)
723831f5dcfSAlexander Motin {
724831f5dcfSAlexander Motin 
725831f5dcfSAlexander Motin 	/* Read as many blocks as possible. */
726831f5dcfSAlexander Motin 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
727831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
728831f5dcfSAlexander Motin 		    SDHCI_DATA_AVAILABLE) {
729831f5dcfSAlexander Motin 			sdhci_read_block_pio(slot);
730831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
731831f5dcfSAlexander Motin 				break;
732831f5dcfSAlexander Motin 		}
733831f5dcfSAlexander Motin 	} else {
734831f5dcfSAlexander Motin 		while (RD4(slot, SDHCI_PRESENT_STATE) &
735831f5dcfSAlexander Motin 		    SDHCI_SPACE_AVAILABLE) {
736831f5dcfSAlexander Motin 			sdhci_write_block_pio(slot);
737831f5dcfSAlexander Motin 			if (slot->offset >= slot->curcmd->data->len)
738831f5dcfSAlexander Motin 				break;
739831f5dcfSAlexander Motin 		}
740831f5dcfSAlexander Motin 	}
741831f5dcfSAlexander Motin }
742831f5dcfSAlexander Motin 
743831f5dcfSAlexander Motin static void
7447e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused)
745831f5dcfSAlexander Motin {
746831f5dcfSAlexander Motin 	struct sdhci_slot *slot = arg;
7477e6ccea3SMarius Strobl 	device_t d;
748831f5dcfSAlexander Motin 
749831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
7506e37fb2bSIan Lepore 	if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
751a94a63f0SWarner Losh #ifdef MMCCAM
752a94a63f0SWarner Losh 		if (slot->card_present == 0) {
753a94a63f0SWarner Losh #else
754831f5dcfSAlexander Motin 		if (slot->dev == NULL) {
755a94a63f0SWarner Losh #endif
756831f5dcfSAlexander Motin 			/* If card is present - attach mmc bus. */
757639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
758639f59f0SIan Lepore 				slot_printf(slot, "Card inserted\n");
759a94a63f0SWarner Losh #ifdef MMCCAM
760a94a63f0SWarner Losh 			slot->card_present = 1;
761c7a49948SEmmanuel Vadot 			mmccam_start_discovery(slot->sim);
762a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
763a94a63f0SWarner Losh #else
764aca38eabSMarius Strobl 			d = slot->dev = device_add_child(slot->bus, "mmc", -1);
765831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
766aca38eabSMarius Strobl 			if (d) {
767aca38eabSMarius Strobl 				device_set_ivars(d, slot);
768aca38eabSMarius Strobl 				(void)device_probe_and_attach(d);
769aca38eabSMarius Strobl 			}
770a94a63f0SWarner Losh #endif
771831f5dcfSAlexander Motin 		} else
772831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
773831f5dcfSAlexander Motin 	} else {
774a94a63f0SWarner Losh #ifdef MMCCAM
775a94a63f0SWarner Losh 		if (slot->card_present == 1) {
776a94a63f0SWarner Losh #else
777831f5dcfSAlexander Motin 		if (slot->dev != NULL) {
778a94a63f0SWarner Losh #endif
779831f5dcfSAlexander Motin 			/* If no card present - detach mmc bus. */
780639f59f0SIan Lepore 			if (bootverbose || sdhci_debug)
781639f59f0SIan Lepore 				slot_printf(slot, "Card removed\n");
7827e6ccea3SMarius Strobl 			d = slot->dev;
783831f5dcfSAlexander Motin 			slot->dev = NULL;
784a94a63f0SWarner Losh #ifdef MMCCAM
785a94a63f0SWarner Losh 			slot->card_present = 0;
786c7a49948SEmmanuel Vadot 			mmccam_start_discovery(slot->sim);
787a94a63f0SWarner Losh 			SDHCI_UNLOCK(slot);
788a94a63f0SWarner Losh #else
789aca38eabSMarius Strobl 			slot->intmask &= ~sdhci_tuning_intmask(slot);
790cc22204bSMarius Strobl 			WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
791aca38eabSMarius Strobl 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
792aca38eabSMarius Strobl 			slot->opt &= ~SDHCI_TUNING_ENABLED;
793831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
794aca38eabSMarius Strobl 			callout_drain(&slot->retune_callout);
795d6b3aaf8SOleksandr Tymoshenko 			device_delete_child(slot->bus, d);
796a94a63f0SWarner Losh #endif
797831f5dcfSAlexander Motin 		} else
798831f5dcfSAlexander Motin 			SDHCI_UNLOCK(slot);
799831f5dcfSAlexander Motin 	}
800831f5dcfSAlexander Motin }
801831f5dcfSAlexander Motin 
802b8bf08b1SIan Lepore static void
803b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
804639f59f0SIan Lepore {
805639f59f0SIan Lepore 	bool was_present;
806639f59f0SIan Lepore 
807639f59f0SIan Lepore 	/*
808639f59f0SIan Lepore 	 * If there was no card and now there is one, schedule the task to
809639f59f0SIan Lepore 	 * create the child device after a short delay.  The delay is to
810639f59f0SIan Lepore 	 * debounce the card insert (sometimes the card detect pin stabilizes
811639f59f0SIan Lepore 	 * before the other pins have made good contact).
812639f59f0SIan Lepore 	 *
813639f59f0SIan Lepore 	 * If there was a card present and now it's gone, immediately schedule
814639f59f0SIan Lepore 	 * the task to delete the child device.  No debouncing -- gone is gone,
815639f59f0SIan Lepore 	 * because once power is removed, a full card re-init is needed, and
816639f59f0SIan Lepore 	 * that happens by deleting and recreating the child device.
817639f59f0SIan Lepore 	 */
818a94a63f0SWarner Losh #ifdef MMCCAM
819a94a63f0SWarner Losh 	was_present = slot->card_present;
820a94a63f0SWarner Losh #else
821639f59f0SIan Lepore 	was_present = slot->dev != NULL;
822a94a63f0SWarner Losh #endif
823639f59f0SIan Lepore 	if (!was_present && is_present) {
824639f59f0SIan Lepore 		taskqueue_enqueue_timeout(taskqueue_swi_giant,
825639f59f0SIan Lepore 		    &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
826639f59f0SIan Lepore 	} else if (was_present && !is_present) {
827639f59f0SIan Lepore 		taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
828639f59f0SIan Lepore 	}
829b8bf08b1SIan Lepore }
830b8bf08b1SIan Lepore 
831b8bf08b1SIan Lepore void
832b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
833b8bf08b1SIan Lepore {
834b8bf08b1SIan Lepore 
835b8bf08b1SIan Lepore 	SDHCI_LOCK(slot);
836b8bf08b1SIan Lepore 	sdhci_handle_card_present_locked(slot, is_present);
837639f59f0SIan Lepore 	SDHCI_UNLOCK(slot);
838639f59f0SIan Lepore }
839639f59f0SIan Lepore 
840639f59f0SIan Lepore static void
841639f59f0SIan Lepore sdhci_card_poll(void *arg)
842639f59f0SIan Lepore {
843639f59f0SIan Lepore 	struct sdhci_slot *slot = arg;
844639f59f0SIan Lepore 
845639f59f0SIan Lepore 	sdhci_handle_card_present(slot,
846639f59f0SIan Lepore 	    SDHCI_GET_CARD_PRESENT(slot->bus, slot));
847639f59f0SIan Lepore 	callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
848639f59f0SIan Lepore 	    sdhci_card_poll, slot);
849639f59f0SIan Lepore }
850639f59f0SIan Lepore 
851ab00a509SMarius Strobl static int
8527d8700bcSMarcin Wojtas sdhci_dma_alloc(struct sdhci_slot *slot, uint32_t caps)
853ab00a509SMarius Strobl {
854ab00a509SMarius Strobl 	int err;
855ab00a509SMarius Strobl 
856ab00a509SMarius Strobl 	if (!(slot->quirks & SDHCI_QUIRK_BROKEN_SDMA_BOUNDARY)) {
857cd853791SKonstantin Belousov 		if (maxphys <= 1024 * 4)
858ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_4K;
859cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 8)
860ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_8K;
861cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 16)
862ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_16K;
863cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 32)
864ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_32K;
865cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 64)
866ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_64K;
867cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 128)
868ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_128K;
869cd853791SKonstantin Belousov 		else if (maxphys <= 1024 * 256)
870ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_256K;
871ab00a509SMarius Strobl 		else
872ab00a509SMarius Strobl 			slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_512K;
873ab00a509SMarius Strobl 	}
874ab00a509SMarius Strobl 	slot->sdma_bbufsz = SDHCI_SDMA_BNDRY_TO_BBUFSZ(slot->sdma_boundary);
875ab00a509SMarius Strobl 
876ab00a509SMarius Strobl 	/*
877ab00a509SMarius Strobl 	 * Allocate the DMA tag for an SDMA bounce buffer.
878ab00a509SMarius Strobl 	 * Note that the SDHCI specification doesn't state any alignment
879ab00a509SMarius Strobl 	 * constraint for the SDMA system address.  However, controllers
880ab00a509SMarius Strobl 	 * typically ignore the SDMA boundary bits in SDHCI_DMA_ADDRESS when
881ab00a509SMarius Strobl 	 * forming the actual address of data, requiring the SDMA buffer to
882ab00a509SMarius Strobl 	 * be aligned to the SDMA boundary.
883ab00a509SMarius Strobl 	 */
884ab00a509SMarius Strobl 	err = bus_dma_tag_create(bus_get_dma_tag(slot->bus), slot->sdma_bbufsz,
8857d8700bcSMarcin Wojtas 	    0, (caps & SDHCI_CAN_DO_64BIT) ? BUS_SPACE_MAXADDR :
8867d8700bcSMarcin Wojtas 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
887ab00a509SMarius Strobl 	    slot->sdma_bbufsz, 1, slot->sdma_bbufsz, BUS_DMA_ALLOCNOW,
888ab00a509SMarius Strobl 	    NULL, NULL, &slot->dmatag);
889ab00a509SMarius Strobl 	if (err != 0) {
890ab00a509SMarius Strobl 		slot_printf(slot, "Can't create DMA tag for SDMA\n");
891ab00a509SMarius Strobl 		return (err);
892ab00a509SMarius Strobl 	}
893ab00a509SMarius Strobl 	/* Allocate DMA memory for the SDMA bounce buffer. */
894ab00a509SMarius Strobl 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
895ab00a509SMarius Strobl 	    BUS_DMA_NOWAIT, &slot->dmamap);
896ab00a509SMarius Strobl 	if (err != 0) {
897ab00a509SMarius Strobl 		slot_printf(slot, "Can't alloc DMA memory for SDMA\n");
898ab00a509SMarius Strobl 		bus_dma_tag_destroy(slot->dmatag);
899ab00a509SMarius Strobl 		return (err);
900ab00a509SMarius Strobl 	}
901ab00a509SMarius Strobl 	/* Map the memory of the SDMA bounce buffer. */
902ab00a509SMarius Strobl 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
903ab00a509SMarius Strobl 	    (void *)slot->dmamem, slot->sdma_bbufsz, sdhci_getaddr,
904ab00a509SMarius Strobl 	    &slot->paddr, 0);
905ab00a509SMarius Strobl 	if (err != 0 || slot->paddr == 0) {
906ab00a509SMarius Strobl 		slot_printf(slot, "Can't load DMA memory for SDMA\n");
907ab00a509SMarius Strobl 		bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
908ab00a509SMarius Strobl 		bus_dma_tag_destroy(slot->dmatag);
909ab00a509SMarius Strobl 		if (err)
910ab00a509SMarius Strobl 			return (err);
911ab00a509SMarius Strobl 		else
912ab00a509SMarius Strobl 			return (EFAULT);
913ab00a509SMarius Strobl 	}
914ab00a509SMarius Strobl 
915ab00a509SMarius Strobl 	return (0);
916ab00a509SMarius Strobl }
917ab00a509SMarius Strobl 
918ab00a509SMarius Strobl static void
919ab00a509SMarius Strobl sdhci_dma_free(struct sdhci_slot *slot)
920ab00a509SMarius Strobl {
921ab00a509SMarius Strobl 
922ab00a509SMarius Strobl 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
923ab00a509SMarius Strobl 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
924ab00a509SMarius Strobl 	bus_dma_tag_destroy(slot->dmatag);
925ab00a509SMarius Strobl }
926ab00a509SMarius Strobl 
927d6b3aaf8SOleksandr Tymoshenko int
928d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
929831f5dcfSAlexander Motin {
930aca38eabSMarius Strobl 	kobjop_desc_t kobj_desc;
931aca38eabSMarius Strobl 	kobj_method_t *kobj_method;
9320f34084fSMarius Strobl 	uint32_t caps, caps2, freq, host_caps;
933d6b3aaf8SOleksandr Tymoshenko 	int err;
934d00c1f7fSBartlomiej Grzesik 	char node_name[8];
935d00c1f7fSBartlomiej Grzesik 	struct sysctl_oid *node_oid;
936831f5dcfSAlexander Motin 
937831f5dcfSAlexander Motin 	SDHCI_LOCK_INIT(slot);
938a94a63f0SWarner Losh 
939d6b3aaf8SOleksandr Tymoshenko 	slot->num = num;
940d6b3aaf8SOleksandr Tymoshenko 	slot->bus = dev;
941d6b3aaf8SOleksandr Tymoshenko 
942d6b3aaf8SOleksandr Tymoshenko 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
943d6b3aaf8SOleksandr Tymoshenko 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
9440f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) {
9458f3b7d56SOleksandr Tymoshenko 		caps = slot->caps;
9460f34084fSMarius Strobl 		caps2 = slot->caps2;
9470f34084fSMarius Strobl 	} else {
948831f5dcfSAlexander Motin 		caps = RD4(slot, SDHCI_CAPABILITIES);
9490f34084fSMarius Strobl 		if (slot->version >= SDHCI_SPEC_300)
9500f34084fSMarius Strobl 			caps2 = RD4(slot, SDHCI_CAPABILITIES2);
9510f34084fSMarius Strobl 		else
9520f34084fSMarius Strobl 			caps2 = 0;
9530f34084fSMarius Strobl 	}
9547fcf4780SMarius Strobl 	if (slot->version >= SDHCI_SPEC_300) {
9557fcf4780SMarius Strobl 		if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE &&
9567fcf4780SMarius Strobl 		    (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) {
957ab00a509SMarius Strobl 			slot_printf(slot,
9587fcf4780SMarius Strobl 			    "Driver doesn't support shared bus slots\n");
9597fcf4780SMarius Strobl 			SDHCI_LOCK_DESTROY(slot);
9607fcf4780SMarius Strobl 			return (ENXIO);
9617fcf4780SMarius Strobl 		} else if ((caps & SDHCI_SLOTTYPE_MASK) ==
9627fcf4780SMarius Strobl 		    SDHCI_SLOTTYPE_EMBEDDED) {
9637fcf4780SMarius Strobl 			slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE;
9647fcf4780SMarius Strobl 		}
9657fcf4780SMarius Strobl 	}
966831f5dcfSAlexander Motin 	/* Calculate base clock frequency. */
96733aad34dSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
96887a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
96987a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
97033aad34dSOleksandr Tymoshenko 	else
97187a6a871SIan Lepore 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
97287a6a871SIan Lepore 		    SDHCI_CLOCK_BASE_SHIFT;
97387a6a871SIan Lepore 	if (freq != 0)
97487a6a871SIan Lepore 		slot->max_clk = freq * 1000000;
97587a6a871SIan Lepore 	/*
97687a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
97787a6a871SIan Lepore 	 * hasn't already set max_clk we're probably not going to work right
97887a6a871SIan Lepore 	 * with an assumption, so complain about it.
97987a6a871SIan Lepore 	 */
980831f5dcfSAlexander Motin 	if (slot->max_clk == 0) {
98187a6a871SIan Lepore 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
982ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't specify base clock "
9831bacf3beSMarius Strobl 		    "frequency, using %dMHz as default.\n",
9841bacf3beSMarius Strobl 		    SDHCI_DEFAULT_MAX_FREQ);
985831f5dcfSAlexander Motin 	}
986a2832f9fSMarius Strobl 	/* Calculate/set timeout clock frequency. */
9878f3b7d56SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
9888f3b7d56SOleksandr Tymoshenko 		slot->timeout_clk = slot->max_clk / 1000;
989a2832f9fSMarius Strobl 	} else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) {
990a2832f9fSMarius Strobl 		slot->timeout_clk = 1000;
9918f3b7d56SOleksandr Tymoshenko 	} else {
9921bacf3beSMarius Strobl 		slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >>
9931bacf3beSMarius Strobl 		    SDHCI_TIMEOUT_CLK_SHIFT;
9948f3b7d56SOleksandr Tymoshenko 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
9958f3b7d56SOleksandr Tymoshenko 			slot->timeout_clk *= 1000;
9968f3b7d56SOleksandr Tymoshenko 	}
99787a6a871SIan Lepore 	/*
99887a6a871SIan Lepore 	 * If the frequency wasn't in the capabilities and the hardware driver
99987a6a871SIan Lepore 	 * hasn't already set timeout_clk we'll probably work okay using the
100087a6a871SIan Lepore 	 * max timeout, but still mention it.
100187a6a871SIan Lepore 	 */
1002831f5dcfSAlexander Motin 	if (slot->timeout_clk == 0) {
1003ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't specify timeout clock "
1004ceb9e9f7SIan Lepore 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
1005ceb9e9f7SIan Lepore 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
1006831f5dcfSAlexander Motin 	}
1007831f5dcfSAlexander Motin 
100857677a3aSOleksandr Tymoshenko 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
1009831f5dcfSAlexander Motin 	slot->host.f_max = slot->max_clk;
1010831f5dcfSAlexander Motin 	slot->host.host_ocr = 0;
1011831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_330)
1012831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
1013831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_VDD_300)
1014831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
101549dfdf63SIan Lepore 	/*
101649dfdf63SIan Lepore 	 * 1.8V VDD is not supposed to be used for removable cards.  Hardware
101749dfdf63SIan Lepore 	 * prior to v3.0 had no way to indicate embedded slots, but did
101849dfdf63SIan Lepore 	 * sometimes support 1.8v for non-removable devices.
101949dfdf63SIan Lepore 	 */
102049dfdf63SIan Lepore 	if ((caps & SDHCI_CAN_VDD_180) && (slot->version < SDHCI_SPEC_300 ||
102149dfdf63SIan Lepore 	    (slot->opt & SDHCI_SLOT_EMBEDDED)))
1022831f5dcfSAlexander Motin 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
1023831f5dcfSAlexander Motin 	if (slot->host.host_ocr == 0) {
1024ab00a509SMarius Strobl 		slot_printf(slot, "Hardware doesn't report any "
1025831f5dcfSAlexander Motin 		    "support voltages.\n");
1026831f5dcfSAlexander Motin 	}
1027aca38eabSMarius Strobl 
10285652be30SMarcin Wojtas 	host_caps = slot->host.caps;
10295652be30SMarcin Wojtas 	host_caps |= MMC_CAP_4_BIT_DATA;
10302d1731b8SIan Lepore 	if (caps & SDHCI_CAN_DO_8BITBUS)
10310f34084fSMarius Strobl 		host_caps |= MMC_CAP_8_BIT_DATA;
1032831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_HISPD)
10330f34084fSMarius Strobl 		host_caps |= MMC_CAP_HSPEED;
103472dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC)
10350f34084fSMarius Strobl 		host_caps |= MMC_CAP_BOOT_NOACC;
103672dec079SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY)
10370f34084fSMarius Strobl 		host_caps |= MMC_CAP_WAIT_WHILE_BUSY;
1038aca38eabSMarius Strobl 
1039aca38eabSMarius Strobl 	/* Determine supported UHS-I and eMMC modes. */
10400f34084fSMarius Strobl 	if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50))
10410f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
10420f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_SDR104) {
10430f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
10440f34084fSMarius Strobl 		if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200))
10450f34084fSMarius Strobl 			host_caps |= MMC_CAP_MMC_HS200;
10460f34084fSMarius Strobl 	} else if (caps2 & SDHCI_CAN_SDR50)
10470f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_SDR50;
10480f34084fSMarius Strobl 	if (caps2 & SDHCI_CAN_DDR50 &&
10490f34084fSMarius Strobl 	    !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50))
10500f34084fSMarius Strobl 		host_caps |= MMC_CAP_UHS_DDR50;
10510f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_DDR52)
10520f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_DDR52;
10530f34084fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 &&
10540f34084fSMarius Strobl 	    caps2 & SDHCI_CAN_MMC_HS400)
10550f34084fSMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
1056835998c2SMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 &&
1057835998c2SMarius Strobl 	    caps2 & SDHCI_CAN_SDR104)
1058835998c2SMarius Strobl 		host_caps |= MMC_CAP_MMC_HS400;
1059aca38eabSMarius Strobl 
1060aca38eabSMarius Strobl 	/*
1061aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes if the set_uhs_timing method is the
1062aca38eabSMarius Strobl 	 * default NULL implementation.
1063aca38eabSMarius Strobl 	 */
1064aca38eabSMarius Strobl 	kobj_desc = &sdhci_set_uhs_timing_desc;
1065aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
1066aca38eabSMarius Strobl 	    kobj_desc);
1067aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
1068aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
1069aca38eabSMarius Strobl 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
1070aca38eabSMarius Strobl 		    MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400);
1071aca38eabSMarius Strobl 
1072aca38eabSMarius Strobl #define	SDHCI_CAP_MODES_TUNING(caps2)					\
1073aca38eabSMarius Strobl     (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) |		\
1074aca38eabSMarius Strobl     MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 |	\
1075aca38eabSMarius Strobl     MMC_CAP_MMC_HS400)
1076aca38eabSMarius Strobl 
1077aca38eabSMarius Strobl 	/*
1078aca38eabSMarius Strobl 	 * Disable UHS-I and eMMC modes that require (re-)tuning if either
1079aca38eabSMarius Strobl 	 * the tune or re-tune method is the default NULL implementation.
1080aca38eabSMarius Strobl 	 */
1081aca38eabSMarius Strobl 	kobj_desc = &mmcbr_tune_desc;
1082aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
1083aca38eabSMarius Strobl 	    kobj_desc);
1084aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
1085aca38eabSMarius Strobl 		goto no_tuning;
1086aca38eabSMarius Strobl 	kobj_desc = &mmcbr_retune_desc;
1087aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
1088aca38eabSMarius Strobl 	    kobj_desc);
1089aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt) {
1090aca38eabSMarius Strobl no_tuning:
1091aca38eabSMarius Strobl 		host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2));
1092aca38eabSMarius Strobl 	}
1093aca38eabSMarius Strobl 
1094aca38eabSMarius Strobl 	/* Allocate tuning structures and determine tuning parameters. */
1095aca38eabSMarius Strobl 	if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) {
1096aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_SUPPORTED;
1097aca38eabSMarius Strobl 		slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF,
1098aca38eabSMarius Strobl 		    M_WAITOK);
1099aca38eabSMarius Strobl 		slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF,
1100aca38eabSMarius Strobl 		    M_WAITOK);
1101aca38eabSMarius Strobl 		slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF,
1102aca38eabSMarius Strobl 		    M_WAITOK);
1103aca38eabSMarius Strobl 		if (caps2 & SDHCI_TUNE_SDR50)
1104aca38eabSMarius Strobl 			slot->opt |= SDHCI_SDR50_NEEDS_TUNING;
1105aca38eabSMarius Strobl 		slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >>
1106aca38eabSMarius Strobl 		    SDHCI_RETUNE_MODES_SHIFT;
1107aca38eabSMarius Strobl 		if (slot->retune_mode == SDHCI_RETUNE_MODE_1) {
1108aca38eabSMarius Strobl 			slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >>
1109aca38eabSMarius Strobl 			    SDHCI_RETUNE_CNT_SHIFT;
1110aca38eabSMarius Strobl 			if (slot->retune_count > 0xb) {
1111ab00a509SMarius Strobl 				slot_printf(slot, "Unknown re-tuning count "
1112aca38eabSMarius Strobl 				    "%x, using 1 sec\n", slot->retune_count);
1113aca38eabSMarius Strobl 				slot->retune_count = 1;
1114aca38eabSMarius Strobl 			} else if (slot->retune_count != 0)
1115aca38eabSMarius Strobl 				slot->retune_count =
1116aca38eabSMarius Strobl 				    1 << (slot->retune_count - 1);
1117aca38eabSMarius Strobl 		}
1118aca38eabSMarius Strobl 	}
1119aca38eabSMarius Strobl 
1120aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING
1121aca38eabSMarius Strobl 
1122aca38eabSMarius Strobl 	/* Determine supported VCCQ signaling levels. */
11230f34084fSMarius Strobl 	host_caps |= MMC_CAP_SIGNALING_330;
11240f34084fSMarius Strobl 	if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
1125aca38eabSMarius Strobl 	    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
11260f34084fSMarius Strobl 	    MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 |
11270f34084fSMarius Strobl 	    MMC_CAP_MMC_HS400_180))
1128aca38eabSMarius Strobl 		host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180;
1129aca38eabSMarius Strobl 
1130aca38eabSMarius Strobl 	/*
1131aca38eabSMarius Strobl 	 * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the
1132aca38eabSMarius Strobl 	 * default NULL implementation.  Disable 1.2 V support if it's the
1133aca38eabSMarius Strobl 	 * generic SDHCI implementation.
1134aca38eabSMarius Strobl 	 */
1135aca38eabSMarius Strobl 	kobj_desc = &mmcbr_switch_vccq_desc;
1136aca38eabSMarius Strobl 	kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
1137aca38eabSMarius Strobl 	    kobj_desc);
1138aca38eabSMarius Strobl 	if (kobj_method == &kobj_desc->deflt)
1139aca38eabSMarius Strobl 		host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180);
1140aca38eabSMarius Strobl 	else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq)
1141aca38eabSMarius Strobl 		host_caps &= ~MMC_CAP_SIGNALING_120;
1142aca38eabSMarius Strobl 
1143aca38eabSMarius Strobl 	/* Determine supported driver types (type B is always mandatory). */
1144f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_A)
11450f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_A;
1146f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_C)
11470f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_C;
1148f8b883c1SImre Vadász 	if (caps2 & SDHCI_CAN_DRIVE_TYPE_D)
11490f34084fSMarius Strobl 		host_caps |= MMC_CAP_DRIVER_TYPE_D;
11500f34084fSMarius Strobl 	slot->host.caps = host_caps;
11510f34084fSMarius Strobl 
1152831f5dcfSAlexander Motin 	/* Decide if we have usable DMA. */
1153831f5dcfSAlexander Motin 	if (caps & SDHCI_CAN_DO_DMA)
1154831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
1155d6b3aaf8SOleksandr Tymoshenko 
1156d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
1157831f5dcfSAlexander Motin 		slot->opt &= ~SDHCI_HAVE_DMA;
1158d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
1159831f5dcfSAlexander Motin 		slot->opt |= SDHCI_HAVE_DMA;
1160a2832f9fSMarius Strobl 	if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE)
1161a2832f9fSMarius Strobl 		slot->opt |= SDHCI_NON_REMOVABLE;
1162831f5dcfSAlexander Motin 
1163c3a0f75aSOleksandr Tymoshenko 	/*
1164c3a0f75aSOleksandr Tymoshenko 	 * Use platform-provided transfer backend
1165c3a0f75aSOleksandr Tymoshenko 	 * with PIO as a fallback mechanism
1166c3a0f75aSOleksandr Tymoshenko 	 */
1167c3a0f75aSOleksandr Tymoshenko 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
1168c3a0f75aSOleksandr Tymoshenko 		slot->opt &= ~SDHCI_HAVE_DMA;
1169c3a0f75aSOleksandr Tymoshenko 
1170ab00a509SMarius Strobl 	if (slot->opt & SDHCI_HAVE_DMA) {
11717d8700bcSMarcin Wojtas 		err = sdhci_dma_alloc(slot, caps);
1172ab00a509SMarius Strobl 		if (err != 0) {
1173ab00a509SMarius Strobl 			if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1174ab00a509SMarius Strobl 				free(slot->tune_req, M_DEVBUF);
1175ab00a509SMarius Strobl 				free(slot->tune_cmd, M_DEVBUF);
1176ab00a509SMarius Strobl 				free(slot->tune_data, M_DEVBUF);
1177ab00a509SMarius Strobl 			}
1178ab00a509SMarius Strobl 			SDHCI_LOCK_DESTROY(slot);
1179ab00a509SMarius Strobl 			return (err);
1180ab00a509SMarius Strobl 		}
1181ab00a509SMarius Strobl 	}
1182ab00a509SMarius Strobl 
11835b69a497SAlexander Motin 	if (bootverbose || sdhci_debug) {
1184d00c1f7fSBartlomiej Grzesik 		sdhci_dumpcaps(slot);
1185831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
1186831f5dcfSAlexander Motin 	}
1187831f5dcfSAlexander Motin 
1188ba6fc1c7SLuiz Otavio O Souza 	slot->timeout = 10;
1189ba6fc1c7SLuiz Otavio O Souza 	SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
1190ba6fc1c7SLuiz Otavio O Souza 	    SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
11914d52f81dSIan Lepore 	    "timeout", CTLFLAG_RWTUN, &slot->timeout, 0,
1192ba6fc1c7SLuiz Otavio O Souza 	    "Maximum timeout for SDHCI transfers (in secs)");
1193831f5dcfSAlexander Motin 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
1194639f59f0SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
1195639f59f0SIan Lepore 		sdhci_card_task, slot);
1196639f59f0SIan Lepore 	callout_init(&slot->card_poll_callout, 1);
1197e64f01a9SIan Lepore 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
1198aca38eabSMarius Strobl 	callout_init_mtx(&slot->retune_callout, &slot->mtx, 0);
1199ba6fc1c7SLuiz Otavio O Souza 
1200639f59f0SIan Lepore 	if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
1201639f59f0SIan Lepore 	    !(slot->opt & SDHCI_NON_REMOVABLE)) {
1202639f59f0SIan Lepore 		callout_reset(&slot->card_poll_callout,
1203639f59f0SIan Lepore 		    SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
1204639f59f0SIan Lepore 	}
1205639f59f0SIan Lepore 
1206aca38eabSMarius Strobl 	sdhci_init(slot);
1207aca38eabSMarius Strobl 
1208d00c1f7fSBartlomiej Grzesik 	snprintf(node_name, sizeof(node_name), "slot%d", slot->num);
1209d00c1f7fSBartlomiej Grzesik 
1210d00c1f7fSBartlomiej Grzesik 	node_oid = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev),
1211d00c1f7fSBartlomiej Grzesik 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1212d00c1f7fSBartlomiej Grzesik 	    OID_AUTO, node_name, CTLFLAG_RW, 0, "slot specific node");
1213d00c1f7fSBartlomiej Grzesik 
1214d00c1f7fSBartlomiej Grzesik 	node_oid = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev),
1215d00c1f7fSBartlomiej Grzesik 	    SYSCTL_CHILDREN(node_oid), OID_AUTO, "debug", CTLFLAG_RW, 0,
1216d00c1f7fSBartlomiej Grzesik 	    "Debugging node");
1217d00c1f7fSBartlomiej Grzesik 
1218d00c1f7fSBartlomiej Grzesik 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid),
1219d00c1f7fSBartlomiej Grzesik 	    OID_AUTO, "dumpregs", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1220d00c1f7fSBartlomiej Grzesik 	    slot, 0, &sdhci_syctl_dumpregs,
1221d00c1f7fSBartlomiej Grzesik 	    "A", "Dump SDHCI registers");
1222d00c1f7fSBartlomiej Grzesik 
1223d00c1f7fSBartlomiej Grzesik 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid),
1224d00c1f7fSBartlomiej Grzesik 	    OID_AUTO, "dumpcaps", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1225d00c1f7fSBartlomiej Grzesik 	    slot, 0, &sdhci_syctl_dumpcaps,
1226d00c1f7fSBartlomiej Grzesik 	    "A", "Dump SDHCI capabilites");
1227d00c1f7fSBartlomiej Grzesik 
1228831f5dcfSAlexander Motin 	return (0);
1229831f5dcfSAlexander Motin }
1230831f5dcfSAlexander Motin 
1231d91f1a10SIlya Bakulin #ifndef MMCCAM
1232d6b3aaf8SOleksandr Tymoshenko void
1233d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot)
1234831f5dcfSAlexander Motin {
12357e6ccea3SMarius Strobl 
1236d6b3aaf8SOleksandr Tymoshenko 	sdhci_card_task(slot, 0);
1237d6b3aaf8SOleksandr Tymoshenko }
1238d91f1a10SIlya Bakulin #endif
1239831f5dcfSAlexander Motin 
1240d6b3aaf8SOleksandr Tymoshenko int
1241d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot)
1242d6b3aaf8SOleksandr Tymoshenko {
1243831f5dcfSAlexander Motin 	device_t d;
1244831f5dcfSAlexander Motin 
1245e64f01a9SIan Lepore 	callout_drain(&slot->timeout_callout);
1246639f59f0SIan Lepore 	callout_drain(&slot->card_poll_callout);
1247aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1248831f5dcfSAlexander Motin 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
1249639f59f0SIan Lepore 	taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
1250831f5dcfSAlexander Motin 
1251831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1252831f5dcfSAlexander Motin 	d = slot->dev;
1253831f5dcfSAlexander Motin 	slot->dev = NULL;
1254831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1255831f5dcfSAlexander Motin 	if (d != NULL)
1256d6b3aaf8SOleksandr Tymoshenko 		device_delete_child(slot->bus, d);
1257831f5dcfSAlexander Motin 
1258831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1259831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_ALL);
1260831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
1261ab00a509SMarius Strobl 	if (slot->opt & SDHCI_HAVE_DMA)
1262ab00a509SMarius Strobl 		sdhci_dma_free(slot);
1263aca38eabSMarius Strobl 	if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1264aca38eabSMarius Strobl 		free(slot->tune_req, M_DEVBUF);
1265aca38eabSMarius Strobl 		free(slot->tune_cmd, M_DEVBUF);
1266aca38eabSMarius Strobl 		free(slot->tune_data, M_DEVBUF);
1267aca38eabSMarius Strobl 	}
1268d6b3aaf8SOleksandr Tymoshenko 
1269831f5dcfSAlexander Motin 	SDHCI_LOCK_DESTROY(slot);
1270d6b3aaf8SOleksandr Tymoshenko 
1271831f5dcfSAlexander Motin 	return (0);
1272831f5dcfSAlexander Motin }
1273831f5dcfSAlexander Motin 
1274d6b3aaf8SOleksandr Tymoshenko int
1275d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot)
127692bf0e27SAlexander Motin {
12777e6ccea3SMarius Strobl 
1278aca38eabSMarius Strobl 	/*
1279aca38eabSMarius Strobl 	 * We expect the MMC layer to issue initial tuning after resume.
1280aca38eabSMarius Strobl 	 * Otherwise, we'd need to indicate re-tuning including circuit reset
1281aca38eabSMarius Strobl 	 * being required at least for re-tuning modes 1 and 2 ourselves.
1282aca38eabSMarius Strobl 	 */
1283aca38eabSMarius Strobl 	callout_drain(&slot->retune_callout);
1284aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1285aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1286d6b3aaf8SOleksandr Tymoshenko 	sdhci_reset(slot, SDHCI_RESET_ALL);
1287aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
128892bf0e27SAlexander Motin 
128992bf0e27SAlexander Motin 	return (0);
129092bf0e27SAlexander Motin }
129192bf0e27SAlexander Motin 
1292d6b3aaf8SOleksandr Tymoshenko int
1293d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot)
129492bf0e27SAlexander Motin {
12957e6ccea3SMarius Strobl 
1296aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1297d6b3aaf8SOleksandr Tymoshenko 	sdhci_init(slot);
1298aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
129992bf0e27SAlexander Motin 
1300d6b3aaf8SOleksandr Tymoshenko 	return (0);
130192bf0e27SAlexander Motin }
130292bf0e27SAlexander Motin 
130357677a3aSOleksandr Tymoshenko uint32_t
1304b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot)
130557677a3aSOleksandr Tymoshenko {
13067e6ccea3SMarius Strobl 
130757677a3aSOleksandr Tymoshenko 	if (slot->version >= SDHCI_SPEC_300)
130857677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
130957677a3aSOleksandr Tymoshenko 	else
131057677a3aSOleksandr Tymoshenko 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
131157677a3aSOleksandr Tymoshenko }
131257677a3aSOleksandr Tymoshenko 
13136e37fb2bSIan Lepore bool
1314b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot)
13156e37fb2bSIan Lepore {
13166e37fb2bSIan Lepore 
1317639f59f0SIan Lepore 	if (slot->opt & SDHCI_NON_REMOVABLE)
1318639f59f0SIan Lepore 		return true;
1319639f59f0SIan Lepore 
13206e37fb2bSIan Lepore 	return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
13216e37fb2bSIan Lepore }
13226e37fb2bSIan Lepore 
13230f34084fSMarius Strobl void
13240f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot)
13250f34084fSMarius Strobl {
1326ab00a509SMarius Strobl 	const struct mmc_ios *ios;
13270f34084fSMarius Strobl 	uint16_t hostctrl2;
13280f34084fSMarius Strobl 
13290f34084fSMarius Strobl 	if (slot->version < SDHCI_SPEC_300)
13300f34084fSMarius Strobl 		return;
13310f34084fSMarius Strobl 
1332aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
13330f34084fSMarius Strobl 	ios = &slot->host.ios;
13340f34084fSMarius Strobl 	sdhci_set_clock(slot, 0);
13350f34084fSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
13360f34084fSMarius Strobl 	hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK;
1337aca38eabSMarius Strobl 	if (ios->clock > SD_SDR50_MAX) {
13380f34084fSMarius Strobl 		if (ios->timing == bus_timing_mmc_hs400 ||
13390f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_hs400es)
13400f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_MMC_HS400;
1341aca38eabSMarius Strobl 		else
13420f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR104;
1343aca38eabSMarius Strobl 	}
13440f34084fSMarius Strobl 	else if (ios->clock > SD_SDR25_MAX)
13450f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR50;
13460f34084fSMarius Strobl 	else if (ios->clock > SD_SDR12_MAX) {
13470f34084fSMarius Strobl 		if (ios->timing == bus_timing_uhs_ddr50 ||
13480f34084fSMarius Strobl 		    ios->timing == bus_timing_mmc_ddr52)
13490f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_DDR50;
13500f34084fSMarius Strobl 		else
13510f34084fSMarius Strobl 			hostctrl2 |= SDHCI_CTRL2_UHS_SDR25;
13520f34084fSMarius Strobl 	} else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY)
13530f34084fSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_UHS_SDR12;
13540f34084fSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
13550f34084fSMarius Strobl 	sdhci_set_clock(slot, ios->clock);
13560f34084fSMarius Strobl }
13570f34084fSMarius Strobl 
1358d6b3aaf8SOleksandr Tymoshenko int
1359d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev)
1360831f5dcfSAlexander Motin {
1361831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1362831f5dcfSAlexander Motin 	struct mmc_ios *ios = &slot->host.ios;
1363831f5dcfSAlexander Motin 
1364831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
1365831f5dcfSAlexander Motin 	/* Do full reset on bus power down to clear from any state. */
1366831f5dcfSAlexander Motin 	if (ios->power_mode == power_off) {
1367831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
1368831f5dcfSAlexander Motin 		sdhci_init(slot);
1369831f5dcfSAlexander Motin 	}
1370831f5dcfSAlexander Motin 	/* Configure the bus. */
1371831f5dcfSAlexander Motin 	sdhci_set_clock(slot, ios->clock);
1372831f5dcfSAlexander Motin 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
13732d1731b8SIan Lepore 	if (ios->bus_width == bus_width_8) {
13742d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
1375831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
13762d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_4) {
13772d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
13782d1731b8SIan Lepore 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
13792d1731b8SIan Lepore 	} else if (ios->bus_width == bus_width_1) {
13802d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
13812d1731b8SIan Lepore 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
13822d1731b8SIan Lepore 	} else {
13832d1731b8SIan Lepore 		panic("Invalid bus width: %d", ios->bus_width);
13842d1731b8SIan Lepore 	}
13850f34084fSMarius Strobl 	if (ios->clock > SD_SDR12_MAX &&
1386bba987dcSIan Lepore 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
1387831f5dcfSAlexander Motin 		slot->hostctrl |= SDHCI_CTRL_HISPD;
1388831f5dcfSAlexander Motin 	else
1389831f5dcfSAlexander Motin 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
1390831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
13910f34084fSMarius Strobl 	SDHCI_SET_UHS_TIMING(brdev, slot);
1392831f5dcfSAlexander Motin 	/* Some controllers like reset after bus changes. */
1393d6b3aaf8SOleksandr Tymoshenko 	if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
1394831f5dcfSAlexander Motin 		sdhci_reset(slot, 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 		/* FALLTHROUGH */
1493aca38eabSMarius Strobl 	default:
1494aca38eabSMarius Strobl 		SDHCI_UNLOCK(slot);
1495aca38eabSMarius Strobl 		return (0);
1496aca38eabSMarius Strobl 	}
1497aca38eabSMarius Strobl 
1498aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1499aca38eabSMarius Strobl 	memset(tune_cmd, 0, sizeof(*tune_cmd));
1500aca38eabSMarius Strobl 	tune_cmd->opcode = opcode;
1501aca38eabSMarius Strobl 	tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1502aca38eabSMarius Strobl 	tune_data = tune_cmd->data = slot->tune_data;
1503aca38eabSMarius Strobl 	memset(tune_data, 0, sizeof(*tune_data));
1504aca38eabSMarius Strobl 	tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
1505aca38eabSMarius Strobl 	    ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 :
1506aca38eabSMarius Strobl 	    MMC_TUNING_LEN;
1507aca38eabSMarius Strobl 	tune_data->flags = MMC_DATA_READ;
1508aca38eabSMarius Strobl 	tune_data->mrq = tune_cmd->mrq = slot->tune_req;
1509aca38eabSMarius Strobl 
1510aca38eabSMarius Strobl 	slot->opt &= ~SDHCI_TUNING_ENABLED;
1511aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, true);
1512aca38eabSMarius Strobl 	if (err == 0) {
1513aca38eabSMarius Strobl 		slot->opt |= SDHCI_TUNING_ENABLED;
1514aca38eabSMarius Strobl 		slot->intmask |= sdhci_tuning_intmask(slot);
1515cc22204bSMarius Strobl 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
1516aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1517aca38eabSMarius Strobl 		if (slot->retune_ticks) {
1518aca38eabSMarius Strobl 			callout_reset(&slot->retune_callout, slot->retune_ticks,
1519aca38eabSMarius Strobl 			    sdhci_retune, slot);
1520aca38eabSMarius Strobl 		}
1521aca38eabSMarius Strobl 	}
1522aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1523aca38eabSMarius Strobl 	return (err);
1524aca38eabSMarius Strobl }
1525aca38eabSMarius Strobl 
1526aca38eabSMarius Strobl int
1527aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset)
1528aca38eabSMarius Strobl {
1529aca38eabSMarius Strobl 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1530aca38eabSMarius Strobl 	int err;
1531aca38eabSMarius Strobl 
1532aca38eabSMarius Strobl 	if (!(slot->opt & SDHCI_TUNING_ENABLED))
1533aca38eabSMarius Strobl 		return (0);
1534aca38eabSMarius Strobl 
1535aca38eabSMarius Strobl 	/* HS400 must be tuned in HS200 mode. */
1536aca38eabSMarius Strobl 	if (slot->host.ios.timing == bus_timing_mmc_hs400)
1537aca38eabSMarius Strobl 		return (EINVAL);
1538aca38eabSMarius Strobl 
1539aca38eabSMarius Strobl 	SDHCI_LOCK(slot);
1540aca38eabSMarius Strobl 	err = sdhci_exec_tuning(slot, reset);
1541aca38eabSMarius Strobl 	/*
1542aca38eabSMarius Strobl 	 * There are two ways sdhci_exec_tuning() can fail:
1543aca38eabSMarius Strobl 	 * EBUSY should not actually happen when requests are only issued
1544aca38eabSMarius Strobl 	 *	 with the host properly acquired, and
1545aca38eabSMarius Strobl 	 * EIO   re-tuning failed (but it did work initially).
1546aca38eabSMarius Strobl 	 *
1547aca38eabSMarius Strobl 	 * In both cases, we should retry at later point if periodic re-tuning
1548aca38eabSMarius Strobl 	 * is enabled.  Note that due to slot->retune_req not being cleared in
1549aca38eabSMarius Strobl 	 * these failure cases, the MMC layer should trigger another attempt at
1550aca38eabSMarius Strobl 	 * re-tuning with the next request anyway, though.
1551aca38eabSMarius Strobl 	 */
1552aca38eabSMarius Strobl 	if (slot->retune_ticks) {
1553aca38eabSMarius Strobl 		callout_reset(&slot->retune_callout, slot->retune_ticks,
1554aca38eabSMarius Strobl 		    sdhci_retune, slot);
1555aca38eabSMarius Strobl 	}
1556aca38eabSMarius Strobl 	SDHCI_UNLOCK(slot);
1557aca38eabSMarius Strobl 	return (err);
1558aca38eabSMarius Strobl }
1559aca38eabSMarius Strobl 
1560aca38eabSMarius Strobl static int
1561aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset)
1562aca38eabSMarius Strobl {
1563aca38eabSMarius Strobl 	struct mmc_request *tune_req;
1564aca38eabSMarius Strobl 	struct mmc_command *tune_cmd;
1565aca38eabSMarius Strobl 	int i;
1566aca38eabSMarius Strobl 	uint32_t intmask;
1567aca38eabSMarius Strobl 	uint16_t hostctrl2;
1568aca38eabSMarius Strobl 	u_char opt;
1569aca38eabSMarius Strobl 
1570aca38eabSMarius Strobl 	SDHCI_ASSERT_LOCKED(slot);
1571aca38eabSMarius Strobl 	if (slot->req != NULL)
1572aca38eabSMarius Strobl 		return (EBUSY);
1573aca38eabSMarius Strobl 
1574aca38eabSMarius Strobl 	/* Tuning doesn't work with DMA enabled. */
1575aca38eabSMarius Strobl 	opt = slot->opt;
1576aca38eabSMarius Strobl 	slot->opt = opt & ~SDHCI_HAVE_DMA;
1577aca38eabSMarius Strobl 
1578aca38eabSMarius Strobl 	/*
1579aca38eabSMarius Strobl 	 * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only
1580aca38eabSMarius Strobl 	 * kind of interrupt we receive in response to a tuning request.
1581aca38eabSMarius Strobl 	 */
1582aca38eabSMarius Strobl 	intmask = slot->intmask;
1583aca38eabSMarius Strobl 	slot->intmask = SDHCI_INT_DATA_AVAIL;
1584cc22204bSMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL);
1585aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL);
1586aca38eabSMarius Strobl 
1587aca38eabSMarius Strobl 	hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1588aca38eabSMarius Strobl 	if (reset)
1589aca38eabSMarius Strobl 		hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK;
1590aca38eabSMarius Strobl 	else
1591aca38eabSMarius Strobl 		hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK;
1592aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING);
1593aca38eabSMarius Strobl 
1594aca38eabSMarius Strobl 	tune_req = slot->tune_req;
1595aca38eabSMarius Strobl 	tune_cmd = slot->tune_cmd;
1596aca38eabSMarius Strobl 	for (i = 0; i < MMC_TUNING_MAX; i++) {
1597aca38eabSMarius Strobl 		memset(tune_req, 0, sizeof(*tune_req));
1598aca38eabSMarius Strobl 		tune_req->cmd = tune_cmd;
1599aca38eabSMarius Strobl 		tune_req->done = sdhci_req_wakeup;
1600aca38eabSMarius Strobl 		tune_req->done_data = slot;
1601aca38eabSMarius Strobl 		slot->req = tune_req;
1602aca38eabSMarius Strobl 		slot->flags = 0;
1603aca38eabSMarius Strobl 		sdhci_start(slot);
1604aca38eabSMarius Strobl 		while (!(tune_req->flags & MMC_REQ_DONE))
1605aca38eabSMarius Strobl 			msleep(tune_req, &slot->mtx, 0, "sdhciet", 0);
1606aca38eabSMarius Strobl 		if (!(tune_req->flags & MMC_TUNE_DONE))
1607aca38eabSMarius Strobl 			break;
1608aca38eabSMarius Strobl 		hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1609aca38eabSMarius Strobl 		if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING))
1610aca38eabSMarius Strobl 			break;
1611aca38eabSMarius Strobl 		if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK)
1612aca38eabSMarius Strobl 			DELAY(1000);
1613aca38eabSMarius Strobl 	}
1614aca38eabSMarius Strobl 
161578f8baa8SMarius Strobl 	/*
161678f8baa8SMarius Strobl 	 * Restore DMA usage and interrupts.
161778f8baa8SMarius Strobl 	 * Note that the interrupt aggregation code might have cleared
161878f8baa8SMarius Strobl 	 * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask
161978f8baa8SMarius Strobl 	 * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE
162078f8baa8SMarius Strobl 	 * doesn't lose these.
162178f8baa8SMarius Strobl 	 */
1622aca38eabSMarius Strobl 	slot->opt = opt;
1623aca38eabSMarius Strobl 	slot->intmask = intmask;
162478f8baa8SMarius Strobl 	WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END |
162578f8baa8SMarius Strobl 	    SDHCI_INT_RESPONSE);
1626aca38eabSMarius Strobl 	WR4(slot, SDHCI_SIGNAL_ENABLE, intmask);
1627aca38eabSMarius Strobl 
1628aca38eabSMarius Strobl 	if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING |
1629aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) {
1630aca38eabSMarius Strobl 		slot->retune_req = 0;
1631aca38eabSMarius Strobl 		return (0);
1632aca38eabSMarius Strobl 	}
1633aca38eabSMarius Strobl 
1634aca38eabSMarius Strobl 	slot_printf(slot, "Tuning failed, using fixed sampling clock\n");
1635aca38eabSMarius Strobl 	WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING |
1636aca38eabSMarius Strobl 	    SDHCI_CTRL2_SAMPLING_CLOCK));
1637aca38eabSMarius Strobl 	sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1638aca38eabSMarius Strobl 	return (EIO);
1639aca38eabSMarius Strobl }
1640aca38eabSMarius Strobl 
1641aca38eabSMarius Strobl static void
1642aca38eabSMarius Strobl sdhci_retune(void *arg)
1643aca38eabSMarius Strobl {
1644aca38eabSMarius Strobl 	struct sdhci_slot *slot = arg;
1645aca38eabSMarius Strobl 
1646aca38eabSMarius Strobl 	slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
1647aca38eabSMarius Strobl }
1648aca38eabSMarius Strobl 
1649a94a63f0SWarner Losh #ifdef MMCCAM
1650a94a63f0SWarner Losh static void
1651a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot)
1652a94a63f0SWarner Losh {
1653a94a63f0SWarner Losh 	union ccb *ccb;
165415c440e1SWarner Losh 
1655aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
165615c440e1SWarner Losh 		slot_printf(slot, "%s\n", __func__);
1657a94a63f0SWarner Losh 	if (slot->ccb != NULL && slot->curcmd != NULL) {
1658a94a63f0SWarner Losh 		callout_stop(&slot->timeout_callout);
1659a94a63f0SWarner Losh 		ccb = slot->ccb;
1660a94a63f0SWarner Losh 		slot->ccb = NULL;
1661a94a63f0SWarner Losh 		slot->curcmd = NULL;
1662a94a63f0SWarner Losh 
1663a94a63f0SWarner Losh 		/* Tell CAM the request is finished */
1664a94a63f0SWarner Losh 		struct ccb_mmcio *mmcio;
1665a94a63f0SWarner Losh 		mmcio = &ccb->mmcio;
1666a94a63f0SWarner Losh 
1667a94a63f0SWarner Losh 		ccb->ccb_h.status =
1668a94a63f0SWarner Losh 		    (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR);
1669a94a63f0SWarner Losh 		xpt_done(ccb);
1670a94a63f0SWarner Losh 	}
1671a94a63f0SWarner Losh }
1672a94a63f0SWarner Losh #else
1673831f5dcfSAlexander Motin static void
1674e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot)
1675e64f01a9SIan Lepore {
1676e64f01a9SIan Lepore 	struct mmc_request *req;
1677e64f01a9SIan Lepore 
1678e64f01a9SIan Lepore 	if (slot->req != NULL && slot->curcmd != NULL) {
1679e64f01a9SIan Lepore 		callout_stop(&slot->timeout_callout);
1680e64f01a9SIan Lepore 		req = slot->req;
1681e64f01a9SIan Lepore 		slot->req = NULL;
1682e64f01a9SIan Lepore 		slot->curcmd = NULL;
1683e64f01a9SIan Lepore 		req->done(req);
1684e64f01a9SIan Lepore 	}
1685e64f01a9SIan Lepore }
1686a94a63f0SWarner Losh #endif
1687e64f01a9SIan Lepore 
1688e64f01a9SIan Lepore static void
1689aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req)
1690aca38eabSMarius Strobl {
1691aca38eabSMarius Strobl 	struct sdhci_slot *slot;
1692aca38eabSMarius Strobl 
1693aca38eabSMarius Strobl 	slot = req->done_data;
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);
1706a6873fd1SIan Lepore 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1707e64f01a9SIan Lepore 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1708e64f01a9SIan Lepore 		sdhci_req_done(slot);
17097e586643SIan Lepore 	} else {
17107e586643SIan Lepore 		slot_printf(slot, "Spurious timeout - no active command\n");
1711e64f01a9SIan Lepore 	}
1712e64f01a9SIan Lepore }
1713e64f01a9SIan Lepore 
1714e64f01a9SIan Lepore static void
1715ab00a509SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data)
1716831f5dcfSAlexander Motin {
1717831f5dcfSAlexander Motin 	uint16_t mode;
1718831f5dcfSAlexander Motin 
1719831f5dcfSAlexander Motin 	if (data == NULL)
1720831f5dcfSAlexander Motin 		return;
1721831f5dcfSAlexander Motin 
1722831f5dcfSAlexander Motin 	mode = SDHCI_TRNS_BLK_CNT_EN;
17235d5ae066SIlya Bakulin 	if (data->len > 512 || data->block_count > 1) {
1724831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_MULTI;
17255d5ae066SIlya Bakulin 		if (data->block_count == 0 && __predict_true(
17266dea80e6SMarius Strobl #ifdef MMCCAM
17276dea80e6SMarius Strobl 		    slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION &&
17286dea80e6SMarius Strobl #else
17290519c933SMarius Strobl 		    slot->req->stop != NULL &&
17306dea80e6SMarius Strobl #endif
17316dea80e6SMarius Strobl 		    !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)))
17326dea80e6SMarius Strobl 			mode |= SDHCI_TRNS_ACMD12;
17336dea80e6SMarius Strobl 	}
1734831f5dcfSAlexander Motin 	if (data->flags & MMC_DATA_READ)
1735831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_READ;
1736831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA)
1737831f5dcfSAlexander Motin 		mode |= SDHCI_TRNS_DMA;
1738831f5dcfSAlexander Motin 
1739831f5dcfSAlexander Motin 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
1740831f5dcfSAlexander Motin }
1741831f5dcfSAlexander Motin 
1742831f5dcfSAlexander Motin static void
1743831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
1744831f5dcfSAlexander Motin {
1745831f5dcfSAlexander Motin 	int flags, timeout;
174690993663SIan Lepore 	uint32_t mask;
1747831f5dcfSAlexander Motin 
1748831f5dcfSAlexander Motin 	slot->curcmd = cmd;
1749831f5dcfSAlexander Motin 	slot->cmd_done = 0;
1750831f5dcfSAlexander Motin 
1751831f5dcfSAlexander Motin 	cmd->error = MMC_ERR_NONE;
1752831f5dcfSAlexander Motin 
1753831f5dcfSAlexander Motin 	/* This flags combination is not supported by controller. */
1754831f5dcfSAlexander Motin 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1755831f5dcfSAlexander Motin 		slot_printf(slot, "Unsupported response type!\n");
1756831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1757e64f01a9SIan Lepore 		sdhci_req_done(slot);
1758831f5dcfSAlexander Motin 		return;
1759831f5dcfSAlexander Motin 	}
1760831f5dcfSAlexander Motin 
1761b440e965SMarius Strobl 	/*
1762b440e965SMarius Strobl 	 * Do not issue command if there is no card, clock or power.
1763b440e965SMarius Strobl 	 * Controller will not detect timeout without clock active.
1764b440e965SMarius Strobl 	 */
17656e37fb2bSIan Lepore 	if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
1766d8208d9eSAlexander Motin 	    slot->power == 0 ||
1767d8208d9eSAlexander Motin 	    slot->clock == 0) {
1768a94a63f0SWarner Losh 		slot_printf(slot,
1769440c645bSMitchell Horne 			    "Cannot issue a command (power=%d clock=%d)\n",
1770a94a63f0SWarner Losh 			    slot->power, slot->clock);
1771831f5dcfSAlexander Motin 		cmd->error = MMC_ERR_FAILED;
1772e64f01a9SIan Lepore 		sdhci_req_done(slot);
1773831f5dcfSAlexander Motin 		return;
1774831f5dcfSAlexander Motin 	}
1775831f5dcfSAlexander Motin 	/* Always wait for free CMD bus. */
1776831f5dcfSAlexander Motin 	mask = SDHCI_CMD_INHIBIT;
1777831f5dcfSAlexander Motin 	/* Wait for free DAT if we have data or busy signal. */
1778a94a63f0SWarner Losh 	if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY))
1779831f5dcfSAlexander Motin 		mask |= SDHCI_DAT_INHIBIT;
1780aca38eabSMarius Strobl 	/*
1781aca38eabSMarius Strobl 	 * We shouldn't wait for DAT for stop commands or CMD19/CMD21.  Note
1782aca38eabSMarius Strobl 	 * that these latter are also special in that SDHCI_CMD_DATA should
1783aca38eabSMarius Strobl 	 * be set below but no actual data is ever read from the controller.
1784aca38eabSMarius Strobl 	*/
1785a94a63f0SWarner Losh #ifdef MMCCAM
1786aca38eabSMarius Strobl 	if (cmd == &slot->ccb->mmcio.stop ||
1787a94a63f0SWarner Losh #else
1788aca38eabSMarius Strobl 	if (cmd == slot->req->stop ||
1789a94a63f0SWarner Losh #endif
1790aca38eabSMarius Strobl 	    __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1791aca38eabSMarius Strobl 	    cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))
1792aca38eabSMarius Strobl 		mask &= ~SDHCI_DAT_INHIBIT;
17938775ab45SIan Lepore 	/*
17948775ab45SIan Lepore 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
17958775ab45SIan Lepore 	 *  here at all, but when writing a crash dump we may be bypassing the
17968775ab45SIan Lepore 	 *  host platform's interrupt handler, and in some cases that handler
17978775ab45SIan Lepore 	 *  may be working around hardware quirks such as not respecting r1b
17988775ab45SIan Lepore 	 *  busy indications.  In those cases, this wait-loop serves the purpose
17998775ab45SIan Lepore 	 *  of waiting for the prior command and data transfers to be done, and
18008775ab45SIan Lepore 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
18018775ab45SIan Lepore 	 *  (It's usually more like 20-30ms in the real world.)
18028775ab45SIan Lepore 	 */
18038775ab45SIan Lepore 	timeout = 250;
180490993663SIan Lepore 	while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
1805831f5dcfSAlexander Motin 		if (timeout == 0) {
1806831f5dcfSAlexander Motin 			slot_printf(slot, "Controller never released "
1807831f5dcfSAlexander Motin 			    "inhibit bit(s).\n");
1808831f5dcfSAlexander Motin 			sdhci_dumpregs(slot);
1809831f5dcfSAlexander Motin 			cmd->error = MMC_ERR_FAILED;
1810e64f01a9SIan Lepore 			sdhci_req_done(slot);
1811831f5dcfSAlexander Motin 			return;
1812831f5dcfSAlexander Motin 		}
1813831f5dcfSAlexander Motin 		timeout--;
1814831f5dcfSAlexander Motin 		DELAY(1000);
1815831f5dcfSAlexander Motin 	}
1816831f5dcfSAlexander Motin 
1817831f5dcfSAlexander Motin 	/* Prepare command flags. */
1818831f5dcfSAlexander Motin 	if (!(cmd->flags & MMC_RSP_PRESENT))
1819831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_NONE;
1820831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_136)
1821831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_LONG;
1822831f5dcfSAlexander Motin 	else if (cmd->flags & MMC_RSP_BUSY)
1823831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
1824831f5dcfSAlexander Motin 	else
1825831f5dcfSAlexander Motin 		flags = SDHCI_CMD_RESP_SHORT;
1826831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_CRC)
1827831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_CRC;
1828831f5dcfSAlexander Motin 	if (cmd->flags & MMC_RSP_OPCODE)
1829831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_INDEX;
1830a94a63f0SWarner Losh 	if (cmd->data != NULL)
1831831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_DATA;
1832831f5dcfSAlexander Motin 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
1833831f5dcfSAlexander Motin 		flags |= SDHCI_CMD_TYPE_ABORT;
1834831f5dcfSAlexander Motin 	/* Prepare data. */
1835831f5dcfSAlexander Motin 	sdhci_start_data(slot, cmd->data);
1836831f5dcfSAlexander Motin 	/*
1837831f5dcfSAlexander Motin 	 * Interrupt aggregation: To reduce total number of interrupts
1838831f5dcfSAlexander Motin 	 * group response interrupt with data interrupt when possible.
1839831f5dcfSAlexander Motin 	 * If there going to be data interrupt, mask response one.
1840831f5dcfSAlexander Motin 	 */
1841831f5dcfSAlexander Motin 	if (slot->data_done == 0) {
1842831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1843831f5dcfSAlexander Motin 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
1844831f5dcfSAlexander Motin 	}
1845831f5dcfSAlexander Motin 	/* Set command argument. */
1846831f5dcfSAlexander Motin 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
1847831f5dcfSAlexander Motin 	/* Set data transfer mode. */
1848831f5dcfSAlexander Motin 	sdhci_set_transfer_mode(slot, cmd->data);
1849aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
18508adf4202SBjoern A. Zeeb 		slot_printf(slot, "Starting command opcode %#04x flags %#04x\n",
18518adf4202SBjoern A. Zeeb 		    cmd->opcode, flags);
18528adf4202SBjoern A. Zeeb 
1853831f5dcfSAlexander Motin 	/* Start command. */
1854d6b3aaf8SOleksandr Tymoshenko 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
1855a6873fd1SIan Lepore 	/* Start timeout callout. */
1856ba6fc1c7SLuiz Otavio O Souza 	callout_reset(&slot->timeout_callout, slot->timeout * hz,
1857ba6fc1c7SLuiz Otavio O Souza 	    sdhci_timeout, slot);
1858831f5dcfSAlexander Motin }
1859831f5dcfSAlexander Motin 
1860831f5dcfSAlexander Motin static void
1861831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot)
1862831f5dcfSAlexander Motin {
1863831f5dcfSAlexander Motin 	int i;
18641bacf3beSMarius Strobl 	uint32_t val;
18651bacf3beSMarius Strobl 	uint8_t extra;
1866831f5dcfSAlexander Motin 
1867aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
18688adf4202SBjoern A. Zeeb 		slot_printf(slot, "%s: called, err %d flags %#04x\n",
1869a94a63f0SWarner Losh 		    __func__, slot->curcmd->error, slot->curcmd->flags);
1870831f5dcfSAlexander Motin 	slot->cmd_done = 1;
187172dec079SMarius Strobl 	/*
187272dec079SMarius Strobl 	 * Interrupt aggregation: Restore command interrupt.
1873831f5dcfSAlexander Motin 	 * Main restore point for the case when command interrupt
187472dec079SMarius Strobl 	 * happened first.
187572dec079SMarius Strobl 	 */
1876aca38eabSMarius Strobl 	if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK &&
1877aca38eabSMarius Strobl 	    slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200))
1878aca38eabSMarius Strobl 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |=
1879aca38eabSMarius Strobl 		    SDHCI_INT_RESPONSE);
1880831f5dcfSAlexander Motin 	/* In case of error - reset host and return. */
1881831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
1882aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
1883aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1884831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
1885831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
1886831f5dcfSAlexander Motin 		sdhci_start(slot);
1887831f5dcfSAlexander Motin 		return;
1888831f5dcfSAlexander Motin 	}
1889831f5dcfSAlexander Motin 	/* If command has response - fetch it. */
1890831f5dcfSAlexander Motin 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1891831f5dcfSAlexander Motin 		if (slot->curcmd->flags & MMC_RSP_136) {
1892831f5dcfSAlexander Motin 			/* CRC is stripped so we need one byte shift. */
18931bacf3beSMarius Strobl 			extra = 0;
1894831f5dcfSAlexander Motin 			for (i = 0; i < 4; i++) {
18951bacf3beSMarius Strobl 				val = RD4(slot, SDHCI_RESPONSE + i * 4);
18961bacf3beSMarius Strobl 				if (slot->quirks &
18971bacf3beSMarius Strobl 				    SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1898677ee494SIan Lepore 					slot->curcmd->resp[3 - i] = val;
1899677ee494SIan Lepore 				else {
1900677ee494SIan Lepore 					slot->curcmd->resp[3 - i] =
1901677ee494SIan Lepore 					    (val << 8) | extra;
1902831f5dcfSAlexander Motin 					extra = val >> 24;
1903831f5dcfSAlexander Motin 				}
1904677ee494SIan Lepore 			}
1905831f5dcfSAlexander Motin 		} else
1906831f5dcfSAlexander Motin 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1907831f5dcfSAlexander Motin 	}
1908aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
19098adf4202SBjoern A. Zeeb 		slot_printf(slot, "Resp: %#04x %#04x %#04x %#04x\n",
1910a94a63f0SWarner Losh 		    slot->curcmd->resp[0], slot->curcmd->resp[1],
1911a94a63f0SWarner Losh 		    slot->curcmd->resp[2], slot->curcmd->resp[3]);
1912a94a63f0SWarner Losh 
1913831f5dcfSAlexander Motin 	/* If data ready - finish. */
1914831f5dcfSAlexander Motin 	if (slot->data_done)
1915831f5dcfSAlexander Motin 		sdhci_start(slot);
1916831f5dcfSAlexander Motin }
1917831f5dcfSAlexander Motin 
1918831f5dcfSAlexander Motin static void
1919ab00a509SMarius Strobl sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data)
1920831f5dcfSAlexander Motin {
1921ab00a509SMarius Strobl 	uint32_t blkcnt, blksz, current_timeout, sdma_bbufsz, target_timeout;
1922831f5dcfSAlexander Motin 	uint8_t div;
1923831f5dcfSAlexander Motin 
1924831f5dcfSAlexander Motin 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1925831f5dcfSAlexander Motin 		slot->data_done = 1;
1926831f5dcfSAlexander Motin 		return;
1927831f5dcfSAlexander Motin 	}
1928831f5dcfSAlexander Motin 
1929831f5dcfSAlexander Motin 	slot->data_done = 0;
1930831f5dcfSAlexander Motin 
1931831f5dcfSAlexander Motin 	/* Calculate and set data timeout.*/
1932831f5dcfSAlexander Motin 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
1933ceb9e9f7SIan Lepore 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1934ceb9e9f7SIan Lepore 		div = 0xE;
1935ceb9e9f7SIan Lepore 	} else {
1936831f5dcfSAlexander Motin 		target_timeout = 1000000;
1937831f5dcfSAlexander Motin 		div = 0;
1938831f5dcfSAlexander Motin 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1939ceb9e9f7SIan Lepore 		while (current_timeout < target_timeout && div < 0xE) {
1940ceb9e9f7SIan Lepore 			++div;
1941831f5dcfSAlexander Motin 			current_timeout <<= 1;
1942831f5dcfSAlexander Motin 		}
1943831f5dcfSAlexander Motin 		/* Compensate for an off-by-one error in the CaFe chip.*/
1944ceb9e9f7SIan Lepore 		if (div < 0xE &&
1945ceb9e9f7SIan Lepore 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1946ceb9e9f7SIan Lepore 			++div;
1947831f5dcfSAlexander Motin 		}
1948ceb9e9f7SIan Lepore 	}
1949831f5dcfSAlexander Motin 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1950831f5dcfSAlexander Motin 
1951831f5dcfSAlexander Motin 	if (data == NULL)
1952831f5dcfSAlexander Motin 		return;
1953831f5dcfSAlexander Motin 
1954831f5dcfSAlexander Motin 	/* Use DMA if possible. */
1955831f5dcfSAlexander Motin 	if ((slot->opt & SDHCI_HAVE_DMA))
1956831f5dcfSAlexander Motin 		slot->flags |= SDHCI_USE_DMA;
1957ab00a509SMarius Strobl 	/* If data is small, broken DMA may return zeroes instead of data. */
1958d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1959831f5dcfSAlexander Motin 	    (data->len <= 512))
1960831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1961831f5dcfSAlexander Motin 	/* Some controllers require even block sizes. */
1962d6b3aaf8SOleksandr Tymoshenko 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1963831f5dcfSAlexander Motin 	    ((data->len) & 0x3))
1964831f5dcfSAlexander Motin 		slot->flags &= ~SDHCI_USE_DMA;
1965831f5dcfSAlexander Motin 	/* Load DMA buffer. */
1966831f5dcfSAlexander Motin 	if (slot->flags & SDHCI_USE_DMA) {
1967ab00a509SMarius Strobl 		sdma_bbufsz = slot->sdma_bbufsz;
1968831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ)
1969ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1970ecc2d997SRui Paulo 			    BUS_DMASYNC_PREREAD);
1971831f5dcfSAlexander Motin 		else {
1972ab00a509SMarius Strobl 			memcpy(slot->dmamem, data->data, ulmin(data->len,
1973ab00a509SMarius Strobl 			    sdma_bbufsz));
1974ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1975ecc2d997SRui Paulo 			    BUS_DMASYNC_PREWRITE);
1976831f5dcfSAlexander Motin 		}
1977831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1978ab00a509SMarius Strobl 		/*
1979ab00a509SMarius Strobl 		 * Interrupt aggregation: Mask border interrupt for the last
1980ab00a509SMarius Strobl 		 * bounce buffer and unmask otherwise.
1981ab00a509SMarius Strobl 		 */
1982ab00a509SMarius Strobl 		if (data->len == sdma_bbufsz)
1983831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
1984831f5dcfSAlexander Motin 		else
1985831f5dcfSAlexander Motin 			slot->intmask |= SDHCI_INT_DMA_END;
1986831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1987831f5dcfSAlexander Motin 	}
1988831f5dcfSAlexander Motin 	/* Current data offset for both PIO and DMA. */
1989831f5dcfSAlexander Motin 	slot->offset = 0;
19905d5ae066SIlya Bakulin #ifdef MMCCAM
19915d5ae066SIlya Bakulin 	if (data->flags & MMC_DATA_BLOCK_SIZE) {
19925d5ae066SIlya Bakulin 		/* Set block size and request border interrupts on the SDMA boundary. */
19935d5ae066SIlya Bakulin 		blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, data->block_size);
19945d5ae066SIlya Bakulin 		blkcnt = data->block_count;
19955d5ae066SIlya Bakulin 		if (__predict_false(sdhci_debug > 0))
19965d5ae066SIlya Bakulin 			slot_printf(slot, "SDIO Custom block params: blksz: "
19975d5ae066SIlya Bakulin 			    "%#10x, blk cnt: %#10x\n", blksz, blkcnt);
19985d5ae066SIlya Bakulin 	} else
19995d5ae066SIlya Bakulin #endif
20005d5ae066SIlya Bakulin 	{
2001ab00a509SMarius Strobl 		/* Set block size and request border interrupts on the SDMA boundary. */
2002ab00a509SMarius Strobl 		blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, ulmin(data->len, 512));
2003ab00a509SMarius Strobl 		blkcnt = howmany(data->len, 512);
20045d5ae066SIlya Bakulin 	}
20055d5ae066SIlya Bakulin 
20065d5ae066SIlya Bakulin 	WR2(slot, SDHCI_BLOCK_SIZE, blksz);
2007ab00a509SMarius Strobl 	WR2(slot, SDHCI_BLOCK_COUNT, blkcnt);
2008aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
2009ab00a509SMarius Strobl 		slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
2010ab00a509SMarius Strobl 		    blksz, blkcnt);
2011831f5dcfSAlexander Motin }
2012831f5dcfSAlexander Motin 
2013c3a0f75aSOleksandr Tymoshenko void
2014831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot)
2015831f5dcfSAlexander Motin {
2016831f5dcfSAlexander Motin 	struct mmc_data *data = slot->curcmd->data;
20177e6ccea3SMarius Strobl 	size_t left;
2018831f5dcfSAlexander Motin 
2019831f5dcfSAlexander Motin 	/* Interrupt aggregation: Restore command interrupt.
2020ecc2d997SRui Paulo 	 * Auxiliary restore point for the case when data interrupt
2021831f5dcfSAlexander Motin 	 * happened first. */
2022831f5dcfSAlexander Motin 	if (!slot->cmd_done) {
2023831f5dcfSAlexander Motin 		WR4(slot, SDHCI_SIGNAL_ENABLE,
2024831f5dcfSAlexander Motin 		    slot->intmask |= SDHCI_INT_RESPONSE);
2025831f5dcfSAlexander Motin 	}
2026831f5dcfSAlexander Motin 	/* Unload rest of data from DMA buffer. */
2027915780d7SLuiz Otavio O Souza 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) &&
2028915780d7SLuiz Otavio O Souza 	    slot->curcmd->data != NULL) {
2029831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
20307e6ccea3SMarius Strobl 			left = data->len - slot->offset;
2031ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2032ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTREAD);
2033831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
2034ab00a509SMarius Strobl 			    ulmin(left, slot->sdma_bbufsz));
2035831f5dcfSAlexander Motin 		} else
2036ecc2d997SRui Paulo 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2037ecc2d997SRui Paulo 			    BUS_DMASYNC_POSTWRITE);
2038831f5dcfSAlexander Motin 	}
2039a98788edSIan Lepore 	slot->data_done = 1;
2040831f5dcfSAlexander Motin 	/* If there was error - reset the host. */
2041831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
2042aca38eabSMarius Strobl 		if (slot->curcmd->error == MMC_ERR_BADCRC)
2043aca38eabSMarius Strobl 			slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
2044831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
2045831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
2046831f5dcfSAlexander Motin 		sdhci_start(slot);
2047831f5dcfSAlexander Motin 		return;
2048831f5dcfSAlexander Motin 	}
2049831f5dcfSAlexander Motin 	/* If we already have command response - finish. */
2050831f5dcfSAlexander Motin 	if (slot->cmd_done)
2051831f5dcfSAlexander Motin 		sdhci_start(slot);
2052831f5dcfSAlexander Motin }
2053831f5dcfSAlexander Motin 
2054a94a63f0SWarner Losh #ifdef MMCCAM
2055a94a63f0SWarner Losh static void
2056a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot)
2057a94a63f0SWarner Losh {
2058a94a63f0SWarner Losh 	union ccb *ccb;
2059ab00a509SMarius Strobl 	struct ccb_mmcio *mmcio;
2060a94a63f0SWarner Losh 
2061a94a63f0SWarner Losh 	ccb = slot->ccb;
2062a94a63f0SWarner Losh 	if (ccb == NULL)
2063a94a63f0SWarner Losh 		return;
2064a94a63f0SWarner Losh 
2065a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
2066a94a63f0SWarner Losh 	if (!(slot->flags & CMD_STARTED)) {
2067a94a63f0SWarner Losh 		slot->flags |= CMD_STARTED;
2068a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->cmd);
2069a94a63f0SWarner Losh 		return;
2070a94a63f0SWarner Losh 	}
2071a94a63f0SWarner Losh 
2072a94a63f0SWarner Losh 	/*
2073a94a63f0SWarner Losh 	 * Old stack doesn't use this!
2074a94a63f0SWarner Losh 	 * Enabling this code causes significant performance degradation
2075a94a63f0SWarner Losh 	 * and IRQ storms on BBB, Wandboard behaves fine.
2076a94a63f0SWarner Losh 	 * Not using this code does no harm...
2077a94a63f0SWarner Losh 	if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) {
2078a94a63f0SWarner Losh 		slot->flags |= STOP_STARTED;
2079a94a63f0SWarner Losh 		sdhci_start_command(slot, &mmcio->stop);
2080a94a63f0SWarner Losh 		return;
2081a94a63f0SWarner Losh 	}
2082a94a63f0SWarner Losh 	*/
2083aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
2084a94a63f0SWarner Losh 		slot_printf(slot, "result: %d\n", mmcio->cmd.error);
2085a94a63f0SWarner Losh 	if (mmcio->cmd.error == 0 &&
2086a94a63f0SWarner Losh 	    (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
2087a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD);
2088a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_DATA);
2089a94a63f0SWarner Losh 	}
2090a94a63f0SWarner Losh 
2091a94a63f0SWarner Losh 	sdhci_req_done(slot);
2092a94a63f0SWarner Losh }
2093a94a63f0SWarner Losh #else
2094831f5dcfSAlexander Motin static void
2095831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot)
2096831f5dcfSAlexander Motin {
2097ab00a509SMarius Strobl 	const struct mmc_request *req;
2098831f5dcfSAlexander Motin 
2099831f5dcfSAlexander Motin 	req = slot->req;
2100831f5dcfSAlexander Motin 	if (req == NULL)
2101831f5dcfSAlexander Motin 		return;
2102831f5dcfSAlexander Motin 
2103831f5dcfSAlexander Motin 	if (!(slot->flags & CMD_STARTED)) {
2104831f5dcfSAlexander Motin 		slot->flags |= CMD_STARTED;
2105831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->cmd);
2106831f5dcfSAlexander Motin 		return;
2107831f5dcfSAlexander Motin 	}
2108915780d7SLuiz Otavio O Souza 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) &&
2109915780d7SLuiz Otavio O Souza 	    !(slot->flags & STOP_STARTED) && req->stop) {
2110831f5dcfSAlexander Motin 		slot->flags |= STOP_STARTED;
2111831f5dcfSAlexander Motin 		sdhci_start_command(slot, req->stop);
2112831f5dcfSAlexander Motin 		return;
2113831f5dcfSAlexander Motin 	}
2114aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1))
21155b69a497SAlexander Motin 		slot_printf(slot, "result: %d\n", req->cmd->error);
21165b69a497SAlexander Motin 	if (!req->cmd->error &&
2117915780d7SLuiz Otavio O Souza 	    ((slot->curcmd == req->stop &&
2118915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) ||
2119915780d7SLuiz Otavio O Souza 	     (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2120831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_CMD);
2121831f5dcfSAlexander Motin 		sdhci_reset(slot, SDHCI_RESET_DATA);
2122831f5dcfSAlexander Motin 	}
2123831f5dcfSAlexander Motin 
2124e64f01a9SIan Lepore 	sdhci_req_done(slot);
2125831f5dcfSAlexander Motin }
2126a94a63f0SWarner Losh #endif
2127831f5dcfSAlexander Motin 
2128d6b3aaf8SOleksandr Tymoshenko int
2129b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev,
2130b440e965SMarius Strobl     struct mmc_request *req)
2131831f5dcfSAlexander Motin {
2132831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2133831f5dcfSAlexander Motin 
2134831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2135831f5dcfSAlexander Motin 	if (slot->req != NULL) {
2136831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
2137831f5dcfSAlexander Motin 		return (EBUSY);
2138831f5dcfSAlexander Motin 	}
2139aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
21401bacf3beSMarius Strobl 		slot_printf(slot,
21411bacf3beSMarius Strobl 		    "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
2142831f5dcfSAlexander Motin 		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
21435b69a497SAlexander Motin 		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
21445b69a497SAlexander Motin 		    (req->cmd->data)?req->cmd->data->flags:0);
21455b69a497SAlexander Motin 	}
2146831f5dcfSAlexander Motin 	slot->req = req;
2147831f5dcfSAlexander Motin 	slot->flags = 0;
2148831f5dcfSAlexander Motin 	sdhci_start(slot);
2149831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2150bea2dca2SAlexander Motin 	if (dumping) {
2151bea2dca2SAlexander Motin 		while (slot->req != NULL) {
2152d6b3aaf8SOleksandr Tymoshenko 			sdhci_generic_intr(slot);
2153bea2dca2SAlexander Motin 			DELAY(10);
2154bea2dca2SAlexander Motin 		}
2155bea2dca2SAlexander Motin 	}
2156831f5dcfSAlexander Motin 	return (0);
2157831f5dcfSAlexander Motin }
2158831f5dcfSAlexander Motin 
2159d6b3aaf8SOleksandr Tymoshenko int
2160b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev)
2161831f5dcfSAlexander Motin {
2162831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2163831f5dcfSAlexander Motin 	uint32_t val;
2164831f5dcfSAlexander Motin 
2165831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2166831f5dcfSAlexander Motin 	val = RD4(slot, SDHCI_PRESENT_STATE);
2167831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2168831f5dcfSAlexander Motin 	return (!(val & SDHCI_WRITE_PROTECT));
2169831f5dcfSAlexander Motin }
2170831f5dcfSAlexander Motin 
2171d6b3aaf8SOleksandr Tymoshenko int
2172b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev)
2173831f5dcfSAlexander Motin {
2174831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2175831f5dcfSAlexander Motin 	int err = 0;
2176831f5dcfSAlexander Motin 
2177831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2178831f5dcfSAlexander Motin 	while (slot->bus_busy)
2179d493985aSAlexander Motin 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
2180831f5dcfSAlexander Motin 	slot->bus_busy++;
2181831f5dcfSAlexander Motin 	/* Activate led. */
2182831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
2183831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2184831f5dcfSAlexander Motin 	return (err);
2185831f5dcfSAlexander Motin }
2186831f5dcfSAlexander Motin 
2187d6b3aaf8SOleksandr Tymoshenko int
2188b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev)
2189831f5dcfSAlexander Motin {
2190831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(reqdev);
2191831f5dcfSAlexander Motin 
2192831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2193831f5dcfSAlexander Motin 	/* Deactivate led. */
2194831f5dcfSAlexander Motin 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
2195831f5dcfSAlexander Motin 	slot->bus_busy--;
2196d493985aSAlexander Motin 	wakeup(slot);
219735547df5SScott Long 	SDHCI_UNLOCK(slot);
2198831f5dcfSAlexander Motin 	return (0);
2199831f5dcfSAlexander Motin }
2200831f5dcfSAlexander Motin 
2201831f5dcfSAlexander Motin static void
2202831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
2203831f5dcfSAlexander Motin {
2204831f5dcfSAlexander Motin 
2205831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2206831f5dcfSAlexander Motin 		slot_printf(slot, "Got command interrupt 0x%08x, but "
2207831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2208831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2209831f5dcfSAlexander Motin 		return;
2210831f5dcfSAlexander Motin 	}
2211831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_TIMEOUT)
2212831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2213831f5dcfSAlexander Motin 	else if (intmask & SDHCI_INT_CRC)
2214831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2215831f5dcfSAlexander Motin 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
2216831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_FIFO;
2217831f5dcfSAlexander Motin 
2218831f5dcfSAlexander Motin 	sdhci_finish_command(slot);
2219831f5dcfSAlexander Motin }
2220831f5dcfSAlexander Motin 
2221831f5dcfSAlexander Motin static void
2222831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
2223831f5dcfSAlexander Motin {
22241bacf3beSMarius Strobl 	struct mmc_data *data;
222515c440e1SWarner Losh 	size_t left;
2226ab00a509SMarius Strobl 	uint32_t sdma_bbufsz;
2227831f5dcfSAlexander Motin 
2228831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2229831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2230831f5dcfSAlexander Motin 		    "there is no active command.\n", intmask);
2231831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2232831f5dcfSAlexander Motin 		return;
2233831f5dcfSAlexander Motin 	}
2234831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2235831f5dcfSAlexander Motin 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
2236831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2237831f5dcfSAlexander Motin 		    "there is no active data operation.\n",
2238831f5dcfSAlexander Motin 		    intmask);
2239831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2240831f5dcfSAlexander Motin 		return;
2241831f5dcfSAlexander Motin 	}
2242831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
2243831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_TIMEOUT;
2244acbaa69fSOleksandr Tymoshenko 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
2245831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_BADCRC;
2246831f5dcfSAlexander Motin 	if (slot->curcmd->data == NULL &&
2247831f5dcfSAlexander Motin 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
2248831f5dcfSAlexander Motin 	    SDHCI_INT_DMA_END))) {
2249831f5dcfSAlexander Motin 		slot_printf(slot, "Got data interrupt 0x%08x, but "
2250831f5dcfSAlexander Motin 		    "there is busy-only command.\n", intmask);
2251831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2252831f5dcfSAlexander Motin 		slot->curcmd->error = MMC_ERR_INVALID;
2253831f5dcfSAlexander Motin 	}
2254831f5dcfSAlexander Motin 	if (slot->curcmd->error) {
2255831f5dcfSAlexander Motin 		/* No need to continue after any error. */
2256a98788edSIan Lepore 		goto done;
2257831f5dcfSAlexander Motin 	}
2258831f5dcfSAlexander Motin 
2259aca38eabSMarius Strobl 	/* Handle tuning completion interrupt. */
2260aca38eabSMarius Strobl 	if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) &&
2261aca38eabSMarius Strobl 	    (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK ||
2262aca38eabSMarius Strobl 	    slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) {
2263aca38eabSMarius Strobl 		slot->req->flags |= MMC_TUNE_DONE;
2264aca38eabSMarius Strobl 		sdhci_finish_command(slot);
2265aca38eabSMarius Strobl 		sdhci_finish_data(slot);
2266aca38eabSMarius Strobl 		return;
2267aca38eabSMarius Strobl 	}
2268831f5dcfSAlexander Motin 	/* Handle PIO interrupt. */
2269c3a0f75aSOleksandr Tymoshenko 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
2270c3a0f75aSOleksandr Tymoshenko 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
2271c3a0f75aSOleksandr Tymoshenko 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
22721bacf3beSMarius Strobl 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot,
22731bacf3beSMarius Strobl 			    &intmask);
2274c3a0f75aSOleksandr Tymoshenko 			slot->flags |= PLATFORM_DATA_STARTED;
2275c3a0f75aSOleksandr Tymoshenko 		} else
2276831f5dcfSAlexander Motin 			sdhci_transfer_pio(slot);
2277c3a0f75aSOleksandr Tymoshenko 	}
2278831f5dcfSAlexander Motin 	/* Handle DMA border. */
2279831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DMA_END) {
22801bacf3beSMarius Strobl 		data = slot->curcmd->data;
2281ab00a509SMarius Strobl 		sdma_bbufsz = slot->sdma_bbufsz;
2282831f5dcfSAlexander Motin 
2283831f5dcfSAlexander Motin 		/* Unload DMA buffer ... */
2284831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2285831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2286831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2287831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTREAD);
2288831f5dcfSAlexander Motin 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
2289ab00a509SMarius Strobl 			    ulmin(left, sdma_bbufsz));
2290831f5dcfSAlexander Motin 		} else {
2291831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2292831f5dcfSAlexander Motin 			    BUS_DMASYNC_POSTWRITE);
2293831f5dcfSAlexander Motin 		}
2294831f5dcfSAlexander Motin 		/* ... and reload it again. */
2295ab00a509SMarius Strobl 		slot->offset += sdma_bbufsz;
2296831f5dcfSAlexander Motin 		left = data->len - slot->offset;
2297831f5dcfSAlexander Motin 		if (data->flags & MMC_DATA_READ) {
2298831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2299831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREREAD);
2300831f5dcfSAlexander Motin 		} else {
2301831f5dcfSAlexander Motin 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
2302ab00a509SMarius Strobl 			    ulmin(left, sdma_bbufsz));
2303831f5dcfSAlexander Motin 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
2304831f5dcfSAlexander Motin 			    BUS_DMASYNC_PREWRITE);
2305831f5dcfSAlexander Motin 		}
2306ab00a509SMarius Strobl 		/*
2307ab00a509SMarius Strobl 		 * Interrupt aggregation: Mask border interrupt for the last
2308ab00a509SMarius Strobl 		 * bounce buffer.
2309ab00a509SMarius Strobl 		 */
2310ab00a509SMarius Strobl 		if (left == sdma_bbufsz) {
2311831f5dcfSAlexander Motin 			slot->intmask &= ~SDHCI_INT_DMA_END;
2312831f5dcfSAlexander Motin 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2313831f5dcfSAlexander Motin 		}
2314831f5dcfSAlexander Motin 		/* Restart DMA. */
2315831f5dcfSAlexander Motin 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
2316831f5dcfSAlexander Motin 	}
2317831f5dcfSAlexander Motin 	/* We have got all data. */
2318c3a0f75aSOleksandr Tymoshenko 	if (intmask & SDHCI_INT_DATA_END) {
2319c3a0f75aSOleksandr Tymoshenko 		if (slot->flags & PLATFORM_DATA_STARTED) {
2320c3a0f75aSOleksandr Tymoshenko 			slot->flags &= ~PLATFORM_DATA_STARTED;
2321c3a0f75aSOleksandr Tymoshenko 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2322c3a0f75aSOleksandr Tymoshenko 		} else
2323831f5dcfSAlexander Motin 			sdhci_finish_data(slot);
2324831f5dcfSAlexander Motin 	}
2325a98788edSIan Lepore done:
2326a98788edSIan Lepore 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
2327a98788edSIan Lepore 		if (slot->flags & PLATFORM_DATA_STARTED) {
2328a98788edSIan Lepore 			slot->flags &= ~PLATFORM_DATA_STARTED;
2329a98788edSIan Lepore 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
2330a98788edSIan Lepore 		} else
2331a98788edSIan Lepore 			sdhci_finish_data(slot);
2332a98788edSIan Lepore 	}
2333c3a0f75aSOleksandr Tymoshenko }
2334831f5dcfSAlexander Motin 
2335831f5dcfSAlexander Motin static void
23366dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err)
2337831f5dcfSAlexander Motin {
2338831f5dcfSAlexander Motin 
2339831f5dcfSAlexander Motin 	if (!slot->curcmd) {
2340831f5dcfSAlexander Motin 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
23416dea80e6SMarius Strobl 		    "there is no active command.\n", acmd_err);
2342831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2343831f5dcfSAlexander Motin 		return;
2344831f5dcfSAlexander Motin 	}
23456dea80e6SMarius Strobl 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err);
2346831f5dcfSAlexander Motin 	sdhci_reset(slot, SDHCI_RESET_CMD);
2347831f5dcfSAlexander Motin }
2348831f5dcfSAlexander Motin 
2349d6b3aaf8SOleksandr Tymoshenko void
2350d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot)
2351831f5dcfSAlexander Motin {
23522b96b955SJustin Hibbits 	uint32_t intmask, present;
23536dea80e6SMarius Strobl 	uint16_t val16;
2354831f5dcfSAlexander Motin 
2355831f5dcfSAlexander Motin 	SDHCI_LOCK(slot);
2356831f5dcfSAlexander Motin 	/* Read slot interrupt status. */
2357831f5dcfSAlexander Motin 	intmask = RD4(slot, SDHCI_INT_STATUS);
2358831f5dcfSAlexander Motin 	if (intmask == 0 || intmask == 0xffffffff) {
2359831f5dcfSAlexander Motin 		SDHCI_UNLOCK(slot);
2360d6b3aaf8SOleksandr Tymoshenko 		return;
2361831f5dcfSAlexander Motin 	}
2362aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 2))
23635b69a497SAlexander Motin 		slot_printf(slot, "Interrupt %#x\n", intmask);
23645b69a497SAlexander Motin 
2365aca38eabSMarius Strobl 	/* Handle tuning error interrupt. */
2366aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_TUNEERR)) {
23676dea80e6SMarius Strobl 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR);
2368aca38eabSMarius Strobl 		slot_printf(slot, "Tuning error indicated\n");
2369aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
2370aca38eabSMarius Strobl 		if (slot->curcmd) {
2371aca38eabSMarius Strobl 			slot->curcmd->error = MMC_ERR_BADCRC;
2372aca38eabSMarius Strobl 			sdhci_finish_command(slot);
2373aca38eabSMarius Strobl 		}
2374aca38eabSMarius Strobl 	}
2375aca38eabSMarius Strobl 	/* Handle re-tuning interrupt. */
2376aca38eabSMarius Strobl 	if (__predict_false(intmask & SDHCI_INT_RETUNE))
2377aca38eabSMarius Strobl 		slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
2378831f5dcfSAlexander Motin 	/* Handle card presence interrupts. */
2379831f5dcfSAlexander Motin 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2380639f59f0SIan Lepore 		present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
23812b96b955SJustin Hibbits 		slot->intmask &=
23822b96b955SJustin Hibbits 		    ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
23832b96b955SJustin Hibbits 		slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
23842b96b955SJustin Hibbits 		    SDHCI_INT_CARD_INSERT;
23852b96b955SJustin Hibbits 		WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
23862b96b955SJustin Hibbits 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2387831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask &
2388831f5dcfSAlexander Motin 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
2389b8bf08b1SIan Lepore 		sdhci_handle_card_present_locked(slot, present);
2390831f5dcfSAlexander Motin 	}
2391831f5dcfSAlexander Motin 	/* Handle command interrupts. */
2392831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_CMD_MASK) {
2393831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
2394831f5dcfSAlexander Motin 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
2395831f5dcfSAlexander Motin 	}
2396831f5dcfSAlexander Motin 	/* Handle data interrupts. */
2397831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_DATA_MASK) {
2398831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
23997e6ccea3SMarius Strobl 		/* Don't call data_irq in case of errored command. */
24007e586643SIan Lepore 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
2401831f5dcfSAlexander Motin 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
2402831f5dcfSAlexander Motin 	}
2403831f5dcfSAlexander Motin 	/* Handle AutoCMD12 error interrupt. */
2404831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_ACMD12ERR) {
24056dea80e6SMarius Strobl 		/* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */
24066dea80e6SMarius Strobl 		val16 = RD2(slot, SDHCI_ACMD12_ERR);
2407831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
24086dea80e6SMarius Strobl 		sdhci_acmd_irq(slot, val16);
2409831f5dcfSAlexander Motin 	}
2410831f5dcfSAlexander Motin 	/* Handle bus power interrupt. */
2411831f5dcfSAlexander Motin 	if (intmask & SDHCI_INT_BUS_POWER) {
2412831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
2413aca38eabSMarius Strobl 		slot_printf(slot, "Card is consuming too much power!\n");
2414831f5dcfSAlexander Motin 	}
2415aca38eabSMarius Strobl 	intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE |
2416aca38eabSMarius Strobl 	    SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK |
2417aca38eabSMarius Strobl 	    SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER);
2418831f5dcfSAlexander Motin 	/* The rest is unknown. */
2419831f5dcfSAlexander Motin 	if (intmask) {
2420831f5dcfSAlexander Motin 		WR4(slot, SDHCI_INT_STATUS, intmask);
2421831f5dcfSAlexander Motin 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
2422831f5dcfSAlexander Motin 		    intmask);
2423831f5dcfSAlexander Motin 		sdhci_dumpregs(slot);
2424831f5dcfSAlexander Motin 	}
2425831f5dcfSAlexander Motin 
2426831f5dcfSAlexander Motin 	SDHCI_UNLOCK(slot);
2427831f5dcfSAlexander Motin }
2428831f5dcfSAlexander Motin 
2429d6b3aaf8SOleksandr Tymoshenko int
24301bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which,
24311bacf3beSMarius Strobl     uintptr_t *result)
2432831f5dcfSAlexander Motin {
2433ab00a509SMarius Strobl 	const struct sdhci_slot *slot = device_get_ivars(child);
2434831f5dcfSAlexander Motin 
2435831f5dcfSAlexander Motin 	switch (which) {
2436831f5dcfSAlexander Motin 	default:
2437831f5dcfSAlexander Motin 		return (EINVAL);
2438831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2439bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_mode;
2440831f5dcfSAlexander Motin 		break;
2441831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2442bcd91d25SJayachandran C. 		*result = slot->host.ios.bus_width;
2443831f5dcfSAlexander Motin 		break;
2444831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2445bcd91d25SJayachandran C. 		*result = slot->host.ios.chip_select;
2446831f5dcfSAlexander Motin 		break;
2447831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2448bcd91d25SJayachandran C. 		*result = slot->host.ios.clock;
2449831f5dcfSAlexander Motin 		break;
2450831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2451bcd91d25SJayachandran C. 		*result = slot->host.f_min;
2452831f5dcfSAlexander Motin 		break;
2453831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
2454bcd91d25SJayachandran C. 		*result = slot->host.f_max;
2455831f5dcfSAlexander Motin 		break;
2456831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2457bcd91d25SJayachandran C. 		*result = slot->host.host_ocr;
2458831f5dcfSAlexander Motin 		break;
2459831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2460bcd91d25SJayachandran C. 		*result = slot->host.mode;
2461831f5dcfSAlexander Motin 		break;
2462831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2463bcd91d25SJayachandran C. 		*result = slot->host.ocr;
2464831f5dcfSAlexander Motin 		break;
2465831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2466bcd91d25SJayachandran C. 		*result = slot->host.ios.power_mode;
2467831f5dcfSAlexander Motin 		break;
2468831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2469bcd91d25SJayachandran C. 		*result = slot->host.ios.vdd;
2470831f5dcfSAlexander Motin 		break;
2471aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2472aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED) {
2473aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) {
2474aca38eabSMarius Strobl 				*result = retune_req_reset;
2475aca38eabSMarius Strobl 				break;
2476aca38eabSMarius Strobl 			}
2477aca38eabSMarius Strobl 			if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) {
2478aca38eabSMarius Strobl 				*result = retune_req_normal;
2479aca38eabSMarius Strobl 				break;
2480aca38eabSMarius Strobl 			}
2481aca38eabSMarius Strobl 		}
2482aca38eabSMarius Strobl 		*result = retune_req_none;
2483aca38eabSMarius Strobl 		break;
24840f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
24850f34084fSMarius Strobl 		*result = slot->host.ios.vccq;
24860f34084fSMarius Strobl 		break;
2487831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2488bcd91d25SJayachandran C. 		*result = slot->host.caps;
2489831f5dcfSAlexander Motin 		break;
2490831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2491bcd91d25SJayachandran C. 		*result = slot->host.ios.timing;
2492831f5dcfSAlexander Motin 		break;
24933a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2494aca38eabSMarius Strobl 		/*
2495aca38eabSMarius Strobl 		 * Re-tuning modes 1 and 2 restrict the maximum data length
2496aca38eabSMarius Strobl 		 * per read/write command to 4 MiB.
2497aca38eabSMarius Strobl 		 */
2498aca38eabSMarius Strobl 		if (slot->opt & SDHCI_TUNING_ENABLED &&
2499aca38eabSMarius Strobl 		    (slot->retune_mode == SDHCI_RETUNE_MODE_1 ||
2500aca38eabSMarius Strobl 		    slot->retune_mode == SDHCI_RETUNE_MODE_2)) {
2501aca38eabSMarius Strobl 			*result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE;
2502aca38eabSMarius Strobl 			break;
2503aca38eabSMarius Strobl 		}
2504bcd91d25SJayachandran C. 		*result = 65535;
25053a4a2557SAlexander Motin 		break;
250672dec079SMarius Strobl 	case MMCBR_IVAR_MAX_BUSY_TIMEOUT:
250772dec079SMarius Strobl 		/*
250872dec079SMarius Strobl 		 * Currently, sdhci_start_data() hardcodes 1 s for all CMDs.
250972dec079SMarius Strobl 		 */
251072dec079SMarius Strobl 		*result = 1000000;
251172dec079SMarius Strobl 		break;
2512831f5dcfSAlexander Motin 	}
2513831f5dcfSAlexander Motin 	return (0);
2514831f5dcfSAlexander Motin }
2515831f5dcfSAlexander Motin 
2516d6b3aaf8SOleksandr Tymoshenko int
25171bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which,
25181bacf3beSMarius Strobl     uintptr_t value)
2519831f5dcfSAlexander Motin {
2520831f5dcfSAlexander Motin 	struct sdhci_slot *slot = device_get_ivars(child);
2521b440e965SMarius Strobl 	uint32_t clock, max_clock;
2522b440e965SMarius Strobl 	int i;
2523831f5dcfSAlexander Motin 
252415c440e1SWarner Losh 	if (sdhci_debug > 1)
252515c440e1SWarner Losh 		slot_printf(slot, "%s: var=%d\n", __func__, which);
2526831f5dcfSAlexander Motin 	switch (which) {
2527831f5dcfSAlexander Motin 	default:
2528831f5dcfSAlexander Motin 		return (EINVAL);
2529831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_MODE:
2530831f5dcfSAlexander Motin 		slot->host.ios.bus_mode = value;
2531831f5dcfSAlexander Motin 		break;
2532831f5dcfSAlexander Motin 	case MMCBR_IVAR_BUS_WIDTH:
2533831f5dcfSAlexander Motin 		slot->host.ios.bus_width = value;
2534831f5dcfSAlexander Motin 		break;
2535831f5dcfSAlexander Motin 	case MMCBR_IVAR_CHIP_SELECT:
2536831f5dcfSAlexander Motin 		slot->host.ios.chip_select = value;
2537831f5dcfSAlexander Motin 		break;
2538831f5dcfSAlexander Motin 	case MMCBR_IVAR_CLOCK:
2539831f5dcfSAlexander Motin 		if (value > 0) {
254057677a3aSOleksandr Tymoshenko 			max_clock = slot->max_clk;
254157677a3aSOleksandr Tymoshenko 			clock = max_clock;
254257677a3aSOleksandr Tymoshenko 
254357677a3aSOleksandr Tymoshenko 			if (slot->version < SDHCI_SPEC_300) {
254457677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
254557677a3aSOleksandr Tymoshenko 				    i <<= 1) {
2546831f5dcfSAlexander Motin 					if (clock <= value)
2547831f5dcfSAlexander Motin 						break;
2548831f5dcfSAlexander Motin 					clock >>= 1;
2549831f5dcfSAlexander Motin 				}
2550b440e965SMarius Strobl 			} else {
255157677a3aSOleksandr Tymoshenko 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
255257677a3aSOleksandr Tymoshenko 				    i += 2) {
255357677a3aSOleksandr Tymoshenko 					if (clock <= value)
255457677a3aSOleksandr Tymoshenko 						break;
255557677a3aSOleksandr Tymoshenko 					clock = max_clock / (i + 2);
255657677a3aSOleksandr Tymoshenko 				}
255757677a3aSOleksandr Tymoshenko 			}
255857677a3aSOleksandr Tymoshenko 
2559831f5dcfSAlexander Motin 			slot->host.ios.clock = clock;
2560831f5dcfSAlexander Motin 		} else
2561831f5dcfSAlexander Motin 			slot->host.ios.clock = 0;
2562831f5dcfSAlexander Motin 		break;
2563831f5dcfSAlexander Motin 	case MMCBR_IVAR_MODE:
2564831f5dcfSAlexander Motin 		slot->host.mode = value;
2565831f5dcfSAlexander Motin 		break;
2566831f5dcfSAlexander Motin 	case MMCBR_IVAR_OCR:
2567831f5dcfSAlexander Motin 		slot->host.ocr = value;
2568831f5dcfSAlexander Motin 		break;
2569831f5dcfSAlexander Motin 	case MMCBR_IVAR_POWER_MODE:
2570831f5dcfSAlexander Motin 		slot->host.ios.power_mode = value;
2571831f5dcfSAlexander Motin 		break;
2572831f5dcfSAlexander Motin 	case MMCBR_IVAR_VDD:
2573831f5dcfSAlexander Motin 		slot->host.ios.vdd = value;
2574831f5dcfSAlexander Motin 		break;
25750f34084fSMarius Strobl 	case MMCBR_IVAR_VCCQ:
25760f34084fSMarius Strobl 		slot->host.ios.vccq = value;
25770f34084fSMarius Strobl 		break;
2578831f5dcfSAlexander Motin 	case MMCBR_IVAR_TIMING:
2579831f5dcfSAlexander Motin 		slot->host.ios.timing = value;
2580831f5dcfSAlexander Motin 		break;
2581831f5dcfSAlexander Motin 	case MMCBR_IVAR_CAPS:
2582831f5dcfSAlexander Motin 	case MMCBR_IVAR_HOST_OCR:
2583831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MIN:
2584831f5dcfSAlexander Motin 	case MMCBR_IVAR_F_MAX:
25853a4a2557SAlexander Motin 	case MMCBR_IVAR_MAX_DATA:
2586aca38eabSMarius Strobl 	case MMCBR_IVAR_RETUNE_REQ:
2587831f5dcfSAlexander Motin 		return (EINVAL);
2588831f5dcfSAlexander Motin 	}
2589831f5dcfSAlexander Motin 	return (0);
2590831f5dcfSAlexander Motin }
2591831f5dcfSAlexander Motin 
259215c440e1SWarner Losh #ifdef MMCCAM
2593a94a63f0SWarner Losh void
2594d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot)
2595a94a63f0SWarner Losh {
2596ab00a509SMarius Strobl 
2597505f6a0cSBjoern A. Zeeb 	if ((slot->devq = cam_simq_alloc(1)) == NULL)
2598a94a63f0SWarner Losh 		goto fail;
2599a94a63f0SWarner Losh 
2600a94a63f0SWarner Losh 	mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF);
2601aeb04e88SWarner Losh 	slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll,
2602da2f833fSBjoern A. Zeeb 	    "sdhci_slot", slot, device_get_unit(slot->bus),
2603a94a63f0SWarner Losh 	    &slot->sim_mtx, 1, 1, slot->devq);
2604a94a63f0SWarner Losh 
2605a94a63f0SWarner Losh 	if (slot->sim == NULL) {
2606a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2607a94a63f0SWarner Losh 		slot_printf(slot, "cannot allocate CAM SIM\n");
2608a94a63f0SWarner Losh 		goto fail;
2609a94a63f0SWarner Losh 	}
2610a94a63f0SWarner Losh 
2611a94a63f0SWarner Losh 	mtx_lock(&slot->sim_mtx);
2612a94a63f0SWarner Losh 	if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) {
2613505f6a0cSBjoern A. Zeeb 		slot_printf(slot, "cannot register SCSI pass-through bus\n");
2614a94a63f0SWarner Losh 		cam_sim_free(slot->sim, FALSE);
2615a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2616a94a63f0SWarner Losh 		mtx_unlock(&slot->sim_mtx);
2617a94a63f0SWarner Losh 		goto fail;
2618a94a63f0SWarner Losh 	}
2619a94a63f0SWarner Losh 	mtx_unlock(&slot->sim_mtx);
2620505f6a0cSBjoern A. Zeeb 
2621a94a63f0SWarner Losh 	/* End CAM-specific init */
2622a94a63f0SWarner Losh 	slot->card_present = 0;
2623a94a63f0SWarner Losh 	sdhci_card_task(slot, 0);
2624a94a63f0SWarner Losh 	return;
2625a94a63f0SWarner Losh 
2626a94a63f0SWarner Losh fail:
2627a94a63f0SWarner Losh 	if (slot->sim != NULL) {
2628a94a63f0SWarner Losh 		mtx_lock(&slot->sim_mtx);
2629a94a63f0SWarner Losh 		xpt_bus_deregister(cam_sim_path(slot->sim));
2630a94a63f0SWarner Losh 		cam_sim_free(slot->sim, FALSE);
2631a94a63f0SWarner Losh 		mtx_unlock(&slot->sim_mtx);
2632a94a63f0SWarner Losh 	}
2633a94a63f0SWarner Losh 
2634a94a63f0SWarner Losh 	if (slot->devq != NULL)
2635a94a63f0SWarner Losh 		cam_simq_free(slot->devq);
2636a94a63f0SWarner Losh }
2637a94a63f0SWarner Losh 
2638a94a63f0SWarner Losh void
2639a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb)
2640a94a63f0SWarner Losh {
2641a94a63f0SWarner Losh 	struct sdhci_slot *slot;
2642a94a63f0SWarner Losh 
2643a94a63f0SWarner Losh 	slot = cam_sim_softc(sim);
2644a94a63f0SWarner Losh 	if (slot == NULL) {
2645a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2646a94a63f0SWarner Losh 		xpt_done(ccb);
2647a94a63f0SWarner Losh 		return;
2648a94a63f0SWarner Losh 	}
2649a94a63f0SWarner Losh 
2650a94a63f0SWarner Losh 	mtx_assert(&slot->sim_mtx, MA_OWNED);
2651a94a63f0SWarner Losh 
2652a94a63f0SWarner Losh 	switch (ccb->ccb_h.func_code) {
2653a94a63f0SWarner Losh 	case XPT_PATH_INQ:
2654cd853791SKonstantin Belousov 		mmc_path_inq(&ccb->cpi, "Deglitch Networks", sim, maxphys);
2655a94a63f0SWarner Losh 		break;
26568c7cd14aSWarner Losh 
2657af2253f6SEmmanuel Vadot 	case XPT_MMC_GET_TRAN_SETTINGS:
2658a94a63f0SWarner Losh 	case XPT_GET_TRAN_SETTINGS:
2659a94a63f0SWarner Losh 	{
2660a94a63f0SWarner Losh 		struct ccb_trans_settings *cts = &ccb->cts;
26615d20e651SIlya Bakulin 		uint32_t max_data;
2662a94a63f0SWarner Losh 
2663a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2664a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n");
2665a94a63f0SWarner Losh 
2666a94a63f0SWarner Losh 		cts->protocol = PROTO_MMCSD;
2667a94a63f0SWarner Losh 		cts->protocol_version = 1;
2668a94a63f0SWarner Losh 		cts->transport = XPORT_MMCSD;
2669a94a63f0SWarner Losh 		cts->transport_version = 1;
2670a94a63f0SWarner Losh 		cts->xport_specific.valid = 0;
2671a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_ocr = slot->host.host_ocr;
2672a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_min = slot->host.f_min;
2673a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_f_max = slot->host.f_max;
2674a94a63f0SWarner Losh 		cts->proto_specific.mmc.host_caps = slot->host.caps;
26755d20e651SIlya Bakulin 		/*
26765d20e651SIlya Bakulin 		 * Re-tuning modes 1 and 2 restrict the maximum data length
26775d20e651SIlya Bakulin 		 * per read/write command to 4 MiB.
26785d20e651SIlya Bakulin 		 */
26795d20e651SIlya Bakulin 		if (slot->opt & SDHCI_TUNING_ENABLED &&
26805d20e651SIlya Bakulin 		    (slot->retune_mode == SDHCI_RETUNE_MODE_1 ||
26815d20e651SIlya Bakulin 		    slot->retune_mode == SDHCI_RETUNE_MODE_2)) {
26825d20e651SIlya Bakulin 			max_data = 4 * 1024 * 1024 / MMC_SECTOR_SIZE;
26835d20e651SIlya Bakulin 		} else {
26845d20e651SIlya Bakulin 			max_data = 65535;
26855d20e651SIlya Bakulin 		}
26865d20e651SIlya Bakulin 		cts->proto_specific.mmc.host_max_data = max_data;
26875d20e651SIlya Bakulin 
2688a94a63f0SWarner Losh 		memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios));
2689a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2690a94a63f0SWarner Losh 		break;
2691a94a63f0SWarner Losh 	}
2692af2253f6SEmmanuel Vadot 	case XPT_MMC_SET_TRAN_SETTINGS:
2693a94a63f0SWarner Losh 	case XPT_SET_TRAN_SETTINGS:
2694a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2695a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n");
2696a94a63f0SWarner Losh 		sdhci_cam_settran_settings(slot, ccb);
2697a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2698a94a63f0SWarner Losh 		break;
2699a94a63f0SWarner Losh 	case XPT_RESET_BUS:
2700a94a63f0SWarner Losh 		if (sdhci_debug > 1)
2701a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n");
2702a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_CMP;
2703a94a63f0SWarner Losh 		break;
2704a94a63f0SWarner Losh 	case XPT_MMC_IO:
2705a94a63f0SWarner Losh 		/*
2706a94a63f0SWarner Losh 		 * Here is the HW-dependent part of
2707a94a63f0SWarner Losh 		 * sending the command to the underlying h/w
2708a94a63f0SWarner Losh 		 * At some point in the future an interrupt comes.
2709a94a63f0SWarner Losh 		 * Then the request will be marked as completed.
2710a94a63f0SWarner Losh 		 */
2711aca38eabSMarius Strobl 		if (__predict_false(sdhci_debug > 1))
2712a94a63f0SWarner Losh 			slot_printf(slot, "Got XPT_MMC_IO\n");
2713a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INPROG;
2714a94a63f0SWarner Losh 
2715160799c6SWarner Losh 		sdhci_cam_request(cam_sim_softc(sim), ccb);
2716a94a63f0SWarner Losh 		return;
2717a94a63f0SWarner Losh 	default:
2718a94a63f0SWarner Losh 		ccb->ccb_h.status = CAM_REQ_INVALID;
2719a94a63f0SWarner Losh 		break;
2720a94a63f0SWarner Losh 	}
2721a94a63f0SWarner Losh 	xpt_done(ccb);
2722a94a63f0SWarner Losh 	return;
2723a94a63f0SWarner Losh }
2724a94a63f0SWarner Losh 
2725a94a63f0SWarner Losh void
2726a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim)
2727a94a63f0SWarner Losh {
2728a94a63f0SWarner Losh 	return;
2729a94a63f0SWarner Losh }
2730a94a63f0SWarner Losh 
27316dea80e6SMarius Strobl static int
2732ab00a509SMarius Strobl sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot,
2733ab00a509SMarius Strobl     int proposed_clock)
27346dea80e6SMarius Strobl {
2735a94a63f0SWarner Losh 	int max_clock, clock, i;
2736a94a63f0SWarner Losh 
2737a94a63f0SWarner Losh 	if (proposed_clock == 0)
2738a94a63f0SWarner Losh 		return 0;
2739a94a63f0SWarner Losh 	max_clock = slot->max_clk;
2740a94a63f0SWarner Losh 	clock = max_clock;
2741a94a63f0SWarner Losh 
2742a94a63f0SWarner Losh 	if (slot->version < SDHCI_SPEC_300) {
2743505f6a0cSBjoern A. Zeeb 		for (i = 0; i < SDHCI_200_MAX_DIVIDER; i <<= 1) {
2744a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2745a94a63f0SWarner Losh 				break;
2746a94a63f0SWarner Losh 			clock >>= 1;
2747a94a63f0SWarner Losh 		}
2748a94a63f0SWarner Losh 	} else {
2749505f6a0cSBjoern A. Zeeb 		for (i = 0; i < SDHCI_300_MAX_DIVIDER; i += 2) {
2750a94a63f0SWarner Losh 			if (clock <= proposed_clock)
2751a94a63f0SWarner Losh 				break;
2752a94a63f0SWarner Losh 			clock = max_clock / (i + 2);
2753a94a63f0SWarner Losh 		}
2754a94a63f0SWarner Losh 	}
2755a94a63f0SWarner Losh 	return clock;
2756a94a63f0SWarner Losh }
2757a94a63f0SWarner Losh 
2758ab00a509SMarius Strobl static int
2759a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb)
2760a94a63f0SWarner Losh {
2761a94a63f0SWarner Losh 	struct mmc_ios *ios;
2762ab00a509SMarius Strobl 	const struct mmc_ios *new_ios;
2763ab00a509SMarius Strobl 	const struct ccb_trans_settings_mmc *cts;
2764a94a63f0SWarner Losh 
2765a94a63f0SWarner Losh 	ios = &slot->host.ios;
2766a94a63f0SWarner Losh 	cts = &ccb->cts.proto_specific.mmc;
2767a94a63f0SWarner Losh 	new_ios = &cts->ios;
2768a94a63f0SWarner Losh 
2769a94a63f0SWarner Losh 	/* Update only requested fields */
2770a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CLK) {
2771a94a63f0SWarner Losh 		ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock);
2772b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2773a94a63f0SWarner Losh 			slot_printf(slot, "Clock => %d\n", ios->clock);
2774a94a63f0SWarner Losh 	}
2775a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_VDD) {
2776a94a63f0SWarner Losh 		ios->vdd = new_ios->vdd;
2777b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2778a94a63f0SWarner Losh 			slot_printf(slot, "VDD => %d\n", ios->vdd);
2779a94a63f0SWarner Losh 	}
2780a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_CS) {
2781a94a63f0SWarner Losh 		ios->chip_select = new_ios->chip_select;
2782b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2783a94a63f0SWarner Losh 			slot_printf(slot, "CS => %d\n", ios->chip_select);
2784a94a63f0SWarner Losh 	}
2785a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BW) {
2786a94a63f0SWarner Losh 		ios->bus_width = new_ios->bus_width;
2787b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2788a94a63f0SWarner Losh 			slot_printf(slot, "Bus width => %d\n", ios->bus_width);
2789a94a63f0SWarner Losh 	}
2790a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_PM) {
2791a94a63f0SWarner Losh 		ios->power_mode = new_ios->power_mode;
2792b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2793a94a63f0SWarner Losh 			slot_printf(slot, "Power mode => %d\n", ios->power_mode);
2794a94a63f0SWarner Losh 	}
2795a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BT) {
2796a94a63f0SWarner Losh 		ios->timing = new_ios->timing;
2797b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2798a94a63f0SWarner Losh 			slot_printf(slot, "Timing => %d\n", ios->timing);
2799a94a63f0SWarner Losh 	}
2800a94a63f0SWarner Losh 	if (cts->ios_valid & MMC_BM) {
2801a94a63f0SWarner Losh 		ios->bus_mode = new_ios->bus_mode;
2802b18f2ef4SEmmanuel Vadot 		if (sdhci_debug > 1)
2803a94a63f0SWarner Losh 			slot_printf(slot, "Bus mode => %d\n", ios->bus_mode);
2804a94a63f0SWarner Losh 	}
28051a96c143SEmmanuel Vadot 	if (cts->ios_valid & MMC_VCCQ) {
28061a96c143SEmmanuel Vadot 		ios->vccq = new_ios->vccq;
28071a96c143SEmmanuel Vadot 		if (sdhci_debug > 1)
28081a96c143SEmmanuel Vadot 			slot_printf(slot, "VCCQ => %d\n", ios->vccq);
28091a96c143SEmmanuel Vadot 	}
2810a94a63f0SWarner Losh 
2811a94a63f0SWarner Losh 	/* XXX Provide a way to call a chip-specific IOS update, required for TI */
2812a94a63f0SWarner Losh 	return (sdhci_cam_update_ios(slot));
2813a94a63f0SWarner Losh }
2814a94a63f0SWarner Losh 
2815ab00a509SMarius Strobl static int
2816a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot)
2817a94a63f0SWarner Losh {
2818a94a63f0SWarner Losh 	struct mmc_ios *ios = &slot->host.ios;
2819a94a63f0SWarner Losh 
2820b18f2ef4SEmmanuel Vadot 	if (sdhci_debug > 1)
2821a94a63f0SWarner Losh 		slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n",
2822a94a63f0SWarner Losh 		    __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing);
2823a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2824a94a63f0SWarner Losh 	/* Do full reset on bus power down to clear from any state. */
2825a94a63f0SWarner Losh 	if (ios->power_mode == power_off) {
2826a94a63f0SWarner Losh 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
2827a94a63f0SWarner Losh 		sdhci_init(slot);
2828a94a63f0SWarner Losh 	}
2829a94a63f0SWarner Losh 	/* Configure the bus. */
2830a94a63f0SWarner Losh 	sdhci_set_clock(slot, ios->clock);
2831a94a63f0SWarner Losh 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
2832a94a63f0SWarner Losh 	if (ios->bus_width == bus_width_8) {
2833a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
2834a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2835a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_4) {
2836a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2837a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
2838a94a63f0SWarner Losh 	} else if (ios->bus_width == bus_width_1) {
2839a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
2840a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
2841a94a63f0SWarner Losh 	} else {
2842a94a63f0SWarner Losh 		panic("Invalid bus width: %d", ios->bus_width);
2843a94a63f0SWarner Losh 	}
2844a94a63f0SWarner Losh 	if (ios->timing == bus_timing_hs &&
2845a94a63f0SWarner Losh 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
2846a94a63f0SWarner Losh 		slot->hostctrl |= SDHCI_CTRL_HISPD;
2847a94a63f0SWarner Losh 	else
2848a94a63f0SWarner Losh 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
2849a94a63f0SWarner Losh 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
2850a94a63f0SWarner Losh 	/* Some controllers like reset after bus changes. */
2851a94a63f0SWarner Losh 	if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
2852a94a63f0SWarner Losh 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2853a94a63f0SWarner Losh 
2854a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2855a94a63f0SWarner Losh 	return (0);
2856a94a63f0SWarner Losh }
2857a94a63f0SWarner Losh 
2858ab00a509SMarius Strobl static int
2859a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb)
2860a94a63f0SWarner Losh {
2861ab00a509SMarius Strobl 	const struct ccb_mmcio *mmcio;
2862a94a63f0SWarner Losh 
2863a94a63f0SWarner Losh 	mmcio = &ccb->mmcio;
2864a94a63f0SWarner Losh 
2865a94a63f0SWarner Losh 	SDHCI_LOCK(slot);
2866a94a63f0SWarner Losh /*	if (slot->req != NULL) {
2867a94a63f0SWarner Losh 		SDHCI_UNLOCK(slot);
2868a94a63f0SWarner Losh 		return (EBUSY);
2869a94a63f0SWarner Losh 	}
2870a94a63f0SWarner Losh */
2871aca38eabSMarius Strobl 	if (__predict_false(sdhci_debug > 1)) {
28725d5ae066SIlya Bakulin 		slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x "
28735d5ae066SIlya Bakulin 		    "blksz=%zu blkcnt=%zu\n",
2874a94a63f0SWarner Losh 		    mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags,
2875a94a63f0SWarner Losh 		    mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0,
28765d5ae066SIlya Bakulin 		    mmcio->cmd.data != NULL ? mmcio->cmd.data->flags : 0,
28775d5ae066SIlya Bakulin 		    mmcio->cmd.data != NULL ? mmcio->cmd.data->block_size : 0,
28785d5ae066SIlya Bakulin 		    mmcio->cmd.data != NULL ? mmcio->cmd.data->block_count : 0);
2879a94a63f0SWarner Losh 	}
2880a94a63f0SWarner Losh 	if (mmcio->cmd.data != NULL) {
2881a94a63f0SWarner Losh 		if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0)
2882a94a63f0SWarner Losh 			panic("data->len = %d, data->flags = %d -- something is b0rked",
2883a94a63f0SWarner Losh 			    (int)mmcio->cmd.data->len, mmcio->cmd.data->flags);
2884a94a63f0SWarner Losh 	}
2885a94a63f0SWarner Losh 	slot->ccb = ccb;
2886a94a63f0SWarner Losh 	slot->flags = 0;
2887a94a63f0SWarner Losh 	sdhci_start(slot);
2888a94a63f0SWarner Losh 	SDHCI_UNLOCK(slot);
2889a94a63f0SWarner Losh 	if (dumping) {
2890a94a63f0SWarner Losh 		while (slot->ccb != NULL) {
2891a94a63f0SWarner Losh 			sdhci_generic_intr(slot);
2892a94a63f0SWarner Losh 			DELAY(10);
2893a94a63f0SWarner Losh 		}
2894a94a63f0SWarner Losh 	}
2895a94a63f0SWarner Losh 	return (0);
2896a94a63f0SWarner Losh }
289715c440e1SWarner Losh #endif /* MMCCAM */
2898a94a63f0SWarner Losh 
2899ab00a509SMarius Strobl MODULE_VERSION(sdhci, SDHCI_VERSION);
2900