xref: /freebsd/sys/arm/allwinner/aw_mmc.c (revision 020df509043bf3e97f6edf69ad531098c0a7c08c)
1b5be541fSEmmanuel Vadot /*-
2b091392eSEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3b091392eSEmmanuel Vadot  *
4b091392eSEmmanuel Vadot  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
5b5be541fSEmmanuel Vadot  * Copyright (c) 2013 Alexander Fedorov
6b5be541fSEmmanuel Vadot  * All rights reserved.
7b5be541fSEmmanuel Vadot  *
8b5be541fSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
9b5be541fSEmmanuel Vadot  * modification, are permitted provided that the following conditions
10b5be541fSEmmanuel Vadot  * are met:
11b5be541fSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
12b5be541fSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
13b5be541fSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
14b5be541fSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
15b5be541fSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
16b5be541fSEmmanuel Vadot  *
17b5be541fSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18b5be541fSEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19b5be541fSEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20b5be541fSEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21b5be541fSEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22b5be541fSEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23b5be541fSEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24b5be541fSEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25b5be541fSEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26b5be541fSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27b5be541fSEmmanuel Vadot  * SUCH DAMAGE.
28b5be541fSEmmanuel Vadot  */
29b5be541fSEmmanuel Vadot 
30b5be541fSEmmanuel Vadot #include <sys/cdefs.h>
31b5be541fSEmmanuel Vadot __FBSDID("$FreeBSD$");
32b5be541fSEmmanuel Vadot 
33b5be541fSEmmanuel Vadot #include <sys/param.h>
34b5be541fSEmmanuel Vadot #include <sys/systm.h>
35b5be541fSEmmanuel Vadot #include <sys/bus.h>
36b5be541fSEmmanuel Vadot #include <sys/kernel.h>
37b5be541fSEmmanuel Vadot #include <sys/lock.h>
38b5be541fSEmmanuel Vadot #include <sys/malloc.h>
39b5be541fSEmmanuel Vadot #include <sys/module.h>
40b5be541fSEmmanuel Vadot #include <sys/mutex.h>
41b5be541fSEmmanuel Vadot #include <sys/resource.h>
42b5be541fSEmmanuel Vadot #include <sys/rman.h>
43b5be541fSEmmanuel Vadot #include <sys/sysctl.h>
449ed83210SEmmanuel Vadot #include <sys/queue.h>
459ed83210SEmmanuel Vadot #include <sys/taskqueue.h>
46b5be541fSEmmanuel Vadot 
47b5be541fSEmmanuel Vadot #include <machine/bus.h>
48b5be541fSEmmanuel Vadot 
49b5be541fSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
50b5be541fSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
51b5be541fSEmmanuel Vadot 
52b5be541fSEmmanuel Vadot #include <dev/mmc/bridge.h>
53b5be541fSEmmanuel Vadot #include <dev/mmc/mmcbrvar.h>
549ed83210SEmmanuel Vadot #include <dev/mmc/mmc_fdt_helpers.h>
55b5be541fSEmmanuel Vadot 
56b5be541fSEmmanuel Vadot #include <arm/allwinner/aw_mmc.h>
57b5be541fSEmmanuel Vadot #include <dev/extres/clk/clk.h>
58b5be541fSEmmanuel Vadot #include <dev/extres/hwreset/hwreset.h>
59ce0618beSEmmanuel Vadot #include <dev/extres/regulator/regulator.h>
60b5be541fSEmmanuel Vadot 
615e03278fSIlya Bakulin #include "opt_mmccam.h"
625e03278fSIlya Bakulin 
635e03278fSIlya Bakulin #ifdef MMCCAM
645e03278fSIlya Bakulin #include <cam/cam.h>
655e03278fSIlya Bakulin #include <cam/cam_ccb.h>
665e03278fSIlya Bakulin #include <cam/cam_debug.h>
675e03278fSIlya Bakulin #include <cam/cam_sim.h>
685e03278fSIlya Bakulin #include <cam/cam_xpt_sim.h>
695e03278fSIlya Bakulin #endif
705e03278fSIlya Bakulin 
71b5be541fSEmmanuel Vadot #define	AW_MMC_MEMRES		0
72b5be541fSEmmanuel Vadot #define	AW_MMC_IRQRES		1
73b5be541fSEmmanuel Vadot #define	AW_MMC_RESSZ		2
74c39ea909SEmmanuel Vadot #define	AW_MMC_DMA_SEGS		(PAGE_SIZE / sizeof(struct aw_mmc_dma_desc))
75c39ea909SEmmanuel Vadot #define	AW_MMC_DMA_DESC_SIZE	(sizeof(struct aw_mmc_dma_desc) * AW_MMC_DMA_SEGS)
76b5be541fSEmmanuel Vadot #define	AW_MMC_DMA_FTRGLEVEL	0x20070008
77c39ea909SEmmanuel Vadot 
78b5be541fSEmmanuel Vadot #define	AW_MMC_RESET_RETRY	1000
79b5be541fSEmmanuel Vadot 
80b5be541fSEmmanuel Vadot #define	CARD_ID_FREQUENCY	400000
81b5be541fSEmmanuel Vadot 
82ce0618beSEmmanuel Vadot struct aw_mmc_conf {
83ce0618beSEmmanuel Vadot 	uint32_t	dma_xferlen;
84ce0618beSEmmanuel Vadot 	bool		mask_data0;
85ce0618beSEmmanuel Vadot 	bool		can_calibrate;
86ce0618beSEmmanuel Vadot 	bool		new_timing;
87ce0618beSEmmanuel Vadot };
88ce0618beSEmmanuel Vadot 
89ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a10_mmc_conf = {
90ce0618beSEmmanuel Vadot 	.dma_xferlen = 0x2000,
91ce0618beSEmmanuel Vadot };
92ce0618beSEmmanuel Vadot 
93ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a13_mmc_conf = {
94ce0618beSEmmanuel Vadot 	.dma_xferlen = 0x10000,
95ce0618beSEmmanuel Vadot };
96ce0618beSEmmanuel Vadot 
97ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_mmc_conf = {
98ce0618beSEmmanuel Vadot 	.dma_xferlen = 0x10000,
99ce0618beSEmmanuel Vadot 	.mask_data0 = true,
100ce0618beSEmmanuel Vadot 	.can_calibrate = true,
101ce0618beSEmmanuel Vadot 	.new_timing = true,
102ce0618beSEmmanuel Vadot };
103ce0618beSEmmanuel Vadot 
104ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_emmc_conf = {
105ce0618beSEmmanuel Vadot 	.dma_xferlen = 0x2000,
106ce0618beSEmmanuel Vadot 	.can_calibrate = true,
107ce0618beSEmmanuel Vadot };
108ce0618beSEmmanuel Vadot 
109b5be541fSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
110ce0618beSEmmanuel Vadot 	{"allwinner,sun4i-a10-mmc", (uintptr_t)&a10_mmc_conf},
111ce0618beSEmmanuel Vadot 	{"allwinner,sun5i-a13-mmc", (uintptr_t)&a13_mmc_conf},
112ce0618beSEmmanuel Vadot 	{"allwinner,sun7i-a20-mmc", (uintptr_t)&a13_mmc_conf},
113ce0618beSEmmanuel Vadot 	{"allwinner,sun50i-a64-mmc", (uintptr_t)&a64_mmc_conf},
114ce0618beSEmmanuel Vadot 	{"allwinner,sun50i-a64-emmc", (uintptr_t)&a64_emmc_conf},
115b5be541fSEmmanuel Vadot 	{NULL,             0}
116b5be541fSEmmanuel Vadot };
117b5be541fSEmmanuel Vadot 
118b5be541fSEmmanuel Vadot struct aw_mmc_softc {
119b5be541fSEmmanuel Vadot 	device_t		aw_dev;
120b5be541fSEmmanuel Vadot 	clk_t			aw_clk_ahb;
121b5be541fSEmmanuel Vadot 	clk_t			aw_clk_mmc;
122b5be541fSEmmanuel Vadot 	hwreset_t		aw_rst_ahb;
123b5be541fSEmmanuel Vadot 	int			aw_bus_busy;
124b5be541fSEmmanuel Vadot 	int			aw_resid;
125b5be541fSEmmanuel Vadot 	int			aw_timeout;
126b5be541fSEmmanuel Vadot 	struct callout		aw_timeoutc;
127b5be541fSEmmanuel Vadot 	struct mmc_host		aw_host;
1289ed83210SEmmanuel Vadot 	struct mmc_fdt_helper	mmc_helper;
1295e03278fSIlya Bakulin #ifdef MMCCAM
1305e03278fSIlya Bakulin 	union ccb *		ccb;
1315e03278fSIlya Bakulin 	struct cam_devq *	devq;
1325e03278fSIlya Bakulin 	struct cam_sim * 	sim;
1335e03278fSIlya Bakulin 	struct mtx		sim_mtx;
1345e03278fSIlya Bakulin #else
135b5be541fSEmmanuel Vadot 	struct mmc_request *	aw_req;
1365e03278fSIlya Bakulin #endif
137b5be541fSEmmanuel Vadot 	struct mtx		aw_mtx;
138b5be541fSEmmanuel Vadot 	struct resource *	aw_res[AW_MMC_RESSZ];
139ce0618beSEmmanuel Vadot 	struct aw_mmc_conf *	aw_mmc_conf;
140b5be541fSEmmanuel Vadot 	uint32_t		aw_intr;
141b5be541fSEmmanuel Vadot 	uint32_t		aw_intr_wait;
142b5be541fSEmmanuel Vadot 	void *			aw_intrhand;
1430f7a6420SEmmanuel Vadot 	unsigned int		aw_clock;
1449ed83210SEmmanuel Vadot 	device_t		child;
145b5be541fSEmmanuel Vadot 
146b5be541fSEmmanuel Vadot 	/* Fields required for DMA access. */
147b5be541fSEmmanuel Vadot 	bus_addr_t	  	aw_dma_desc_phys;
148b5be541fSEmmanuel Vadot 	bus_dmamap_t		aw_dma_map;
149b5be541fSEmmanuel Vadot 	bus_dma_tag_t 		aw_dma_tag;
150b5be541fSEmmanuel Vadot 	void * 			aw_dma_desc;
151b5be541fSEmmanuel Vadot 	bus_dmamap_t		aw_dma_buf_map;
152b5be541fSEmmanuel Vadot 	bus_dma_tag_t		aw_dma_buf_tag;
153b5be541fSEmmanuel Vadot 	int			aw_dma_map_err;
154b5be541fSEmmanuel Vadot };
155b5be541fSEmmanuel Vadot 
156b5be541fSEmmanuel Vadot static struct resource_spec aw_mmc_res_spec[] = {
157b5be541fSEmmanuel Vadot 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
158b5be541fSEmmanuel Vadot 	{ SYS_RES_IRQ,		0,	RF_ACTIVE | RF_SHAREABLE },
159b5be541fSEmmanuel Vadot 	{ -1,			0,	0 }
160b5be541fSEmmanuel Vadot };
161b5be541fSEmmanuel Vadot 
162b5be541fSEmmanuel Vadot static int aw_mmc_probe(device_t);
163b5be541fSEmmanuel Vadot static int aw_mmc_attach(device_t);
164b5be541fSEmmanuel Vadot static int aw_mmc_detach(device_t);
165b5be541fSEmmanuel Vadot static int aw_mmc_setup_dma(struct aw_mmc_softc *);
166cbba9a7bSEmmanuel Vadot static void aw_mmc_teardown_dma(struct aw_mmc_softc *sc);
167b5be541fSEmmanuel Vadot static int aw_mmc_reset(struct aw_mmc_softc *);
16835a18619SEmmanuel Vadot static int aw_mmc_init(struct aw_mmc_softc *);
169b5be541fSEmmanuel Vadot static void aw_mmc_intr(void *);
170b5be541fSEmmanuel Vadot static int aw_mmc_update_clock(struct aw_mmc_softc *, uint32_t);
1719ed83210SEmmanuel Vadot static void aw_mmc_helper_cd_handler(device_t, bool);
172b5be541fSEmmanuel Vadot 
1735e03278fSIlya Bakulin static void aw_mmc_print_error(uint32_t);
174b5be541fSEmmanuel Vadot static int aw_mmc_update_ios(device_t, device_t);
175b5be541fSEmmanuel Vadot static int aw_mmc_request(device_t, device_t, struct mmc_request *);
176b5be541fSEmmanuel Vadot static int aw_mmc_get_ro(device_t, device_t);
177b5be541fSEmmanuel Vadot static int aw_mmc_acquire_host(device_t, device_t);
178b5be541fSEmmanuel Vadot static int aw_mmc_release_host(device_t, device_t);
1795e03278fSIlya Bakulin #ifdef MMCCAM
1805e03278fSIlya Bakulin static void aw_mmc_cam_action(struct cam_sim *, union ccb *);
1815e03278fSIlya Bakulin static void aw_mmc_cam_poll(struct cam_sim *);
1825e03278fSIlya Bakulin static int aw_mmc_cam_settran_settings(struct aw_mmc_softc *, union ccb *);
1835e03278fSIlya Bakulin static int aw_mmc_cam_request(struct aw_mmc_softc *, union ccb *);
1845e03278fSIlya Bakulin static void aw_mmc_cam_handle_mmcio(struct cam_sim *, union ccb *);
1855e03278fSIlya Bakulin #endif
186b5be541fSEmmanuel Vadot 
187b5be541fSEmmanuel Vadot #define	AW_MMC_LOCK(_sc)	mtx_lock(&(_sc)->aw_mtx)
188b5be541fSEmmanuel Vadot #define	AW_MMC_UNLOCK(_sc)	mtx_unlock(&(_sc)->aw_mtx)
189b5be541fSEmmanuel Vadot #define	AW_MMC_READ_4(_sc, _reg)					\
190b5be541fSEmmanuel Vadot 	bus_read_4((_sc)->aw_res[AW_MMC_MEMRES], _reg)
191b5be541fSEmmanuel Vadot #define	AW_MMC_WRITE_4(_sc, _reg, _value)				\
192b5be541fSEmmanuel Vadot 	bus_write_4((_sc)->aw_res[AW_MMC_MEMRES], _reg, _value)
193b5be541fSEmmanuel Vadot 
194*020df509SEmmanuel Vadot SYSCTL_NODE(_hw, OID_AUTO, aw_mmc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
195*020df509SEmmanuel Vadot     "aw_mmc driver");
196*020df509SEmmanuel Vadot 
197*020df509SEmmanuel Vadot static int aw_mmc_debug = 0;
198*020df509SEmmanuel Vadot SYSCTL_INT(_hw_aw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &aw_mmc_debug, 0,
199*020df509SEmmanuel Vadot     "Debug level bit0=card changes bit1=ios changes, bit2=interrupts, bit3=commands");
200*020df509SEmmanuel Vadot #define	AW_MMC_DEBUG_CARD	0x1
201*020df509SEmmanuel Vadot #define	AW_MMC_DEBUG_IOS	0x2
202*020df509SEmmanuel Vadot #define	AW_MMC_DEBUG_INT	0x4
203*020df509SEmmanuel Vadot #define	AW_MMC_DEBUG_CMD	0x8
204*020df509SEmmanuel Vadot 
2055e03278fSIlya Bakulin #ifdef MMCCAM
2065e03278fSIlya Bakulin static void
2075e03278fSIlya Bakulin aw_mmc_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb)
2085e03278fSIlya Bakulin {
2095e03278fSIlya Bakulin 	struct aw_mmc_softc *sc;
2105e03278fSIlya Bakulin 
2115e03278fSIlya Bakulin 	sc = cam_sim_softc(sim);
2125e03278fSIlya Bakulin 
2135e03278fSIlya Bakulin 	aw_mmc_cam_request(sc, ccb);
2145e03278fSIlya Bakulin }
2155e03278fSIlya Bakulin 
2165e03278fSIlya Bakulin static void
2175e03278fSIlya Bakulin aw_mmc_cam_action(struct cam_sim *sim, union ccb *ccb)
2185e03278fSIlya Bakulin {
2195e03278fSIlya Bakulin 	struct aw_mmc_softc *sc;
2205e03278fSIlya Bakulin 
2215e03278fSIlya Bakulin 	sc = cam_sim_softc(sim);
2225e03278fSIlya Bakulin 	if (sc == NULL) {
2235e03278fSIlya Bakulin 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2245e03278fSIlya Bakulin 		xpt_done(ccb);
2255e03278fSIlya Bakulin 		return;
2265e03278fSIlya Bakulin 	}
2275e03278fSIlya Bakulin 
2285e03278fSIlya Bakulin 	mtx_assert(&sc->sim_mtx, MA_OWNED);
2295e03278fSIlya Bakulin 
2305e03278fSIlya Bakulin 	switch (ccb->ccb_h.func_code) {
2315e03278fSIlya Bakulin 	case XPT_PATH_INQ:
2328c7cd14aSWarner Losh 		mmc_path_inq(&ccb->cpi, "Deglitch Networks", sim,
2338c7cd14aSWarner Losh 		    (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS) /
2348c7cd14aSWarner Losh 		    MMC_SECTOR_SIZE);
2355e03278fSIlya Bakulin 		break;
2368c7cd14aSWarner Losh 
2375e03278fSIlya Bakulin 	case XPT_GET_TRAN_SETTINGS:
2385e03278fSIlya Bakulin 	{
2395e03278fSIlya Bakulin 		struct ccb_trans_settings *cts = &ccb->cts;
2405e03278fSIlya Bakulin 
241*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS))
2425e03278fSIlya Bakulin 			device_printf(sc->aw_dev, "Got XPT_GET_TRAN_SETTINGS\n");
2435e03278fSIlya Bakulin 
2445e03278fSIlya Bakulin 		cts->protocol = PROTO_MMCSD;
2455e03278fSIlya Bakulin 		cts->protocol_version = 1;
2465e03278fSIlya Bakulin 		cts->transport = XPORT_MMCSD;
2475e03278fSIlya Bakulin 		cts->transport_version = 1;
2485e03278fSIlya Bakulin 		cts->xport_specific.valid = 0;
2495e03278fSIlya Bakulin 		cts->proto_specific.mmc.host_ocr = sc->aw_host.host_ocr;
2505e03278fSIlya Bakulin 		cts->proto_specific.mmc.host_f_min = sc->aw_host.f_min;
2515e03278fSIlya Bakulin 		cts->proto_specific.mmc.host_f_max = sc->aw_host.f_max;
2525e03278fSIlya Bakulin 		cts->proto_specific.mmc.host_caps = sc->aw_host.caps;
2535d20e651SIlya Bakulin 		cts->proto_specific.mmc.host_max_data = (sc->aw_mmc_conf->dma_xferlen *
2545d20e651SIlya Bakulin 		    AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE;
2555e03278fSIlya Bakulin 		memcpy(&cts->proto_specific.mmc.ios, &sc->aw_host.ios, sizeof(struct mmc_ios));
2565e03278fSIlya Bakulin 		ccb->ccb_h.status = CAM_REQ_CMP;
2575e03278fSIlya Bakulin 		break;
2585e03278fSIlya Bakulin 	}
2595e03278fSIlya Bakulin 	case XPT_SET_TRAN_SETTINGS:
2605e03278fSIlya Bakulin 	{
261*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS))
2625e03278fSIlya Bakulin 			device_printf(sc->aw_dev, "Got XPT_SET_TRAN_SETTINGS\n");
2635e03278fSIlya Bakulin 		aw_mmc_cam_settran_settings(sc, ccb);
2645e03278fSIlya Bakulin 		ccb->ccb_h.status = CAM_REQ_CMP;
2655e03278fSIlya Bakulin 		break;
2665e03278fSIlya Bakulin 	}
2675e03278fSIlya Bakulin 	case XPT_RESET_BUS:
268*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS))
2695e03278fSIlya Bakulin 			device_printf(sc->aw_dev, "Got XPT_RESET_BUS, ACK it...\n");
2705e03278fSIlya Bakulin 		ccb->ccb_h.status = CAM_REQ_CMP;
2715e03278fSIlya Bakulin 		break;
2725e03278fSIlya Bakulin 	case XPT_MMC_IO:
2735e03278fSIlya Bakulin 		/*
2745e03278fSIlya Bakulin 		 * Here is the HW-dependent part of
2755e03278fSIlya Bakulin 		 * sending the command to the underlying h/w
2765e03278fSIlya Bakulin 		 * At some point in the future an interrupt comes.
2775e03278fSIlya Bakulin 		 * Then the request will be marked as completed.
2785e03278fSIlya Bakulin 		 */
2795e03278fSIlya Bakulin 		ccb->ccb_h.status = CAM_REQ_INPROG;
2805e03278fSIlya Bakulin 
2815e03278fSIlya Bakulin 		aw_mmc_cam_handle_mmcio(sim, ccb);
2825e03278fSIlya Bakulin 		return;
2835e03278fSIlya Bakulin 		/* NOTREACHED */
2845e03278fSIlya Bakulin 		break;
2855e03278fSIlya Bakulin 	default:
2865e03278fSIlya Bakulin 		ccb->ccb_h.status = CAM_REQ_INVALID;
2875e03278fSIlya Bakulin 		break;
2885e03278fSIlya Bakulin 	}
2895e03278fSIlya Bakulin 	xpt_done(ccb);
2905e03278fSIlya Bakulin 	return;
2915e03278fSIlya Bakulin }
2925e03278fSIlya Bakulin 
2935e03278fSIlya Bakulin static void
2945e03278fSIlya Bakulin aw_mmc_cam_poll(struct cam_sim *sim)
2955e03278fSIlya Bakulin {
2965e03278fSIlya Bakulin 	return;
2975e03278fSIlya Bakulin }
2985e03278fSIlya Bakulin 
2995e03278fSIlya Bakulin static int
3005e03278fSIlya Bakulin aw_mmc_cam_settran_settings(struct aw_mmc_softc *sc, union ccb *ccb)
3015e03278fSIlya Bakulin {
3025e03278fSIlya Bakulin 	struct mmc_ios *ios;
3035e03278fSIlya Bakulin 	struct mmc_ios *new_ios;
3045e03278fSIlya Bakulin 	struct ccb_trans_settings_mmc *cts;
3055e03278fSIlya Bakulin 
3065e03278fSIlya Bakulin 	ios = &sc->aw_host.ios;
3075e03278fSIlya Bakulin 
3085e03278fSIlya Bakulin 	cts = &ccb->cts.proto_specific.mmc;
3095e03278fSIlya Bakulin 	new_ios = &cts->ios;
3105e03278fSIlya Bakulin 
3115e03278fSIlya Bakulin 	/* Update only requested fields */
3125e03278fSIlya Bakulin 	if (cts->ios_valid & MMC_CLK) {
3135e03278fSIlya Bakulin 		ios->clock = new_ios->clock;
314*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS))
3155e03278fSIlya Bakulin 			device_printf(sc->aw_dev, "Clock => %d\n", ios->clock);
3165e03278fSIlya Bakulin 	}
3175e03278fSIlya Bakulin 	if (cts->ios_valid & MMC_VDD) {
3185e03278fSIlya Bakulin 		ios->vdd = new_ios->vdd;
319*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS))
3205e03278fSIlya Bakulin 			device_printf(sc->aw_dev, "VDD => %d\n", ios->vdd);
3215e03278fSIlya Bakulin 	}
3225e03278fSIlya Bakulin 	if (cts->ios_valid & MMC_CS) {
3235e03278fSIlya Bakulin 		ios->chip_select = new_ios->chip_select;
324*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS))
3255e03278fSIlya Bakulin 			device_printf(sc->aw_dev, "CS => %d\n", ios->chip_select);
3265e03278fSIlya Bakulin 	}
3275e03278fSIlya Bakulin 	if (cts->ios_valid & MMC_BW) {
3285e03278fSIlya Bakulin 		ios->bus_width = new_ios->bus_width;
329*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS))
3305e03278fSIlya Bakulin 			device_printf(sc->aw_dev, "Bus width => %d\n", ios->bus_width);
3315e03278fSIlya Bakulin 	}
3325e03278fSIlya Bakulin 	if (cts->ios_valid & MMC_PM) {
3335e03278fSIlya Bakulin 		ios->power_mode = new_ios->power_mode;
334*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS))
3355e03278fSIlya Bakulin 			device_printf(sc->aw_dev, "Power mode => %d\n", ios->power_mode);
3365e03278fSIlya Bakulin 	}
3375e03278fSIlya Bakulin 	if (cts->ios_valid & MMC_BT) {
3385e03278fSIlya Bakulin 		ios->timing = new_ios->timing;
339*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS))
3405e03278fSIlya Bakulin 			device_printf(sc->aw_dev, "Timing => %d\n", ios->timing);
3415e03278fSIlya Bakulin 	}
3425e03278fSIlya Bakulin 	if (cts->ios_valid & MMC_BM) {
3435e03278fSIlya Bakulin 		ios->bus_mode = new_ios->bus_mode;
344*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS))
3455e03278fSIlya Bakulin 			device_printf(sc->aw_dev, "Bus mode => %d\n", ios->bus_mode);
3465e03278fSIlya Bakulin 	}
3475e03278fSIlya Bakulin 
3485e03278fSIlya Bakulin 	return (aw_mmc_update_ios(sc->aw_dev, NULL));
3495e03278fSIlya Bakulin }
3505e03278fSIlya Bakulin 
3515e03278fSIlya Bakulin static int
3525e03278fSIlya Bakulin aw_mmc_cam_request(struct aw_mmc_softc *sc, union ccb *ccb)
3535e03278fSIlya Bakulin {
3545e03278fSIlya Bakulin 	struct ccb_mmcio *mmcio;
3555e03278fSIlya Bakulin 
3565e03278fSIlya Bakulin 	mmcio = &ccb->mmcio;
3575e03278fSIlya Bakulin 
3585e03278fSIlya Bakulin 	AW_MMC_LOCK(sc);
3595e03278fSIlya Bakulin 
360*020df509SEmmanuel Vadot 	if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CMD)) {
3615e03278fSIlya Bakulin 		device_printf(sc->aw_dev, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
3625e03278fSIlya Bakulin 			    mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags,
3635e03278fSIlya Bakulin 			    mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0,
3645e03278fSIlya Bakulin 			    mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0);
3655e03278fSIlya Bakulin 	}
3665e03278fSIlya Bakulin 	if (mmcio->cmd.data != NULL) {
3675e03278fSIlya Bakulin 		if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0)
3685e03278fSIlya Bakulin 			panic("data->len = %d, data->flags = %d -- something is b0rked",
3695e03278fSIlya Bakulin 			      (int)mmcio->cmd.data->len, mmcio->cmd.data->flags);
3705e03278fSIlya Bakulin 	}
3715e03278fSIlya Bakulin 	if (sc->ccb != NULL) {
3725e03278fSIlya Bakulin 		device_printf(sc->aw_dev, "Controller still has an active command\n");
3735e03278fSIlya Bakulin 		return (EBUSY);
3745e03278fSIlya Bakulin 	}
3755e03278fSIlya Bakulin 	sc->ccb = ccb;
3765e03278fSIlya Bakulin 	/* aw_mmc_request locks again */
3775e03278fSIlya Bakulin 	AW_MMC_UNLOCK(sc);
3785e03278fSIlya Bakulin 	aw_mmc_request(sc->aw_dev, NULL, NULL);
3795e03278fSIlya Bakulin 
3805e03278fSIlya Bakulin 	return (0);
3815e03278fSIlya Bakulin }
3825e03278fSIlya Bakulin #endif /* MMCCAM */
3835e03278fSIlya Bakulin 
3849ed83210SEmmanuel Vadot static void
3859ed83210SEmmanuel Vadot aw_mmc_helper_cd_handler(device_t dev, bool present)
3869ed83210SEmmanuel Vadot {
3879ed83210SEmmanuel Vadot 	struct aw_mmc_softc *sc;
3889ed83210SEmmanuel Vadot 
3899ed83210SEmmanuel Vadot 	sc = device_get_softc(dev);
3909bca4667SEmmanuel Vadot #ifdef MMCCAM
3919bca4667SEmmanuel Vadot 	mmccam_start_discovery(sc->sim);
3929bca4667SEmmanuel Vadot #else
3939ed83210SEmmanuel Vadot 	AW_MMC_LOCK(sc);
3949ed83210SEmmanuel Vadot 	if (present) {
3959ed83210SEmmanuel Vadot 		if (sc->child == NULL) {
396*020df509SEmmanuel Vadot 			if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD))
3979ed83210SEmmanuel Vadot 				device_printf(sc->aw_dev, "Card inserted\n");
3989ed83210SEmmanuel Vadot 
3999ed83210SEmmanuel Vadot 			sc->child = device_add_child(sc->aw_dev, "mmc", -1);
4009ed83210SEmmanuel Vadot 			AW_MMC_UNLOCK(sc);
4019ed83210SEmmanuel Vadot 			if (sc->child) {
4029ed83210SEmmanuel Vadot 				device_set_ivars(sc->child, sc);
4039ed83210SEmmanuel Vadot 				(void)device_probe_and_attach(sc->child);
4049ed83210SEmmanuel Vadot 			}
4059ed83210SEmmanuel Vadot 		} else
4069ed83210SEmmanuel Vadot 			AW_MMC_UNLOCK(sc);
4079ed83210SEmmanuel Vadot 	} else {
4089ed83210SEmmanuel Vadot 		/* Card isn't present, detach if necessary */
4099ed83210SEmmanuel Vadot 		if (sc->child != NULL) {
410*020df509SEmmanuel Vadot 			if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD))
4119ed83210SEmmanuel Vadot 				device_printf(sc->aw_dev, "Card removed\n");
4129ed83210SEmmanuel Vadot 
4139ed83210SEmmanuel Vadot 			AW_MMC_UNLOCK(sc);
4149ed83210SEmmanuel Vadot 			device_delete_child(sc->aw_dev, sc->child);
4159ed83210SEmmanuel Vadot 			sc->child = NULL;
4169ed83210SEmmanuel Vadot 		} else
4179ed83210SEmmanuel Vadot 			AW_MMC_UNLOCK(sc);
4189ed83210SEmmanuel Vadot 	}
4199bca4667SEmmanuel Vadot #endif /* MMCCAM */
4209ed83210SEmmanuel Vadot }
4219ed83210SEmmanuel Vadot 
422b5be541fSEmmanuel Vadot static int
423b5be541fSEmmanuel Vadot aw_mmc_probe(device_t dev)
424b5be541fSEmmanuel Vadot {
425b5be541fSEmmanuel Vadot 
426b5be541fSEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
427b5be541fSEmmanuel Vadot 		return (ENXIO);
428b5be541fSEmmanuel Vadot 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
429b5be541fSEmmanuel Vadot 		return (ENXIO);
430b5be541fSEmmanuel Vadot 
431b5be541fSEmmanuel Vadot 	device_set_desc(dev, "Allwinner Integrated MMC/SD controller");
432b5be541fSEmmanuel Vadot 
433b5be541fSEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
434b5be541fSEmmanuel Vadot }
435b5be541fSEmmanuel Vadot 
436b5be541fSEmmanuel Vadot static int
437b5be541fSEmmanuel Vadot aw_mmc_attach(device_t dev)
438b5be541fSEmmanuel Vadot {
439b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
440b5be541fSEmmanuel Vadot 	struct sysctl_ctx_list *ctx;
441b5be541fSEmmanuel Vadot 	struct sysctl_oid_list *tree;
442b5be541fSEmmanuel Vadot 	int error;
443b5be541fSEmmanuel Vadot 
444b5be541fSEmmanuel Vadot 	sc = device_get_softc(dev);
445b5be541fSEmmanuel Vadot 	sc->aw_dev = dev;
446ce0618beSEmmanuel Vadot 
447ce0618beSEmmanuel Vadot 	sc->aw_mmc_conf = (struct aw_mmc_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
448ce0618beSEmmanuel Vadot 
4495e03278fSIlya Bakulin #ifndef MMCCAM
450b5be541fSEmmanuel Vadot 	sc->aw_req = NULL;
4515e03278fSIlya Bakulin #endif
452b5be541fSEmmanuel Vadot 	if (bus_alloc_resources(dev, aw_mmc_res_spec, sc->aw_res) != 0) {
453b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot allocate device resources\n");
454b5be541fSEmmanuel Vadot 		return (ENXIO);
455b5be541fSEmmanuel Vadot 	}
456b5be541fSEmmanuel Vadot 	if (bus_setup_intr(dev, sc->aw_res[AW_MMC_IRQRES],
4579ed83210SEmmanuel Vadot 	    INTR_TYPE_NET | INTR_MPSAFE, NULL, aw_mmc_intr, sc,
458b5be541fSEmmanuel Vadot 	    &sc->aw_intrhand)) {
459b5be541fSEmmanuel Vadot 		bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res);
460b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot setup interrupt handler\n");
461b5be541fSEmmanuel Vadot 		return (ENXIO);
462b5be541fSEmmanuel Vadot 	}
463b5be541fSEmmanuel Vadot 	mtx_init(&sc->aw_mtx, device_get_nameunit(sc->aw_dev), "aw_mmc",
464b5be541fSEmmanuel Vadot 	    MTX_DEF);
465b5be541fSEmmanuel Vadot 	callout_init_mtx(&sc->aw_timeoutc, &sc->aw_mtx, 0);
466b5be541fSEmmanuel Vadot 
467b5be541fSEmmanuel Vadot 	/* De-assert reset */
468b5be541fSEmmanuel Vadot 	if (hwreset_get_by_ofw_name(dev, 0, "ahb", &sc->aw_rst_ahb) == 0) {
469b5be541fSEmmanuel Vadot 		error = hwreset_deassert(sc->aw_rst_ahb);
470b5be541fSEmmanuel Vadot 		if (error != 0) {
471b5be541fSEmmanuel Vadot 			device_printf(dev, "cannot de-assert reset\n");
472b5be541fSEmmanuel Vadot 			goto fail;
473b5be541fSEmmanuel Vadot 		}
474b5be541fSEmmanuel Vadot 	}
475b5be541fSEmmanuel Vadot 
476b5be541fSEmmanuel Vadot 	/* Activate the module clock. */
477b5be541fSEmmanuel Vadot 	error = clk_get_by_ofw_name(dev, 0, "ahb", &sc->aw_clk_ahb);
478b5be541fSEmmanuel Vadot 	if (error != 0) {
479b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot get ahb clock\n");
480b5be541fSEmmanuel Vadot 		goto fail;
481b5be541fSEmmanuel Vadot 	}
482b5be541fSEmmanuel Vadot 	error = clk_enable(sc->aw_clk_ahb);
483b5be541fSEmmanuel Vadot 	if (error != 0) {
484b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot enable ahb clock\n");
485b5be541fSEmmanuel Vadot 		goto fail;
486b5be541fSEmmanuel Vadot 	}
487b5be541fSEmmanuel Vadot 	error = clk_get_by_ofw_name(dev, 0, "mmc", &sc->aw_clk_mmc);
488b5be541fSEmmanuel Vadot 	if (error != 0) {
489b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot get mmc clock\n");
490b5be541fSEmmanuel Vadot 		goto fail;
491b5be541fSEmmanuel Vadot 	}
492b5be541fSEmmanuel Vadot 	error = clk_set_freq(sc->aw_clk_mmc, CARD_ID_FREQUENCY,
493b5be541fSEmmanuel Vadot 	    CLK_SET_ROUND_DOWN);
494b5be541fSEmmanuel Vadot 	if (error != 0) {
495b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot init mmc clock\n");
496b5be541fSEmmanuel Vadot 		goto fail;
497b5be541fSEmmanuel Vadot 	}
498b5be541fSEmmanuel Vadot 	error = clk_enable(sc->aw_clk_mmc);
499b5be541fSEmmanuel Vadot 	if (error != 0) {
500b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot enable mmc clock\n");
501b5be541fSEmmanuel Vadot 		goto fail;
502b5be541fSEmmanuel Vadot 	}
503b5be541fSEmmanuel Vadot 
504b5be541fSEmmanuel Vadot 	sc->aw_timeout = 10;
505b5be541fSEmmanuel Vadot 	ctx = device_get_sysctl_ctx(dev);
506b5be541fSEmmanuel Vadot 	tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
507b5be541fSEmmanuel Vadot 	SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "req_timeout", CTLFLAG_RW,
508b5be541fSEmmanuel Vadot 	    &sc->aw_timeout, 0, "Request timeout in seconds");
509b5be541fSEmmanuel Vadot 
510b5be541fSEmmanuel Vadot 	/* Soft Reset controller. */
511b5be541fSEmmanuel Vadot 	if (aw_mmc_reset(sc) != 0) {
512b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot reset the controller\n");
513b5be541fSEmmanuel Vadot 		goto fail;
514b5be541fSEmmanuel Vadot 	}
515b5be541fSEmmanuel Vadot 
516b5be541fSEmmanuel Vadot 	if (aw_mmc_setup_dma(sc) != 0) {
517b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev, "Couldn't setup DMA!\n");
518b5be541fSEmmanuel Vadot 		goto fail;
519b5be541fSEmmanuel Vadot 	}
520b5be541fSEmmanuel Vadot 
5219ed83210SEmmanuel Vadot 	/* Set some defaults for freq and supported mode */
522b5be541fSEmmanuel Vadot 	sc->aw_host.f_min = 400000;
5239ed83210SEmmanuel Vadot 	sc->aw_host.f_max = 52000000;
524b5be541fSEmmanuel Vadot 	sc->aw_host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340;
5259ed83210SEmmanuel Vadot 	sc->aw_host.caps |= MMC_CAP_HSPEED | MMC_CAP_SIGNALING_330;
5269ed83210SEmmanuel Vadot 	mmc_fdt_parse(dev, 0, &sc->mmc_helper, &sc->aw_host);
5279ed83210SEmmanuel Vadot 	mmc_fdt_gpio_setup(dev, 0, &sc->mmc_helper, aw_mmc_helper_cd_handler);
528b5be541fSEmmanuel Vadot 
5295e03278fSIlya Bakulin #ifdef MMCCAM
5305e03278fSIlya Bakulin 	sc->ccb = NULL;
5315e03278fSIlya Bakulin 	if ((sc->devq = cam_simq_alloc(1)) == NULL) {
5325e03278fSIlya Bakulin 		goto fail;
5335e03278fSIlya Bakulin 	}
5345e03278fSIlya Bakulin 
5355e03278fSIlya Bakulin 	mtx_init(&sc->sim_mtx, "awmmcsim", NULL, MTX_DEF);
536ef546520SIlya Bakulin 	sc->sim = cam_sim_alloc_dev(aw_mmc_cam_action, aw_mmc_cam_poll,
537ef546520SIlya Bakulin 	    "aw_mmc_sim", sc, dev,
5385e03278fSIlya Bakulin 	    &sc->sim_mtx, 1, 1, sc->devq);
5395e03278fSIlya Bakulin 
5405e03278fSIlya Bakulin 	if (sc->sim == NULL) {
5415e03278fSIlya Bakulin 		cam_simq_free(sc->devq);
5425e03278fSIlya Bakulin 		device_printf(dev, "cannot allocate CAM SIM\n");
5435e03278fSIlya Bakulin 		goto fail;
5445e03278fSIlya Bakulin 	}
5455e03278fSIlya Bakulin 
5465e03278fSIlya Bakulin 	mtx_lock(&sc->sim_mtx);
5475e03278fSIlya Bakulin 	if (xpt_bus_register(sc->sim, sc->aw_dev, 0) != 0) {
5485e03278fSIlya Bakulin 		device_printf(dev, "cannot register SCSI pass-through bus\n");
5495e03278fSIlya Bakulin 		cam_sim_free(sc->sim, FALSE);
5505e03278fSIlya Bakulin 		cam_simq_free(sc->devq);
5515e03278fSIlya Bakulin 		mtx_unlock(&sc->sim_mtx);
5525e03278fSIlya Bakulin 		goto fail;
5535e03278fSIlya Bakulin 	}
5545e03278fSIlya Bakulin 
5555e03278fSIlya Bakulin 	mtx_unlock(&sc->sim_mtx);
5565e03278fSIlya Bakulin #endif /* MMCCAM */
5579ed83210SEmmanuel Vadot 
558b5be541fSEmmanuel Vadot 	return (0);
559b5be541fSEmmanuel Vadot 
560b5be541fSEmmanuel Vadot fail:
561b5be541fSEmmanuel Vadot 	callout_drain(&sc->aw_timeoutc);
562b5be541fSEmmanuel Vadot 	mtx_destroy(&sc->aw_mtx);
563b5be541fSEmmanuel Vadot 	bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand);
564b5be541fSEmmanuel Vadot 	bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res);
565b5be541fSEmmanuel Vadot 
5665e03278fSIlya Bakulin #ifdef MMCCAM
5675e03278fSIlya Bakulin 	if (sc->sim != NULL) {
5685e03278fSIlya Bakulin 		mtx_lock(&sc->sim_mtx);
5695e03278fSIlya Bakulin 		xpt_bus_deregister(cam_sim_path(sc->sim));
5705e03278fSIlya Bakulin 		cam_sim_free(sc->sim, FALSE);
5715e03278fSIlya Bakulin 		mtx_unlock(&sc->sim_mtx);
5725e03278fSIlya Bakulin 	}
5735e03278fSIlya Bakulin 
5745e03278fSIlya Bakulin 	if (sc->devq != NULL)
5755e03278fSIlya Bakulin 		cam_simq_free(sc->devq);
5765e03278fSIlya Bakulin #endif
577b5be541fSEmmanuel Vadot 	return (ENXIO);
578b5be541fSEmmanuel Vadot }
579b5be541fSEmmanuel Vadot 
580b5be541fSEmmanuel Vadot static int
581b5be541fSEmmanuel Vadot aw_mmc_detach(device_t dev)
582b5be541fSEmmanuel Vadot {
583cbba9a7bSEmmanuel Vadot 	struct aw_mmc_softc *sc;
584cbba9a7bSEmmanuel Vadot 	device_t d;
585b5be541fSEmmanuel Vadot 
586cbba9a7bSEmmanuel Vadot 	sc = device_get_softc(dev);
587cbba9a7bSEmmanuel Vadot 
588cbba9a7bSEmmanuel Vadot 	clk_disable(sc->aw_clk_mmc);
589cbba9a7bSEmmanuel Vadot 	clk_disable(sc->aw_clk_ahb);
590cbba9a7bSEmmanuel Vadot 	hwreset_assert(sc->aw_rst_ahb);
591cbba9a7bSEmmanuel Vadot 
592cbba9a7bSEmmanuel Vadot 	mmc_fdt_gpio_teardown(&sc->mmc_helper);
593cbba9a7bSEmmanuel Vadot 
594cbba9a7bSEmmanuel Vadot 	callout_drain(&sc->aw_timeoutc);
595cbba9a7bSEmmanuel Vadot 
596cbba9a7bSEmmanuel Vadot 	AW_MMC_LOCK(sc);
597cbba9a7bSEmmanuel Vadot 	d = sc->child;
598cbba9a7bSEmmanuel Vadot 	sc->child = NULL;
599cbba9a7bSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
600cbba9a7bSEmmanuel Vadot 	if (d != NULL)
601cbba9a7bSEmmanuel Vadot 		device_delete_child(sc->aw_dev, d);
602cbba9a7bSEmmanuel Vadot 
603cbba9a7bSEmmanuel Vadot 	aw_mmc_teardown_dma(sc);
604cbba9a7bSEmmanuel Vadot 
605cbba9a7bSEmmanuel Vadot 	mtx_destroy(&sc->aw_mtx);
606cbba9a7bSEmmanuel Vadot 
607cbba9a7bSEmmanuel Vadot 	bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand);
608cbba9a7bSEmmanuel Vadot 	bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res);
609cbba9a7bSEmmanuel Vadot 
610cbba9a7bSEmmanuel Vadot #ifdef MMCCAM
611cbba9a7bSEmmanuel Vadot 	if (sc->sim != NULL) {
612cbba9a7bSEmmanuel Vadot 		mtx_lock(&sc->sim_mtx);
613cbba9a7bSEmmanuel Vadot 		xpt_bus_deregister(cam_sim_path(sc->sim));
614cbba9a7bSEmmanuel Vadot 		cam_sim_free(sc->sim, FALSE);
615cbba9a7bSEmmanuel Vadot 		mtx_unlock(&sc->sim_mtx);
616cbba9a7bSEmmanuel Vadot 	}
617cbba9a7bSEmmanuel Vadot 
618cbba9a7bSEmmanuel Vadot 	if (sc->devq != NULL)
619cbba9a7bSEmmanuel Vadot 		cam_simq_free(sc->devq);
620cbba9a7bSEmmanuel Vadot #endif
621cbba9a7bSEmmanuel Vadot 
622cbba9a7bSEmmanuel Vadot 	return (0);
623b5be541fSEmmanuel Vadot }
624b5be541fSEmmanuel Vadot 
625b5be541fSEmmanuel Vadot static void
626b5be541fSEmmanuel Vadot aw_dma_desc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err)
627b5be541fSEmmanuel Vadot {
628b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
629b5be541fSEmmanuel Vadot 
630b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
631b5be541fSEmmanuel Vadot 	if (err) {
632b5be541fSEmmanuel Vadot 		sc->aw_dma_map_err = err;
633b5be541fSEmmanuel Vadot 		return;
634b5be541fSEmmanuel Vadot 	}
635b5be541fSEmmanuel Vadot 	sc->aw_dma_desc_phys = segs[0].ds_addr;
636b5be541fSEmmanuel Vadot }
637b5be541fSEmmanuel Vadot 
638b5be541fSEmmanuel Vadot static int
639b5be541fSEmmanuel Vadot aw_mmc_setup_dma(struct aw_mmc_softc *sc)
640b5be541fSEmmanuel Vadot {
641c39ea909SEmmanuel Vadot 	int error;
642b5be541fSEmmanuel Vadot 
643b5be541fSEmmanuel Vadot 	/* Allocate the DMA descriptor memory. */
644c39ea909SEmmanuel Vadot 	error = bus_dma_tag_create(
645c39ea909SEmmanuel Vadot 	    bus_get_dma_tag(sc->aw_dev),	/* parent */
646c39ea909SEmmanuel Vadot 	    AW_MMC_DMA_ALIGN, 0,		/* align, boundary */
647c39ea909SEmmanuel Vadot 	    BUS_SPACE_MAXADDR_32BIT,		/* lowaddr */
648c39ea909SEmmanuel Vadot 	    BUS_SPACE_MAXADDR,			/* highaddr */
649c39ea909SEmmanuel Vadot 	    NULL, NULL,				/* filter, filterarg*/
650c39ea909SEmmanuel Vadot 	    AW_MMC_DMA_DESC_SIZE, 1,		/* maxsize, nsegment */
651c39ea909SEmmanuel Vadot 	    AW_MMC_DMA_DESC_SIZE,		/* maxsegsize */
652c39ea909SEmmanuel Vadot 	    0,					/* flags */
653c39ea909SEmmanuel Vadot 	    NULL, NULL,				/* lock, lockarg*/
654c39ea909SEmmanuel Vadot 	    &sc->aw_dma_tag);
655b5be541fSEmmanuel Vadot 	if (error)
656b5be541fSEmmanuel Vadot 		return (error);
657b5be541fSEmmanuel Vadot 
658c39ea909SEmmanuel Vadot 	error = bus_dmamem_alloc(sc->aw_dma_tag, &sc->aw_dma_desc,
659c39ea909SEmmanuel Vadot 	    BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO,
660c39ea909SEmmanuel Vadot 	    &sc->aw_dma_map);
661c39ea909SEmmanuel Vadot 	if (error)
662c39ea909SEmmanuel Vadot 		return (error);
663c39ea909SEmmanuel Vadot 
664c39ea909SEmmanuel Vadot 	error = bus_dmamap_load(sc->aw_dma_tag,
665c39ea909SEmmanuel Vadot 	    sc->aw_dma_map,
666c39ea909SEmmanuel Vadot 	    sc->aw_dma_desc, AW_MMC_DMA_DESC_SIZE,
667c39ea909SEmmanuel Vadot 	    aw_dma_desc_cb, sc, 0);
668b5be541fSEmmanuel Vadot 	if (error)
669b5be541fSEmmanuel Vadot 		return (error);
670b5be541fSEmmanuel Vadot 	if (sc->aw_dma_map_err)
671b5be541fSEmmanuel Vadot 		return (sc->aw_dma_map_err);
672b5be541fSEmmanuel Vadot 
673b5be541fSEmmanuel Vadot 	/* Create the DMA map for data transfers. */
674c39ea909SEmmanuel Vadot 	error = bus_dma_tag_create(
675c39ea909SEmmanuel Vadot 	    bus_get_dma_tag(sc->aw_dev),	/* parent */
676c39ea909SEmmanuel Vadot 	    AW_MMC_DMA_ALIGN, 0,		/* align, boundary */
677c39ea909SEmmanuel Vadot 	    BUS_SPACE_MAXADDR_32BIT,		/* lowaddr */
678c39ea909SEmmanuel Vadot 	    BUS_SPACE_MAXADDR,			/* highaddr */
679c39ea909SEmmanuel Vadot 	    NULL, NULL,				/* filter, filterarg*/
680c39ea909SEmmanuel Vadot 	    sc->aw_mmc_conf->dma_xferlen *
681c39ea909SEmmanuel Vadot 	    AW_MMC_DMA_SEGS, AW_MMC_DMA_SEGS,	/* maxsize, nsegments */
682c39ea909SEmmanuel Vadot 	    sc->aw_mmc_conf->dma_xferlen,	/* maxsegsize */
683c39ea909SEmmanuel Vadot 	    BUS_DMA_ALLOCNOW,			/* flags */
684c39ea909SEmmanuel Vadot 	    NULL, NULL,				/* lock, lockarg*/
685b5be541fSEmmanuel Vadot 	    &sc->aw_dma_buf_tag);
686b5be541fSEmmanuel Vadot 	if (error)
687b5be541fSEmmanuel Vadot 		return (error);
688b5be541fSEmmanuel Vadot 	error = bus_dmamap_create(sc->aw_dma_buf_tag, 0,
689b5be541fSEmmanuel Vadot 	    &sc->aw_dma_buf_map);
690b5be541fSEmmanuel Vadot 	if (error)
691b5be541fSEmmanuel Vadot 		return (error);
692b5be541fSEmmanuel Vadot 
693b5be541fSEmmanuel Vadot 	return (0);
694b5be541fSEmmanuel Vadot }
695b5be541fSEmmanuel Vadot 
696b5be541fSEmmanuel Vadot static void
697cbba9a7bSEmmanuel Vadot aw_mmc_teardown_dma(struct aw_mmc_softc *sc)
698cbba9a7bSEmmanuel Vadot {
699cbba9a7bSEmmanuel Vadot 
700cbba9a7bSEmmanuel Vadot 	bus_dmamap_unload(sc->aw_dma_tag, sc->aw_dma_map);
701cbba9a7bSEmmanuel Vadot 	bus_dmamem_free(sc->aw_dma_tag, sc->aw_dma_desc, sc->aw_dma_map);
702cbba9a7bSEmmanuel Vadot 	if (bus_dma_tag_destroy(sc->aw_dma_tag) != 0)
703cbba9a7bSEmmanuel Vadot 		device_printf(sc->aw_dev, "Cannot destroy the dma tag\n");
704cbba9a7bSEmmanuel Vadot 
705cbba9a7bSEmmanuel Vadot 	bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map);
706cbba9a7bSEmmanuel Vadot 	bus_dmamap_destroy(sc->aw_dma_buf_tag, sc->aw_dma_buf_map);
707cbba9a7bSEmmanuel Vadot 	if (bus_dma_tag_destroy(sc->aw_dma_buf_tag) != 0)
708cbba9a7bSEmmanuel Vadot 		device_printf(sc->aw_dev, "Cannot destroy the dma buf tag\n");
709cbba9a7bSEmmanuel Vadot }
710cbba9a7bSEmmanuel Vadot 
711cbba9a7bSEmmanuel Vadot static void
712b5be541fSEmmanuel Vadot aw_dma_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err)
713b5be541fSEmmanuel Vadot {
714b5be541fSEmmanuel Vadot 	int i;
715b5be541fSEmmanuel Vadot 	struct aw_mmc_dma_desc *dma_desc;
716b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
717b5be541fSEmmanuel Vadot 
718b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
719b5be541fSEmmanuel Vadot 	sc->aw_dma_map_err = err;
720b5be541fSEmmanuel Vadot 
721b5be541fSEmmanuel Vadot 	if (err)
722b5be541fSEmmanuel Vadot 		return;
723b5be541fSEmmanuel Vadot 
724b5be541fSEmmanuel Vadot 	dma_desc = sc->aw_dma_desc;
725b5be541fSEmmanuel Vadot 	for (i = 0; i < nsegs; i++) {
726c39ea909SEmmanuel Vadot 		if (segs[i].ds_len == sc->aw_mmc_conf->dma_xferlen)
727c39ea909SEmmanuel Vadot 			dma_desc[i].buf_size = 0;		/* Size of 0 indicate max len */
728c39ea909SEmmanuel Vadot 		else
729b5be541fSEmmanuel Vadot 			dma_desc[i].buf_size = segs[i].ds_len;
730b5be541fSEmmanuel Vadot 		dma_desc[i].buf_addr = segs[i].ds_addr;
731b5be541fSEmmanuel Vadot 		dma_desc[i].config = AW_MMC_DMA_CONFIG_CH |
732c39ea909SEmmanuel Vadot 			AW_MMC_DMA_CONFIG_OWN | AW_MMC_DMA_CONFIG_DIC;
733c39ea909SEmmanuel Vadot 
734b5be541fSEmmanuel Vadot 		dma_desc[i].next = sc->aw_dma_desc_phys +
735b5be541fSEmmanuel Vadot 			((i + 1) * sizeof(struct aw_mmc_dma_desc));
736c39ea909SEmmanuel Vadot 	}
737c39ea909SEmmanuel Vadot 
738c39ea909SEmmanuel Vadot 	dma_desc[0].config |= AW_MMC_DMA_CONFIG_FD;
739c39ea909SEmmanuel Vadot 	dma_desc[nsegs - 1].config |= AW_MMC_DMA_CONFIG_LD |
740b5be541fSEmmanuel Vadot 		AW_MMC_DMA_CONFIG_ER;
741c39ea909SEmmanuel Vadot 	dma_desc[nsegs - 1].config &= ~AW_MMC_DMA_CONFIG_DIC;
742c39ea909SEmmanuel Vadot 	dma_desc[nsegs - 1].next = 0;
743b5be541fSEmmanuel Vadot }
744b5be541fSEmmanuel Vadot 
745b5be541fSEmmanuel Vadot static int
746b5be541fSEmmanuel Vadot aw_mmc_prepare_dma(struct aw_mmc_softc *sc)
747b5be541fSEmmanuel Vadot {
748b5be541fSEmmanuel Vadot 	bus_dmasync_op_t sync_op;
749b5be541fSEmmanuel Vadot 	int error;
750b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
751b5be541fSEmmanuel Vadot 	uint32_t val;
752b5be541fSEmmanuel Vadot 
7535e03278fSIlya Bakulin #ifdef MMCCAM
7545e03278fSIlya Bakulin 	cmd = &sc->ccb->mmcio.cmd;
7555e03278fSIlya Bakulin #else
756b5be541fSEmmanuel Vadot 	cmd = sc->aw_req->cmd;
7575e03278fSIlya Bakulin #endif
758ce0618beSEmmanuel Vadot 	if (cmd->data->len > (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS))
759b5be541fSEmmanuel Vadot 		return (EFBIG);
760b5be541fSEmmanuel Vadot 	error = bus_dmamap_load(sc->aw_dma_buf_tag, sc->aw_dma_buf_map,
761b5be541fSEmmanuel Vadot 	    cmd->data->data, cmd->data->len, aw_dma_cb, sc, 0);
762b5be541fSEmmanuel Vadot 	if (error)
763b5be541fSEmmanuel Vadot 		return (error);
764b5be541fSEmmanuel Vadot 	if (sc->aw_dma_map_err)
765b5be541fSEmmanuel Vadot 		return (sc->aw_dma_map_err);
766b5be541fSEmmanuel Vadot 
767b5be541fSEmmanuel Vadot 	if (cmd->data->flags & MMC_DATA_WRITE)
768b5be541fSEmmanuel Vadot 		sync_op = BUS_DMASYNC_PREWRITE;
769b5be541fSEmmanuel Vadot 	else
770b5be541fSEmmanuel Vadot 		sync_op = BUS_DMASYNC_PREREAD;
771b5be541fSEmmanuel Vadot 	bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, sync_op);
772b5be541fSEmmanuel Vadot 	bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, BUS_DMASYNC_PREWRITE);
773b5be541fSEmmanuel Vadot 
774b5be541fSEmmanuel Vadot 	/* Enable DMA */
775b5be541fSEmmanuel Vadot 	val = AW_MMC_READ_4(sc, AW_MMC_GCTL);
776b091392eSEmmanuel Vadot 	val &= ~AW_MMC_GCTL_FIFO_AC_MOD;
777b091392eSEmmanuel Vadot 	val |= AW_MMC_GCTL_DMA_ENB;
778b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val);
779b5be541fSEmmanuel Vadot 
780b5be541fSEmmanuel Vadot 	/* Reset DMA */
781b091392eSEmmanuel Vadot 	val |= AW_MMC_GCTL_DMA_RST;
782b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val);
783b5be541fSEmmanuel Vadot 
784b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_DMAC, AW_MMC_DMAC_IDMAC_SOFT_RST);
785b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_DMAC,
786b5be541fSEmmanuel Vadot 	    AW_MMC_DMAC_IDMAC_IDMA_ON | AW_MMC_DMAC_IDMAC_FIX_BURST);
787b5be541fSEmmanuel Vadot 
788b5be541fSEmmanuel Vadot 	/* Enable RX or TX DMA interrupt */
789a37d59c1SEmmanuel Vadot 	val = AW_MMC_READ_4(sc, AW_MMC_IDIE);
790b5be541fSEmmanuel Vadot 	if (cmd->data->flags & MMC_DATA_WRITE)
791b5be541fSEmmanuel Vadot 		val |= AW_MMC_IDST_TX_INT;
792b5be541fSEmmanuel Vadot 	else
793b5be541fSEmmanuel Vadot 		val |= AW_MMC_IDST_RX_INT;
794b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IDIE, val);
795b5be541fSEmmanuel Vadot 
796b5be541fSEmmanuel Vadot 	/* Set DMA descritptor list address */
797b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_DLBA, sc->aw_dma_desc_phys);
798b5be541fSEmmanuel Vadot 
799b5be541fSEmmanuel Vadot 	/* FIFO trigger level */
800b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_FWLR, AW_MMC_DMA_FTRGLEVEL);
801b5be541fSEmmanuel Vadot 
802b5be541fSEmmanuel Vadot 	return (0);
803b5be541fSEmmanuel Vadot }
804b5be541fSEmmanuel Vadot 
805b5be541fSEmmanuel Vadot static int
806b5be541fSEmmanuel Vadot aw_mmc_reset(struct aw_mmc_softc *sc)
807b5be541fSEmmanuel Vadot {
808b091392eSEmmanuel Vadot 	uint32_t reg;
809b5be541fSEmmanuel Vadot 	int timeout;
810b5be541fSEmmanuel Vadot 
811b091392eSEmmanuel Vadot 	reg = AW_MMC_READ_4(sc, AW_MMC_GCTL);
812b091392eSEmmanuel Vadot 	reg |= AW_MMC_GCTL_RESET;
813b091392eSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg);
814c39ea909SEmmanuel Vadot 	timeout = AW_MMC_RESET_RETRY;
815b5be541fSEmmanuel Vadot 	while (--timeout > 0) {
816b091392eSEmmanuel Vadot 		if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & AW_MMC_GCTL_RESET) == 0)
817b5be541fSEmmanuel Vadot 			break;
818b5be541fSEmmanuel Vadot 		DELAY(100);
819b5be541fSEmmanuel Vadot 	}
820b5be541fSEmmanuel Vadot 	if (timeout == 0)
821b5be541fSEmmanuel Vadot 		return (ETIMEDOUT);
822b5be541fSEmmanuel Vadot 
82335a18619SEmmanuel Vadot 	return (0);
82435a18619SEmmanuel Vadot }
82535a18619SEmmanuel Vadot 
82635a18619SEmmanuel Vadot static int
82735a18619SEmmanuel Vadot aw_mmc_init(struct aw_mmc_softc *sc)
82835a18619SEmmanuel Vadot {
829b091392eSEmmanuel Vadot 	uint32_t reg;
83035a18619SEmmanuel Vadot 	int ret;
83135a18619SEmmanuel Vadot 
83235a18619SEmmanuel Vadot 	ret = aw_mmc_reset(sc);
83335a18619SEmmanuel Vadot 	if (ret != 0)
83435a18619SEmmanuel Vadot 		return (ret);
83535a18619SEmmanuel Vadot 
836b5be541fSEmmanuel Vadot 	/* Set the timeout. */
837b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_TMOR,
838b5be541fSEmmanuel Vadot 	    AW_MMC_TMOR_DTO_LMT_SHIFT(AW_MMC_TMOR_DTO_LMT_MASK) |
839b5be541fSEmmanuel Vadot 	    AW_MMC_TMOR_RTO_LMT_SHIFT(AW_MMC_TMOR_RTO_LMT_MASK));
840b5be541fSEmmanuel Vadot 
84135a18619SEmmanuel Vadot 	/* Unmask interrupts. */
84235a18619SEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IMKR, 0);
84335a18619SEmmanuel Vadot 
844b5be541fSEmmanuel Vadot 	/* Clear pending interrupts. */
845b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff);
84635a18619SEmmanuel Vadot 
84735a18619SEmmanuel Vadot 	/* Debug register, undocumented */
84835a18619SEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_DBGC, 0xdeb);
84935a18619SEmmanuel Vadot 
85035a18619SEmmanuel Vadot 	/* Function select register */
85135a18619SEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_FUNS, 0xceaa0000);
85235a18619SEmmanuel Vadot 
853b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IDST, 0xffffffff);
85435a18619SEmmanuel Vadot 
855b091392eSEmmanuel Vadot 	/* Enable interrupts and disable AHB access. */
856b091392eSEmmanuel Vadot 	reg = AW_MMC_READ_4(sc, AW_MMC_GCTL);
857b091392eSEmmanuel Vadot 	reg |= AW_MMC_GCTL_INT_ENB;
858b091392eSEmmanuel Vadot 	reg &= ~AW_MMC_GCTL_FIFO_AC_MOD;
859b091392eSEmmanuel Vadot 	reg &= ~AW_MMC_GCTL_WAIT_MEM_ACCESS;
860b091392eSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg);
861b5be541fSEmmanuel Vadot 
862b5be541fSEmmanuel Vadot 	return (0);
863b5be541fSEmmanuel Vadot }
864b5be541fSEmmanuel Vadot 
865b5be541fSEmmanuel Vadot static void
866b5be541fSEmmanuel Vadot aw_mmc_req_done(struct aw_mmc_softc *sc)
867b5be541fSEmmanuel Vadot {
868b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
8695e03278fSIlya Bakulin #ifdef MMCCAM
8705e03278fSIlya Bakulin 	union ccb *ccb;
8715e03278fSIlya Bakulin #else
872b5be541fSEmmanuel Vadot 	struct mmc_request *req;
8735e03278fSIlya Bakulin #endif
874b5be541fSEmmanuel Vadot 	uint32_t val, mask;
875b5be541fSEmmanuel Vadot 	int retry;
876b5be541fSEmmanuel Vadot 
8775e03278fSIlya Bakulin #ifdef MMCCAM
8785e03278fSIlya Bakulin 	ccb = sc->ccb;
8795e03278fSIlya Bakulin 	cmd = &ccb->mmcio.cmd;
8805e03278fSIlya Bakulin #else
881b5be541fSEmmanuel Vadot 	cmd = sc->aw_req->cmd;
8825e03278fSIlya Bakulin #endif
883*020df509SEmmanuel Vadot 	if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CMD)) {
8845e03278fSIlya Bakulin 		device_printf(sc->aw_dev, "%s: cmd %d err %d\n", __func__, cmd->opcode, cmd->error);
8855e03278fSIlya Bakulin 	}
886b5be541fSEmmanuel Vadot 	if (cmd->error != MMC_ERR_NONE) {
887b5be541fSEmmanuel Vadot 		/* Reset the FIFO and DMA engines. */
888b091392eSEmmanuel Vadot 		mask = AW_MMC_GCTL_FIFO_RST | AW_MMC_GCTL_DMA_RST;
889b5be541fSEmmanuel Vadot 		val = AW_MMC_READ_4(sc, AW_MMC_GCTL);
890b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val | mask);
891b5be541fSEmmanuel Vadot 
892b5be541fSEmmanuel Vadot 		retry = AW_MMC_RESET_RETRY;
893b5be541fSEmmanuel Vadot 		while (--retry > 0) {
894c39ea909SEmmanuel Vadot 			if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) &
895c39ea909SEmmanuel Vadot 			    AW_MMC_GCTL_RESET) == 0)
896b5be541fSEmmanuel Vadot 				break;
897c39ea909SEmmanuel Vadot 			DELAY(100);
898b5be541fSEmmanuel Vadot 		}
899b5be541fSEmmanuel Vadot 		if (retry == 0)
900b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev,
901b5be541fSEmmanuel Vadot 			    "timeout resetting DMA/FIFO\n");
902b5be541fSEmmanuel Vadot 		aw_mmc_update_clock(sc, 1);
903b5be541fSEmmanuel Vadot 	}
904b5be541fSEmmanuel Vadot 
905b5be541fSEmmanuel Vadot 	callout_stop(&sc->aw_timeoutc);
906b5be541fSEmmanuel Vadot 	sc->aw_intr = 0;
907b5be541fSEmmanuel Vadot 	sc->aw_resid = 0;
908b5be541fSEmmanuel Vadot 	sc->aw_dma_map_err = 0;
909b5be541fSEmmanuel Vadot 	sc->aw_intr_wait = 0;
9105e03278fSIlya Bakulin #ifdef MMCCAM
9115e03278fSIlya Bakulin 	sc->ccb = NULL;
9125e03278fSIlya Bakulin 	ccb->ccb_h.status =
9135e03278fSIlya Bakulin 		(ccb->mmcio.cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR);
9145e03278fSIlya Bakulin 	xpt_done(ccb);
9155e03278fSIlya Bakulin #else
9165e03278fSIlya Bakulin 	req = sc->aw_req;
9175e03278fSIlya Bakulin 	sc->aw_req = NULL;
918b5be541fSEmmanuel Vadot 	req->done(req);
9195e03278fSIlya Bakulin #endif
920b5be541fSEmmanuel Vadot }
921b5be541fSEmmanuel Vadot 
922b5be541fSEmmanuel Vadot static void
923b5be541fSEmmanuel Vadot aw_mmc_req_ok(struct aw_mmc_softc *sc)
924b5be541fSEmmanuel Vadot {
925b5be541fSEmmanuel Vadot 	int timeout;
926b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
927b5be541fSEmmanuel Vadot 	uint32_t status;
928b5be541fSEmmanuel Vadot 
929b5be541fSEmmanuel Vadot 	timeout = 1000;
930b5be541fSEmmanuel Vadot 	while (--timeout > 0) {
931b5be541fSEmmanuel Vadot 		status = AW_MMC_READ_4(sc, AW_MMC_STAR);
932b5be541fSEmmanuel Vadot 		if ((status & AW_MMC_STAR_CARD_BUSY) == 0)
933b5be541fSEmmanuel Vadot 			break;
934b5be541fSEmmanuel Vadot 		DELAY(1000);
935b5be541fSEmmanuel Vadot 	}
9365e03278fSIlya Bakulin #ifdef MMCCAM
9375e03278fSIlya Bakulin 	cmd = &sc->ccb->mmcio.cmd;
9385e03278fSIlya Bakulin #else
939b5be541fSEmmanuel Vadot 	cmd = sc->aw_req->cmd;
9405e03278fSIlya Bakulin #endif
941b5be541fSEmmanuel Vadot 	if (timeout == 0) {
942b5be541fSEmmanuel Vadot 		cmd->error = MMC_ERR_FAILED;
943b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
944b5be541fSEmmanuel Vadot 		return;
945b5be541fSEmmanuel Vadot 	}
946b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_PRESENT) {
947b5be541fSEmmanuel Vadot 		if (cmd->flags & MMC_RSP_136) {
948b5be541fSEmmanuel Vadot 			cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP3);
949b5be541fSEmmanuel Vadot 			cmd->resp[1] = AW_MMC_READ_4(sc, AW_MMC_RESP2);
950b5be541fSEmmanuel Vadot 			cmd->resp[2] = AW_MMC_READ_4(sc, AW_MMC_RESP1);
951b5be541fSEmmanuel Vadot 			cmd->resp[3] = AW_MMC_READ_4(sc, AW_MMC_RESP0);
952b5be541fSEmmanuel Vadot 		} else
953b5be541fSEmmanuel Vadot 			cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP0);
954b5be541fSEmmanuel Vadot 	}
955b5be541fSEmmanuel Vadot 	/* All data has been transferred ? */
956b5be541fSEmmanuel Vadot 	if (cmd->data != NULL && (sc->aw_resid << 2) < cmd->data->len)
957b5be541fSEmmanuel Vadot 		cmd->error = MMC_ERR_FAILED;
958b5be541fSEmmanuel Vadot 	aw_mmc_req_done(sc);
959b5be541fSEmmanuel Vadot }
960b5be541fSEmmanuel Vadot 
9615e03278fSIlya Bakulin static inline void
9625e03278fSIlya Bakulin set_mmc_error(struct aw_mmc_softc *sc, int error_code)
9635e03278fSIlya Bakulin {
9645e03278fSIlya Bakulin #ifdef MMCCAM
9655e03278fSIlya Bakulin 	sc->ccb->mmcio.cmd.error = error_code;
9665e03278fSIlya Bakulin #else
9675e03278fSIlya Bakulin 	sc->aw_req->cmd->error = error_code;
9685e03278fSIlya Bakulin #endif
9695e03278fSIlya Bakulin }
9705e03278fSIlya Bakulin 
971b5be541fSEmmanuel Vadot static void
972b5be541fSEmmanuel Vadot aw_mmc_timeout(void *arg)
973b5be541fSEmmanuel Vadot {
974b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
975b5be541fSEmmanuel Vadot 
976b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
9775e03278fSIlya Bakulin #ifdef MMCCAM
9785e03278fSIlya Bakulin 	if (sc->ccb != NULL) {
9795e03278fSIlya Bakulin #else
980b5be541fSEmmanuel Vadot 	if (sc->aw_req != NULL) {
9815e03278fSIlya Bakulin #endif
982b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev, "controller timeout\n");
9835e03278fSIlya Bakulin 		set_mmc_error(sc, MMC_ERR_TIMEOUT);
984b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
985b5be541fSEmmanuel Vadot 	} else
986b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev,
987b5be541fSEmmanuel Vadot 		    "Spurious timeout - no active request\n");
988b5be541fSEmmanuel Vadot }
989b5be541fSEmmanuel Vadot 
990b5be541fSEmmanuel Vadot static void
9915e03278fSIlya Bakulin aw_mmc_print_error(uint32_t err)
9925e03278fSIlya Bakulin {
9935e03278fSIlya Bakulin 	if(err & AW_MMC_INT_RESP_ERR)
9945e03278fSIlya Bakulin 		printf("AW_MMC_INT_RESP_ERR ");
9955e03278fSIlya Bakulin 	if (err & AW_MMC_INT_RESP_CRC_ERR)
9965e03278fSIlya Bakulin 		printf("AW_MMC_INT_RESP_CRC_ERR ");
9975e03278fSIlya Bakulin 	if (err & AW_MMC_INT_DATA_CRC_ERR)
9985e03278fSIlya Bakulin 		printf("AW_MMC_INT_DATA_CRC_ERR ");
9995e03278fSIlya Bakulin 	if (err & AW_MMC_INT_RESP_TIMEOUT)
10005e03278fSIlya Bakulin 		printf("AW_MMC_INT_RESP_TIMEOUT ");
10015e03278fSIlya Bakulin 	if (err & AW_MMC_INT_FIFO_RUN_ERR)
10025e03278fSIlya Bakulin 		printf("AW_MMC_INT_FIFO_RUN_ERR ");
10035e03278fSIlya Bakulin 	if (err & AW_MMC_INT_CMD_BUSY)
10045e03278fSIlya Bakulin 		printf("AW_MMC_INT_CMD_BUSY ");
10055e03278fSIlya Bakulin 	if (err & AW_MMC_INT_DATA_START_ERR)
10065e03278fSIlya Bakulin 		printf("AW_MMC_INT_DATA_START_ERR ");
10075e03278fSIlya Bakulin 	if (err & AW_MMC_INT_DATA_END_BIT_ERR)
10085e03278fSIlya Bakulin 		printf("AW_MMC_INT_DATA_END_BIT_ERR");
10095e03278fSIlya Bakulin 	printf("\n");
10105e03278fSIlya Bakulin }
10115e03278fSIlya Bakulin 
10125e03278fSIlya Bakulin static void
1013b5be541fSEmmanuel Vadot aw_mmc_intr(void *arg)
1014b5be541fSEmmanuel Vadot {
1015b5be541fSEmmanuel Vadot 	bus_dmasync_op_t sync_op;
1016b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1017b5be541fSEmmanuel Vadot 	struct mmc_data *data;
1018b5be541fSEmmanuel Vadot 	uint32_t idst, imask, rint;
1019b5be541fSEmmanuel Vadot 
1020b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
1021b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
1022b5be541fSEmmanuel Vadot 	rint = AW_MMC_READ_4(sc, AW_MMC_RISR);
1023b5be541fSEmmanuel Vadot 	idst = AW_MMC_READ_4(sc, AW_MMC_IDST);
1024b5be541fSEmmanuel Vadot 	imask = AW_MMC_READ_4(sc, AW_MMC_IMKR);
1025b5be541fSEmmanuel Vadot 	if (idst == 0 && imask == 0 && rint == 0) {
1026b5be541fSEmmanuel Vadot 		AW_MMC_UNLOCK(sc);
1027b5be541fSEmmanuel Vadot 		return;
1028b5be541fSEmmanuel Vadot 	}
1029*020df509SEmmanuel Vadot 	if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_INT)) {
1030b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev, "idst: %#x, imask: %#x, rint: %#x\n",
1031b5be541fSEmmanuel Vadot 		    idst, imask, rint);
1032*020df509SEmmanuel Vadot 	}
10335e03278fSIlya Bakulin #ifdef MMCCAM
10345e03278fSIlya Bakulin 	if (sc->ccb == NULL) {
10355e03278fSIlya Bakulin #else
1036b5be541fSEmmanuel Vadot 	if (sc->aw_req == NULL) {
10375e03278fSIlya Bakulin #endif
1038b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev,
1039b5be541fSEmmanuel Vadot 		    "Spurious interrupt - no active request, rint: 0x%08X\n",
1040b5be541fSEmmanuel Vadot 		    rint);
10415e03278fSIlya Bakulin 		aw_mmc_print_error(rint);
1042b5be541fSEmmanuel Vadot 		goto end;
1043b5be541fSEmmanuel Vadot 	}
1044b5be541fSEmmanuel Vadot 	if (rint & AW_MMC_INT_ERR_BIT) {
1045*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_INT)) {
1046b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev, "error rint: 0x%08X\n", rint);
10475e03278fSIlya Bakulin 			aw_mmc_print_error(rint);
1048*020df509SEmmanuel Vadot 		}
1049b5be541fSEmmanuel Vadot 		if (rint & AW_MMC_INT_RESP_TIMEOUT)
10505e03278fSIlya Bakulin 			set_mmc_error(sc, MMC_ERR_TIMEOUT);
1051b5be541fSEmmanuel Vadot 		else
10525e03278fSIlya Bakulin 			set_mmc_error(sc, MMC_ERR_FAILED);
1053b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
1054b5be541fSEmmanuel Vadot 		goto end;
1055b5be541fSEmmanuel Vadot 	}
1056b5be541fSEmmanuel Vadot 	if (idst & AW_MMC_IDST_ERROR) {
1057*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_INT))
1058b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev, "error idst: 0x%08x\n", idst);
10595e03278fSIlya Bakulin 		set_mmc_error(sc, MMC_ERR_FAILED);
1060b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
1061b5be541fSEmmanuel Vadot 		goto end;
1062b5be541fSEmmanuel Vadot 	}
1063b5be541fSEmmanuel Vadot 
1064b5be541fSEmmanuel Vadot 	sc->aw_intr |= rint;
10655e03278fSIlya Bakulin #ifdef MMCCAM
10665e03278fSIlya Bakulin 	data = sc->ccb->mmcio.cmd.data;
10675e03278fSIlya Bakulin #else
1068b5be541fSEmmanuel Vadot 	data = sc->aw_req->cmd->data;
10695e03278fSIlya Bakulin #endif
1070b5be541fSEmmanuel Vadot 	if (data != NULL && (idst & AW_MMC_IDST_COMPLETE) != 0) {
1071b5be541fSEmmanuel Vadot 		if (data->flags & MMC_DATA_WRITE)
1072b5be541fSEmmanuel Vadot 			sync_op = BUS_DMASYNC_POSTWRITE;
1073b5be541fSEmmanuel Vadot 		else
1074b5be541fSEmmanuel Vadot 			sync_op = BUS_DMASYNC_POSTREAD;
1075b5be541fSEmmanuel Vadot 		bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map,
1076b5be541fSEmmanuel Vadot 		    sync_op);
1077b5be541fSEmmanuel Vadot 		bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map,
1078b5be541fSEmmanuel Vadot 		    BUS_DMASYNC_POSTWRITE);
1079b5be541fSEmmanuel Vadot 		bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map);
1080b5be541fSEmmanuel Vadot 		sc->aw_resid = data->len >> 2;
1081b5be541fSEmmanuel Vadot 	}
1082b5be541fSEmmanuel Vadot 	if ((sc->aw_intr & sc->aw_intr_wait) == sc->aw_intr_wait)
1083b5be541fSEmmanuel Vadot 		aw_mmc_req_ok(sc);
1084b5be541fSEmmanuel Vadot 
1085b5be541fSEmmanuel Vadot end:
1086b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IDST, idst);
1087b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_RISR, rint);
1088b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
1089b5be541fSEmmanuel Vadot }
1090b5be541fSEmmanuel Vadot 
1091b5be541fSEmmanuel Vadot static int
1092b5be541fSEmmanuel Vadot aw_mmc_request(device_t bus, device_t child, struct mmc_request *req)
1093b5be541fSEmmanuel Vadot {
1094b5be541fSEmmanuel Vadot 	int blksz;
1095b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1096b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
109735a18619SEmmanuel Vadot 	uint32_t cmdreg, imask;
1098b5be541fSEmmanuel Vadot 	int err;
1099b5be541fSEmmanuel Vadot 
1100b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
1101c39ea909SEmmanuel Vadot 
1102b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
11035e03278fSIlya Bakulin #ifdef MMCCAM
11045e03278fSIlya Bakulin 	KASSERT(req == NULL, ("req should be NULL in MMCCAM case!"));
11055e03278fSIlya Bakulin 	/*
11065e03278fSIlya Bakulin 	 * For MMCCAM, sc->ccb has been NULL-checked and populated
11075e03278fSIlya Bakulin 	 * by aw_mmc_cam_request() already.
11085e03278fSIlya Bakulin 	 */
11095e03278fSIlya Bakulin 	cmd = &sc->ccb->mmcio.cmd;
11105e03278fSIlya Bakulin #else
1111b5be541fSEmmanuel Vadot 	if (sc->aw_req) {
1112b5be541fSEmmanuel Vadot 		AW_MMC_UNLOCK(sc);
1113b5be541fSEmmanuel Vadot 		return (EBUSY);
1114b5be541fSEmmanuel Vadot 	}
1115b5be541fSEmmanuel Vadot 	sc->aw_req = req;
1116b5be541fSEmmanuel Vadot 	cmd = req->cmd;
11175e03278fSIlya Bakulin 
1118*020df509SEmmanuel Vadot 	if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CMD)) {
11195e03278fSIlya Bakulin 		device_printf(sc->aw_dev, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
11205e03278fSIlya Bakulin 			      cmd->opcode, cmd->arg, cmd->flags,
11215e03278fSIlya Bakulin 			      cmd->data != NULL ? (unsigned int)cmd->data->len : 0,
11225e03278fSIlya Bakulin 			      cmd->data != NULL ? cmd->data->flags: 0);
1123*020df509SEmmanuel Vadot 	}
11245e03278fSIlya Bakulin #endif
1125b5be541fSEmmanuel Vadot 	cmdreg = AW_MMC_CMDR_LOAD;
112635a18619SEmmanuel Vadot 	imask = AW_MMC_INT_ERR_BIT;
112735a18619SEmmanuel Vadot 	sc->aw_intr_wait = 0;
112835a18619SEmmanuel Vadot 	sc->aw_intr = 0;
112935a18619SEmmanuel Vadot 	sc->aw_resid = 0;
113035a18619SEmmanuel Vadot 	cmd->error = MMC_ERR_NONE;
113135a18619SEmmanuel Vadot 
1132b5be541fSEmmanuel Vadot 	if (cmd->opcode == MMC_GO_IDLE_STATE)
1133b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_SEND_INIT_SEQ;
113435a18619SEmmanuel Vadot 
1135b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_PRESENT)
1136b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_RESP_RCV;
1137b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_136)
1138b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_LONG_RESP;
1139b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_CRC)
1140b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_CHK_RESP_CRC;
1141b5be541fSEmmanuel Vadot 
114235a18619SEmmanuel Vadot 	if (cmd->data) {
1143b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_DATA_TRANS | AW_MMC_CMDR_WAIT_PRE_OVER;
114435a18619SEmmanuel Vadot 
1145b5be541fSEmmanuel Vadot 		if (cmd->data->flags & MMC_DATA_MULTI) {
1146b5be541fSEmmanuel Vadot 			cmdreg |= AW_MMC_CMDR_STOP_CMD_FLAG;
114735a18619SEmmanuel Vadot 			imask |= AW_MMC_INT_AUTO_STOP_DONE;
1148b5be541fSEmmanuel Vadot 			sc->aw_intr_wait |= AW_MMC_INT_AUTO_STOP_DONE;
114935a18619SEmmanuel Vadot 		} else {
115035a18619SEmmanuel Vadot 			sc->aw_intr_wait |= AW_MMC_INT_DATA_OVER;
115135a18619SEmmanuel Vadot 			imask |= AW_MMC_INT_DATA_OVER;
1152b5be541fSEmmanuel Vadot 		}
1153b5be541fSEmmanuel Vadot 		if (cmd->data->flags & MMC_DATA_WRITE)
1154b5be541fSEmmanuel Vadot 			cmdreg |= AW_MMC_CMDR_DIR_WRITE;
11555d5ae066SIlya Bakulin #ifdef MMCCAM
11565d5ae066SIlya Bakulin 		if (cmd->data->flags & MMC_DATA_BLOCK_SIZE) {
11575d5ae066SIlya Bakulin 			AW_MMC_WRITE_4(sc, AW_MMC_BKSR, cmd->data->block_size);
11585d5ae066SIlya Bakulin 			AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len);
11595d5ae066SIlya Bakulin 		} else
11605d5ae066SIlya Bakulin #endif
11615d5ae066SIlya Bakulin 		{
1162440565daSBjoern A. Zeeb 			blksz = min(cmd->data->len, MMC_SECTOR_SIZE);
1163b5be541fSEmmanuel Vadot 			AW_MMC_WRITE_4(sc, AW_MMC_BKSR, blksz);
1164b5be541fSEmmanuel Vadot 			AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len);
11655d5ae066SIlya Bakulin 		}
116635a18619SEmmanuel Vadot 	} else {
116735a18619SEmmanuel Vadot 		imask |= AW_MMC_INT_CMD_DONE;
116835a18619SEmmanuel Vadot 	}
1169b5be541fSEmmanuel Vadot 
117035a18619SEmmanuel Vadot 	/* Enable the interrupts we are interested in */
117135a18619SEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IMKR, imask);
117235a18619SEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff);
117335a18619SEmmanuel Vadot 
117435a18619SEmmanuel Vadot 	/* Enable auto stop if needed */
117535a18619SEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_A12A,
117635a18619SEmmanuel Vadot 	    cmdreg & AW_MMC_CMDR_STOP_CMD_FLAG ? 0 : 0xffff);
117735a18619SEmmanuel Vadot 
117835a18619SEmmanuel Vadot 	/* Write the command argument */
117935a18619SEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CAGR, cmd->arg);
118035a18619SEmmanuel Vadot 
118135a18619SEmmanuel Vadot 	/*
118235a18619SEmmanuel Vadot 	 * If we don't have data start the request
118335a18619SEmmanuel Vadot 	 * if we do prepare the dma request and start the request
118435a18619SEmmanuel Vadot 	 */
118535a18619SEmmanuel Vadot 	if (cmd->data == NULL) {
118635a18619SEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode);
118735a18619SEmmanuel Vadot 	} else {
1188b5be541fSEmmanuel Vadot 		err = aw_mmc_prepare_dma(sc);
1189b5be541fSEmmanuel Vadot 		if (err != 0)
1190b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev, "prepare_dma failed: %d\n", err);
119135a18619SEmmanuel Vadot 
119235a18619SEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode);
1193b5be541fSEmmanuel Vadot 	}
1194b5be541fSEmmanuel Vadot 
1195b5be541fSEmmanuel Vadot 	callout_reset(&sc->aw_timeoutc, sc->aw_timeout * hz,
1196b5be541fSEmmanuel Vadot 	    aw_mmc_timeout, sc);
1197b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
1198b5be541fSEmmanuel Vadot 
1199b5be541fSEmmanuel Vadot 	return (0);
1200b5be541fSEmmanuel Vadot }
1201b5be541fSEmmanuel Vadot 
1202b5be541fSEmmanuel Vadot static int
1203b5be541fSEmmanuel Vadot aw_mmc_read_ivar(device_t bus, device_t child, int which,
1204b5be541fSEmmanuel Vadot     uintptr_t *result)
1205b5be541fSEmmanuel Vadot {
1206b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1207b5be541fSEmmanuel Vadot 
1208b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
1209b5be541fSEmmanuel Vadot 	switch (which) {
1210b5be541fSEmmanuel Vadot 	default:
1211b5be541fSEmmanuel Vadot 		return (EINVAL);
1212b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_MODE:
1213b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.bus_mode;
1214b5be541fSEmmanuel Vadot 		break;
1215b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_WIDTH:
1216b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.bus_width;
1217b5be541fSEmmanuel Vadot 		break;
1218b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CHIP_SELECT:
1219b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.chip_select;
1220b5be541fSEmmanuel Vadot 		break;
1221b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CLOCK:
1222b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.clock;
1223b5be541fSEmmanuel Vadot 		break;
1224b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MIN:
1225b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.f_min;
1226b5be541fSEmmanuel Vadot 		break;
1227b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MAX:
1228b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.f_max;
1229b5be541fSEmmanuel Vadot 		break;
1230b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_HOST_OCR:
1231b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.host_ocr;
1232b5be541fSEmmanuel Vadot 		break;
1233b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MODE:
1234b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.mode;
1235b5be541fSEmmanuel Vadot 		break;
1236b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_OCR:
1237b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ocr;
1238b5be541fSEmmanuel Vadot 		break;
1239b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_POWER_MODE:
1240b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.power_mode;
1241b5be541fSEmmanuel Vadot 		break;
1242b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_VDD:
1243b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.vdd;
1244b5be541fSEmmanuel Vadot 		break;
1245dfb8c122SEmmanuel Vadot 	case MMCBR_IVAR_VCCQ:
1246dfb8c122SEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.vccq;
1247dfb8c122SEmmanuel Vadot 		break;
1248b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CAPS:
1249b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.caps;
1250b5be541fSEmmanuel Vadot 		break;
1251ce0618beSEmmanuel Vadot 	case MMCBR_IVAR_TIMING:
1252ce0618beSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.timing;
1253ce0618beSEmmanuel Vadot 		break;
1254b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MAX_DATA:
1255c39ea909SEmmanuel Vadot 		*(int *)result = (sc->aw_mmc_conf->dma_xferlen *
1256c39ea909SEmmanuel Vadot 		    AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE;
1257b5be541fSEmmanuel Vadot 		break;
125855f3f71cSEmmanuel Vadot 	case MMCBR_IVAR_RETUNE_REQ:
125955f3f71cSEmmanuel Vadot 		*(int *)result = retune_req_none;
126055f3f71cSEmmanuel Vadot 		break;
1261b5be541fSEmmanuel Vadot 	}
1262b5be541fSEmmanuel Vadot 
1263b5be541fSEmmanuel Vadot 	return (0);
1264b5be541fSEmmanuel Vadot }
1265b5be541fSEmmanuel Vadot 
1266b5be541fSEmmanuel Vadot static int
1267b5be541fSEmmanuel Vadot aw_mmc_write_ivar(device_t bus, device_t child, int which,
1268b5be541fSEmmanuel Vadot     uintptr_t value)
1269b5be541fSEmmanuel Vadot {
1270b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1271b5be541fSEmmanuel Vadot 
1272b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
1273b5be541fSEmmanuel Vadot 	switch (which) {
1274b5be541fSEmmanuel Vadot 	default:
1275b5be541fSEmmanuel Vadot 		return (EINVAL);
1276b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_MODE:
1277b5be541fSEmmanuel Vadot 		sc->aw_host.ios.bus_mode = value;
1278b5be541fSEmmanuel Vadot 		break;
1279b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_WIDTH:
1280b5be541fSEmmanuel Vadot 		sc->aw_host.ios.bus_width = value;
1281b5be541fSEmmanuel Vadot 		break;
1282b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CHIP_SELECT:
1283b5be541fSEmmanuel Vadot 		sc->aw_host.ios.chip_select = value;
1284b5be541fSEmmanuel Vadot 		break;
1285b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CLOCK:
1286b5be541fSEmmanuel Vadot 		sc->aw_host.ios.clock = value;
1287b5be541fSEmmanuel Vadot 		break;
1288b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MODE:
1289b5be541fSEmmanuel Vadot 		sc->aw_host.mode = value;
1290b5be541fSEmmanuel Vadot 		break;
1291b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_OCR:
1292b5be541fSEmmanuel Vadot 		sc->aw_host.ocr = value;
1293b5be541fSEmmanuel Vadot 		break;
1294b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_POWER_MODE:
1295b5be541fSEmmanuel Vadot 		sc->aw_host.ios.power_mode = value;
1296b5be541fSEmmanuel Vadot 		break;
1297b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_VDD:
1298b5be541fSEmmanuel Vadot 		sc->aw_host.ios.vdd = value;
1299b5be541fSEmmanuel Vadot 		break;
1300dfb8c122SEmmanuel Vadot 	case MMCBR_IVAR_VCCQ:
1301dfb8c122SEmmanuel Vadot 		sc->aw_host.ios.vccq = value;
1302dfb8c122SEmmanuel Vadot 		break;
1303ce0618beSEmmanuel Vadot 	case MMCBR_IVAR_TIMING:
1304ce0618beSEmmanuel Vadot 		sc->aw_host.ios.timing = value;
1305ce0618beSEmmanuel Vadot 		break;
1306b5be541fSEmmanuel Vadot 	/* These are read-only */
1307b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CAPS:
1308b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_HOST_OCR:
1309b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MIN:
1310b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MAX:
1311b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MAX_DATA:
1312b5be541fSEmmanuel Vadot 		return (EINVAL);
1313b5be541fSEmmanuel Vadot 	}
1314b5be541fSEmmanuel Vadot 
1315b5be541fSEmmanuel Vadot 	return (0);
1316b5be541fSEmmanuel Vadot }
1317b5be541fSEmmanuel Vadot 
1318b5be541fSEmmanuel Vadot static int
1319b5be541fSEmmanuel Vadot aw_mmc_update_clock(struct aw_mmc_softc *sc, uint32_t clkon)
1320b5be541fSEmmanuel Vadot {
1321ce0618beSEmmanuel Vadot 	uint32_t reg;
1322b5be541fSEmmanuel Vadot 	int retry;
1323b5be541fSEmmanuel Vadot 
1324ce0618beSEmmanuel Vadot 	reg = AW_MMC_READ_4(sc, AW_MMC_CKCR);
1325ffdb1aa8SEmmanuel Vadot 	reg &= ~(AW_MMC_CKCR_ENB | AW_MMC_CKCR_LOW_POWER |
1326ffdb1aa8SEmmanuel Vadot 	    AW_MMC_CKCR_MASK_DATA0);
1327b5be541fSEmmanuel Vadot 
1328b5be541fSEmmanuel Vadot 	if (clkon)
1329ffdb1aa8SEmmanuel Vadot 		reg |= AW_MMC_CKCR_ENB;
1330ce0618beSEmmanuel Vadot 	if (sc->aw_mmc_conf->mask_data0)
1331ffdb1aa8SEmmanuel Vadot 		reg |= AW_MMC_CKCR_MASK_DATA0;
1332b5be541fSEmmanuel Vadot 
1333ce0618beSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg);
1334b5be541fSEmmanuel Vadot 
1335ce0618beSEmmanuel Vadot 	reg = AW_MMC_CMDR_LOAD | AW_MMC_CMDR_PRG_CLK |
1336b5be541fSEmmanuel Vadot 	    AW_MMC_CMDR_WAIT_PRE_OVER;
1337ce0618beSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CMDR, reg);
1338b5be541fSEmmanuel Vadot 	retry = 0xfffff;
1339ce0618beSEmmanuel Vadot 
1340ce0618beSEmmanuel Vadot 	while (reg & AW_MMC_CMDR_LOAD && --retry > 0) {
1341ce0618beSEmmanuel Vadot 		reg = AW_MMC_READ_4(sc, AW_MMC_CMDR);
1342b5be541fSEmmanuel Vadot 		DELAY(10);
1343b5be541fSEmmanuel Vadot 	}
1344b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff);
1345b5be541fSEmmanuel Vadot 
1346ce0618beSEmmanuel Vadot 	if (reg & AW_MMC_CMDR_LOAD) {
1347ce0618beSEmmanuel Vadot 		device_printf(sc->aw_dev, "timeout updating clock\n");
1348b5be541fSEmmanuel Vadot 		return (ETIMEDOUT);
1349b5be541fSEmmanuel Vadot 	}
1350b5be541fSEmmanuel Vadot 
1351ce0618beSEmmanuel Vadot 	if (sc->aw_mmc_conf->mask_data0) {
1352ce0618beSEmmanuel Vadot 		reg = AW_MMC_READ_4(sc, AW_MMC_CKCR);
1353ffdb1aa8SEmmanuel Vadot 		reg &= ~AW_MMC_CKCR_MASK_DATA0;
1354ce0618beSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg);
1355ce0618beSEmmanuel Vadot 	}
1356ce0618beSEmmanuel Vadot 
1357ce0618beSEmmanuel Vadot 	return (0);
1358ce0618beSEmmanuel Vadot }
1359ce0618beSEmmanuel Vadot 
1360623966e1SEmmanuel Vadot static int
1361623966e1SEmmanuel Vadot aw_mmc_switch_vccq(device_t bus, device_t child)
1362ce0618beSEmmanuel Vadot {
1363623966e1SEmmanuel Vadot 	struct aw_mmc_softc *sc;
1364623966e1SEmmanuel Vadot 	int uvolt, err;
1365623966e1SEmmanuel Vadot 
1366623966e1SEmmanuel Vadot 	sc = device_get_softc(bus);
1367ce0618beSEmmanuel Vadot 
13689ed83210SEmmanuel Vadot 	if (sc->mmc_helper.vqmmc_supply == NULL)
1369623966e1SEmmanuel Vadot 		return EOPNOTSUPP;
1370ce0618beSEmmanuel Vadot 
1371623966e1SEmmanuel Vadot 	switch (sc->aw_host.ios.vccq) {
1372dfb8c122SEmmanuel Vadot 	case vccq_180:
1373dfb8c122SEmmanuel Vadot 		uvolt = 1800000;
1374ce0618beSEmmanuel Vadot 		break;
1375dfb8c122SEmmanuel Vadot 	case vccq_330:
1376dfb8c122SEmmanuel Vadot 		uvolt = 3300000;
1377ce0618beSEmmanuel Vadot 		break;
1378dfb8c122SEmmanuel Vadot 	default:
1379623966e1SEmmanuel Vadot 		return EINVAL;
1380ce0618beSEmmanuel Vadot 	}
1381ce0618beSEmmanuel Vadot 
13829ed83210SEmmanuel Vadot 	err = regulator_set_voltage(sc->mmc_helper.vqmmc_supply, uvolt, uvolt);
1383623966e1SEmmanuel Vadot 	if (err != 0) {
1384ce0618beSEmmanuel Vadot 		device_printf(sc->aw_dev,
1385ce0618beSEmmanuel Vadot 		    "Cannot set vqmmc to %d<->%d\n",
1386dfb8c122SEmmanuel Vadot 		    uvolt,
1387dfb8c122SEmmanuel Vadot 		    uvolt);
1388623966e1SEmmanuel Vadot 		return (err);
1389623966e1SEmmanuel Vadot 	}
1390623966e1SEmmanuel Vadot 
1391623966e1SEmmanuel Vadot 	return (0);
1392ce0618beSEmmanuel Vadot }
1393ce0618beSEmmanuel Vadot 
1394b5be541fSEmmanuel Vadot static int
1395b5be541fSEmmanuel Vadot aw_mmc_update_ios(device_t bus, device_t child)
1396b5be541fSEmmanuel Vadot {
1397b5be541fSEmmanuel Vadot 	int error;
1398b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1399b5be541fSEmmanuel Vadot 	struct mmc_ios *ios;
1400ce0618beSEmmanuel Vadot 	unsigned int clock;
1401ce0618beSEmmanuel Vadot 	uint32_t reg, div = 1;
1402b5be541fSEmmanuel Vadot 
1403b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
1404b5be541fSEmmanuel Vadot 
1405b5be541fSEmmanuel Vadot 	ios = &sc->aw_host.ios;
1406b5be541fSEmmanuel Vadot 
1407b5be541fSEmmanuel Vadot 	/* Set the bus width. */
1408b5be541fSEmmanuel Vadot 	switch (ios->bus_width) {
1409b5be541fSEmmanuel Vadot 	case bus_width_1:
1410b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR1);
1411b5be541fSEmmanuel Vadot 		break;
1412b5be541fSEmmanuel Vadot 	case bus_width_4:
1413b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR4);
1414b5be541fSEmmanuel Vadot 		break;
1415b5be541fSEmmanuel Vadot 	case bus_width_8:
1416b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR8);
1417b5be541fSEmmanuel Vadot 		break;
1418b5be541fSEmmanuel Vadot 	}
1419b5be541fSEmmanuel Vadot 
142035a18619SEmmanuel Vadot 	switch (ios->power_mode) {
142135a18619SEmmanuel Vadot 	case power_on:
142235a18619SEmmanuel Vadot 		break;
142335a18619SEmmanuel Vadot 	case power_off:
1424*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD))
1425ce0618beSEmmanuel Vadot 			device_printf(sc->aw_dev, "Powering down sd/mmc\n");
1426dfb8c122SEmmanuel Vadot 
14279ed83210SEmmanuel Vadot 		if (sc->mmc_helper.vmmc_supply)
14289ed83210SEmmanuel Vadot 			regulator_disable(sc->mmc_helper.vmmc_supply);
14299ed83210SEmmanuel Vadot 		if (sc->mmc_helper.vqmmc_supply)
14309ed83210SEmmanuel Vadot 			regulator_disable(sc->mmc_helper.vqmmc_supply);
1431dfb8c122SEmmanuel Vadot 
143235a18619SEmmanuel Vadot 		aw_mmc_reset(sc);
143335a18619SEmmanuel Vadot 		break;
143435a18619SEmmanuel Vadot 	case power_up:
1435*020df509SEmmanuel Vadot 		if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD))
143635a18619SEmmanuel Vadot 			device_printf(sc->aw_dev, "Powering up sd/mmc\n");
1437dfb8c122SEmmanuel Vadot 
14389ed83210SEmmanuel Vadot 		if (sc->mmc_helper.vmmc_supply)
14399ed83210SEmmanuel Vadot 			regulator_enable(sc->mmc_helper.vmmc_supply);
14409ed83210SEmmanuel Vadot 		if (sc->mmc_helper.vqmmc_supply)
14419ed83210SEmmanuel Vadot 			regulator_enable(sc->mmc_helper.vqmmc_supply);
144235a18619SEmmanuel Vadot 		aw_mmc_init(sc);
144335a18619SEmmanuel Vadot 		break;
144435a18619SEmmanuel Vadot 	};
1445ce0618beSEmmanuel Vadot 
1446ce0618beSEmmanuel Vadot 	/* Enable ddr mode if needed */
1447ce0618beSEmmanuel Vadot 	reg = AW_MMC_READ_4(sc, AW_MMC_GCTL);
1448ce0618beSEmmanuel Vadot 	if (ios->timing == bus_timing_uhs_ddr50 ||
1449ce0618beSEmmanuel Vadot 	  ios->timing == bus_timing_mmc_ddr52)
1450b091392eSEmmanuel Vadot 		reg |= AW_MMC_GCTL_DDR_MOD_SEL;
1451ce0618beSEmmanuel Vadot 	else
1452b091392eSEmmanuel Vadot 		reg &= ~AW_MMC_GCTL_DDR_MOD_SEL;
1453ce0618beSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg);
1454ce0618beSEmmanuel Vadot 
14550f7a6420SEmmanuel Vadot 	if (ios->clock && ios->clock != sc->aw_clock) {
14560f7a6420SEmmanuel Vadot 		sc->aw_clock = clock = ios->clock;
1457b5be541fSEmmanuel Vadot 
1458b5be541fSEmmanuel Vadot 		/* Disable clock */
1459b5be541fSEmmanuel Vadot 		error = aw_mmc_update_clock(sc, 0);
1460b5be541fSEmmanuel Vadot 		if (error != 0)
1461b5be541fSEmmanuel Vadot 			return (error);
1462b5be541fSEmmanuel Vadot 
1463ce0618beSEmmanuel Vadot 		if (ios->timing == bus_timing_mmc_ddr52 &&
1464ce0618beSEmmanuel Vadot 		    (sc->aw_mmc_conf->new_timing ||
1465ce0618beSEmmanuel Vadot 		    ios->bus_width == bus_width_8)) {
1466ce0618beSEmmanuel Vadot 			div = 2;
1467ce0618beSEmmanuel Vadot 			clock <<= 1;
1468ce0618beSEmmanuel Vadot 		}
1469ce0618beSEmmanuel Vadot 
1470b5be541fSEmmanuel Vadot 		/* Reset the divider. */
1471ce0618beSEmmanuel Vadot 		reg = AW_MMC_READ_4(sc, AW_MMC_CKCR);
1472ffdb1aa8SEmmanuel Vadot 		reg &= ~AW_MMC_CKCR_DIV;
1473ce0618beSEmmanuel Vadot 		reg |= div - 1;
1474ce0618beSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg);
1475ce0618beSEmmanuel Vadot 
1476ce0618beSEmmanuel Vadot 		/* New timing mode if needed */
1477ce0618beSEmmanuel Vadot 		if (sc->aw_mmc_conf->new_timing) {
1478ce0618beSEmmanuel Vadot 			reg = AW_MMC_READ_4(sc, AW_MMC_NTSR);
1479ce0618beSEmmanuel Vadot 			reg |= AW_MMC_NTSR_MODE_SELECT;
1480ce0618beSEmmanuel Vadot 			AW_MMC_WRITE_4(sc, AW_MMC_NTSR, reg);
1481ce0618beSEmmanuel Vadot 		}
1482b5be541fSEmmanuel Vadot 
1483b5be541fSEmmanuel Vadot 		/* Set the MMC clock. */
1484101260f3SEmmanuel Vadot 		error = clk_disable(sc->aw_clk_mmc);
1485101260f3SEmmanuel Vadot 		if (error != 0 && bootverbose)
1486101260f3SEmmanuel Vadot 			device_printf(sc->aw_dev,
1487101260f3SEmmanuel Vadot 			  "failed to disable mmc clock: %d\n", error);
1488ce0618beSEmmanuel Vadot 		error = clk_set_freq(sc->aw_clk_mmc, clock,
1489b5be541fSEmmanuel Vadot 		    CLK_SET_ROUND_DOWN);
1490b5be541fSEmmanuel Vadot 		if (error != 0) {
1491b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev,
1492b5be541fSEmmanuel Vadot 			    "failed to set frequency to %u Hz: %d\n",
1493ce0618beSEmmanuel Vadot 			    clock, error);
1494b5be541fSEmmanuel Vadot 			return (error);
1495b5be541fSEmmanuel Vadot 		}
1496101260f3SEmmanuel Vadot 		error = clk_enable(sc->aw_clk_mmc);
1497101260f3SEmmanuel Vadot 		if (error != 0 && bootverbose)
1498101260f3SEmmanuel Vadot 			device_printf(sc->aw_dev,
1499101260f3SEmmanuel Vadot 			  "failed to re-enable mmc clock: %d\n", error);
1500b5be541fSEmmanuel Vadot 
1501ce0618beSEmmanuel Vadot 		if (sc->aw_mmc_conf->can_calibrate)
1502ce0618beSEmmanuel Vadot 			AW_MMC_WRITE_4(sc, AW_MMC_SAMP_DL, AW_MMC_SAMP_DL_SW_EN);
1503ce0618beSEmmanuel Vadot 
1504b5be541fSEmmanuel Vadot 		/* Enable clock. */
1505b5be541fSEmmanuel Vadot 		error = aw_mmc_update_clock(sc, 1);
1506b5be541fSEmmanuel Vadot 		if (error != 0)
1507b5be541fSEmmanuel Vadot 			return (error);
1508b5be541fSEmmanuel Vadot 	}
1509b5be541fSEmmanuel Vadot 
1510b5be541fSEmmanuel Vadot 	return (0);
1511b5be541fSEmmanuel Vadot }
1512b5be541fSEmmanuel Vadot 
1513b5be541fSEmmanuel Vadot static int
1514b5be541fSEmmanuel Vadot aw_mmc_get_ro(device_t bus, device_t child)
1515b5be541fSEmmanuel Vadot {
15169ed83210SEmmanuel Vadot 	struct aw_mmc_softc *sc;
1517b5be541fSEmmanuel Vadot 
15189ed83210SEmmanuel Vadot 	sc = device_get_softc(bus);
15199ed83210SEmmanuel Vadot 
15209ed83210SEmmanuel Vadot 	return (mmc_fdt_gpio_get_readonly(&sc->mmc_helper));
1521b5be541fSEmmanuel Vadot }
1522b5be541fSEmmanuel Vadot 
1523b5be541fSEmmanuel Vadot static int
1524b5be541fSEmmanuel Vadot aw_mmc_acquire_host(device_t bus, device_t child)
1525b5be541fSEmmanuel Vadot {
1526b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1527b5be541fSEmmanuel Vadot 	int error;
1528b5be541fSEmmanuel Vadot 
1529b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
1530b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
1531b5be541fSEmmanuel Vadot 	while (sc->aw_bus_busy) {
1532b5be541fSEmmanuel Vadot 		error = msleep(sc, &sc->aw_mtx, PCATCH, "mmchw", 0);
1533b5be541fSEmmanuel Vadot 		if (error != 0) {
1534b5be541fSEmmanuel Vadot 			AW_MMC_UNLOCK(sc);
1535b5be541fSEmmanuel Vadot 			return (error);
1536b5be541fSEmmanuel Vadot 		}
1537b5be541fSEmmanuel Vadot 	}
1538b5be541fSEmmanuel Vadot 	sc->aw_bus_busy++;
1539b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
1540b5be541fSEmmanuel Vadot 
1541b5be541fSEmmanuel Vadot 	return (0);
1542b5be541fSEmmanuel Vadot }
1543b5be541fSEmmanuel Vadot 
1544b5be541fSEmmanuel Vadot static int
1545b5be541fSEmmanuel Vadot aw_mmc_release_host(device_t bus, device_t child)
1546b5be541fSEmmanuel Vadot {
1547b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1548b5be541fSEmmanuel Vadot 
1549b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
1550b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
1551b5be541fSEmmanuel Vadot 	sc->aw_bus_busy--;
1552b5be541fSEmmanuel Vadot 	wakeup(sc);
1553b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
1554b5be541fSEmmanuel Vadot 
1555b5be541fSEmmanuel Vadot 	return (0);
1556b5be541fSEmmanuel Vadot }
1557b5be541fSEmmanuel Vadot 
1558b5be541fSEmmanuel Vadot static device_method_t aw_mmc_methods[] = {
1559b5be541fSEmmanuel Vadot 	/* Device interface */
1560b5be541fSEmmanuel Vadot 	DEVMETHOD(device_probe,		aw_mmc_probe),
1561b5be541fSEmmanuel Vadot 	DEVMETHOD(device_attach,	aw_mmc_attach),
1562b5be541fSEmmanuel Vadot 	DEVMETHOD(device_detach,	aw_mmc_detach),
1563b5be541fSEmmanuel Vadot 
1564b5be541fSEmmanuel Vadot 	/* Bus interface */
1565b5be541fSEmmanuel Vadot 	DEVMETHOD(bus_read_ivar,	aw_mmc_read_ivar),
1566b5be541fSEmmanuel Vadot 	DEVMETHOD(bus_write_ivar,	aw_mmc_write_ivar),
1567ef546520SIlya Bakulin 	DEVMETHOD(bus_add_child,        bus_generic_add_child),
1568b5be541fSEmmanuel Vadot 
1569b5be541fSEmmanuel Vadot 	/* MMC bridge interface */
1570b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_update_ios,	aw_mmc_update_ios),
1571b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_request,	aw_mmc_request),
1572b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_get_ro,		aw_mmc_get_ro),
1573623966e1SEmmanuel Vadot 	DEVMETHOD(mmcbr_switch_vccq,	aw_mmc_switch_vccq),
1574b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_acquire_host,	aw_mmc_acquire_host),
1575b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_release_host,	aw_mmc_release_host),
1576b5be541fSEmmanuel Vadot 
1577b5be541fSEmmanuel Vadot 	DEVMETHOD_END
1578b5be541fSEmmanuel Vadot };
1579b5be541fSEmmanuel Vadot 
1580b5be541fSEmmanuel Vadot static devclass_t aw_mmc_devclass;
1581b5be541fSEmmanuel Vadot 
1582b5be541fSEmmanuel Vadot static driver_t aw_mmc_driver = {
1583b5be541fSEmmanuel Vadot 	"aw_mmc",
1584b5be541fSEmmanuel Vadot 	aw_mmc_methods,
1585b5be541fSEmmanuel Vadot 	sizeof(struct aw_mmc_softc),
1586b5be541fSEmmanuel Vadot };
1587b5be541fSEmmanuel Vadot 
1588b5be541fSEmmanuel Vadot DRIVER_MODULE(aw_mmc, simplebus, aw_mmc_driver, aw_mmc_devclass, NULL,
1589b5be541fSEmmanuel Vadot     NULL);
15905e03278fSIlya Bakulin #ifndef MMCCAM
1591b5be541fSEmmanuel Vadot MMC_DECLARE_BRIDGE(aw_mmc);
15925e03278fSIlya Bakulin #endif
1593cbba9a7bSEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
1594