Home | History | Annotate | Line # | Download | only in sunxi
sunxi_mmc.c revision 1.30
      1 /* $NetBSD: sunxi_mmc.c,v 1.30 2019/01/02 18:39:01 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include "opt_sunximmc.h"
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: sunxi_mmc.c,v 1.30 2019/01/02 18:39:01 jmcneill Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/bus.h>
     36 #include <sys/device.h>
     37 #include <sys/intr.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/gpio.h>
     41 
     42 #include <dev/sdmmc/sdmmcvar.h>
     43 #include <dev/sdmmc/sdmmcchip.h>
     44 #include <dev/sdmmc/sdmmc_ioreg.h>
     45 
     46 #include <dev/fdt/fdtvar.h>
     47 
     48 #include <arm/sunxi/sunxi_mmc.h>
     49 
     50 #ifdef SUNXI_MMC_DEBUG
     51 static int sunxi_mmc_debug = SUNXI_MMC_DEBUG;
     52 #define	DPRINTF(dev, fmt, ...)						\
     53 do {									\
     54 	if (sunxi_mmc_debug & __BIT(device_unit(dev)))			\
     55 		device_printf((dev), fmt, ##__VA_ARGS__);		\
     56 } while (0)
     57 #else
     58 #define	DPRINTF(dev, fmt, ...)		((void)0)
     59 #endif
     60 
     61 enum sunxi_mmc_timing {
     62 	SUNXI_MMC_TIMING_400K,
     63 	SUNXI_MMC_TIMING_25M,
     64 	SUNXI_MMC_TIMING_50M,
     65 	SUNXI_MMC_TIMING_50M_DDR,
     66 	SUNXI_MMC_TIMING_50M_DDR_8BIT,
     67 };
     68 
     69 struct sunxi_mmc_delay {
     70 	u_int	output_phase;
     71 	u_int	sample_phase;
     72 };
     73 
     74 static const struct sunxi_mmc_delay sun7i_mmc_delays[] = {
     75 	[SUNXI_MMC_TIMING_400K]		= { 180,	180 },
     76 	[SUNXI_MMC_TIMING_25M]		= { 180,	 75 },
     77 	[SUNXI_MMC_TIMING_50M]		= {  90,	120 },
     78 	[SUNXI_MMC_TIMING_50M_DDR]	= {  60,	120 },
     79 	[SUNXI_MMC_TIMING_50M_DDR_8BIT]	= {  90,	180 },
     80 };
     81 
     82 static const struct sunxi_mmc_delay sun9i_mmc_delays[] = {
     83 	[SUNXI_MMC_TIMING_400K]		= { 180,	180 },
     84 	[SUNXI_MMC_TIMING_25M]		= { 180,	 75 },
     85 	[SUNXI_MMC_TIMING_50M]		= { 150,	120 },
     86 	[SUNXI_MMC_TIMING_50M_DDR]	= {  54,	 36 },
     87 	[SUNXI_MMC_TIMING_50M_DDR_8BIT]	= {  72,	 72 },
     88 };
     89 
     90 #define SUNXI_MMC_NDESC		64
     91 
     92 struct sunxi_mmc_softc;
     93 
     94 static int	sunxi_mmc_match(device_t, cfdata_t, void *);
     95 static void	sunxi_mmc_attach(device_t, device_t, void *);
     96 static void	sunxi_mmc_attach_i(device_t);
     97 
     98 static int	sunxi_mmc_intr(void *);
     99 static int	sunxi_mmc_dmabounce_setup(struct sunxi_mmc_softc *);
    100 static int	sunxi_mmc_idma_setup(struct sunxi_mmc_softc *);
    101 
    102 static int	sunxi_mmc_host_reset(sdmmc_chipset_handle_t);
    103 static uint32_t	sunxi_mmc_host_ocr(sdmmc_chipset_handle_t);
    104 static int	sunxi_mmc_host_maxblklen(sdmmc_chipset_handle_t);
    105 static int	sunxi_mmc_card_detect(sdmmc_chipset_handle_t);
    106 static int	sunxi_mmc_write_protect(sdmmc_chipset_handle_t);
    107 static int	sunxi_mmc_bus_power(sdmmc_chipset_handle_t, uint32_t);
    108 static int	sunxi_mmc_bus_clock(sdmmc_chipset_handle_t, int, bool);
    109 static int	sunxi_mmc_bus_width(sdmmc_chipset_handle_t, int);
    110 static int	sunxi_mmc_bus_rod(sdmmc_chipset_handle_t, int);
    111 static int	sunxi_mmc_signal_voltage(sdmmc_chipset_handle_t, int);
    112 static int	sunxi_mmc_execute_tuning(sdmmc_chipset_handle_t, int);
    113 static void	sunxi_mmc_exec_command(sdmmc_chipset_handle_t,
    114 				      struct sdmmc_command *);
    115 static void	sunxi_mmc_card_enable_intr(sdmmc_chipset_handle_t, int);
    116 static void	sunxi_mmc_card_intr_ack(sdmmc_chipset_handle_t);
    117 
    118 static struct sdmmc_chip_functions sunxi_mmc_chip_functions = {
    119 	.host_reset = sunxi_mmc_host_reset,
    120 	.host_ocr = sunxi_mmc_host_ocr,
    121 	.host_maxblklen = sunxi_mmc_host_maxblklen,
    122 	.card_detect = sunxi_mmc_card_detect,
    123 	.write_protect = sunxi_mmc_write_protect,
    124 	.bus_power = sunxi_mmc_bus_power,
    125 	.bus_clock_ddr = sunxi_mmc_bus_clock,
    126 	.bus_width = sunxi_mmc_bus_width,
    127 	.bus_rod = sunxi_mmc_bus_rod,
    128 	.signal_voltage = sunxi_mmc_signal_voltage,
    129 	.execute_tuning = sunxi_mmc_execute_tuning,
    130 	.exec_command = sunxi_mmc_exec_command,
    131 	.card_enable_intr = sunxi_mmc_card_enable_intr,
    132 	.card_intr_ack = sunxi_mmc_card_intr_ack,
    133 };
    134 
    135 struct sunxi_mmc_config {
    136 	u_int idma_xferlen;
    137 	u_int flags;
    138 #define	SUNXI_MMC_FLAG_CALIB_REG	0x01
    139 #define	SUNXI_MMC_FLAG_NEW_TIMINGS	0x02
    140 #define	SUNXI_MMC_FLAG_MASK_DATA0	0x04
    141 #define	SUNXI_MMC_FLAG_HS200		0x08
    142 	const struct sunxi_mmc_delay *delays;
    143 	uint32_t dma_ftrglevel;
    144 };
    145 
    146 struct sunxi_mmc_softc {
    147 	device_t sc_dev;
    148 	bus_space_tag_t sc_bst;
    149 	bus_space_handle_t sc_bsh;
    150 	bus_dma_tag_t sc_dmat;
    151 	int sc_phandle;
    152 
    153 	void *sc_ih;
    154 	kmutex_t sc_intr_lock;
    155 	kcondvar_t sc_intr_cv;
    156 	kcondvar_t sc_idst_cv;
    157 
    158 	int sc_mmc_width;
    159 	int sc_mmc_present;
    160 
    161 	u_int sc_max_frequency;
    162 
    163 	device_t sc_sdmmc_dev;
    164 
    165 	struct sunxi_mmc_config *sc_config;
    166 
    167 	bus_dma_segment_t sc_idma_segs[1];
    168 	int sc_idma_nsegs;
    169 	bus_size_t sc_idma_size;
    170 	bus_dmamap_t sc_idma_map;
    171 	int sc_idma_ndesc;
    172 	void *sc_idma_desc;
    173 
    174 	bus_dmamap_t sc_dmabounce_map;
    175 	void *sc_dmabounce_buf;
    176 	size_t sc_dmabounce_buflen;
    177 
    178 	uint32_t sc_intr_rint;
    179 	uint32_t sc_idma_idst;
    180 
    181 	struct clk *sc_clk_ahb;
    182 	struct clk *sc_clk_mmc;
    183 	struct clk *sc_clk_output;
    184 	struct clk *sc_clk_sample;
    185 
    186 	struct fdtbus_reset *sc_rst_ahb;
    187 
    188 	struct fdtbus_gpio_pin *sc_gpio_cd;
    189 	int sc_gpio_cd_inverted;
    190 	struct fdtbus_gpio_pin *sc_gpio_wp;
    191 	int sc_gpio_wp_inverted;
    192 
    193 	struct fdtbus_regulator *sc_reg_vmmc;
    194 	struct fdtbus_regulator *sc_reg_vqmmc;
    195 
    196 	struct fdtbus_mmc_pwrseq *sc_pwrseq;
    197 
    198 	bool sc_non_removable;
    199 	bool sc_broken_cd;
    200 };
    201 
    202 CFATTACH_DECL_NEW(sunxi_mmc, sizeof(struct sunxi_mmc_softc),
    203 	sunxi_mmc_match, sunxi_mmc_attach, NULL, NULL);
    204 
    205 #define MMC_WRITE(sc, reg, val)	\
    206 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    207 #define MMC_READ(sc, reg) \
    208 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    209 
    210 static const struct sunxi_mmc_config sun4i_a10_mmc_config = {
    211 	.idma_xferlen = 0x2000,
    212 	.dma_ftrglevel = 0x20070008,
    213 	.delays = NULL,
    214 	.flags = 0,
    215 };
    216 
    217 static const struct sunxi_mmc_config sun5i_a13_mmc_config = {
    218 	.idma_xferlen = 0x10000,
    219 	.dma_ftrglevel = 0x20070008,
    220 	.delays = NULL,
    221 	.flags = 0,
    222 };
    223 
    224 static const struct sunxi_mmc_config sun7i_a20_mmc_config = {
    225 	.idma_xferlen = 0x2000,
    226 	.dma_ftrglevel = 0x20070008,
    227 	.delays = sun7i_mmc_delays,
    228 	.flags = 0,
    229 };
    230 
    231 static const struct sunxi_mmc_config sun8i_a83t_emmc_config = {
    232 	.idma_xferlen = 0x10000,
    233 	.dma_ftrglevel = 0x20070008,
    234 	.delays = NULL,
    235 	.flags = SUNXI_MMC_FLAG_NEW_TIMINGS,
    236 };
    237 
    238 static const struct sunxi_mmc_config sun9i_a80_mmc_config = {
    239 	.idma_xferlen = 0x10000,
    240 	.dma_ftrglevel = 0x200f0010,
    241 	.delays = sun9i_mmc_delays,
    242 	.flags = 0,
    243 };
    244 
    245 static const struct sunxi_mmc_config sun50i_a64_mmc_config = {
    246 	.idma_xferlen = 0x10000,
    247 	.dma_ftrglevel = 0x20070008,
    248 	.delays = NULL,
    249 	.flags = SUNXI_MMC_FLAG_CALIB_REG |
    250 		 SUNXI_MMC_FLAG_NEW_TIMINGS |
    251 		 SUNXI_MMC_FLAG_MASK_DATA0,
    252 };
    253 
    254 static const struct sunxi_mmc_config sun50i_a64_emmc_config = {
    255 	.idma_xferlen = 0x2000,
    256 	.dma_ftrglevel = 0x20070008,
    257 	.delays = NULL,
    258 	.flags = SUNXI_MMC_FLAG_CALIB_REG |
    259 		 SUNXI_MMC_FLAG_NEW_TIMINGS |
    260 		 SUNXI_MMC_FLAG_HS200,
    261 };
    262 
    263 static const struct sunxi_mmc_config sun50i_h6_mmc_config = {
    264 	.idma_xferlen = 0x10000,
    265 	.dma_ftrglevel = 0x20070008,
    266 	.delays = NULL,
    267 	.flags = SUNXI_MMC_FLAG_CALIB_REG |
    268 		 SUNXI_MMC_FLAG_NEW_TIMINGS |
    269 		 SUNXI_MMC_FLAG_MASK_DATA0,
    270 };
    271 
    272 static const struct sunxi_mmc_config sun50i_h6_emmc_config = {
    273 	.idma_xferlen = 0x2000,
    274 	.dma_ftrglevel = 0x20070008,
    275 	.delays = NULL,
    276 	.flags = SUNXI_MMC_FLAG_CALIB_REG,
    277 };
    278 
    279 static const struct of_compat_data compat_data[] = {
    280 	{ "allwinner,sun4i-a10-mmc",	(uintptr_t)&sun4i_a10_mmc_config },
    281 	{ "allwinner,sun5i-a13-mmc",	(uintptr_t)&sun5i_a13_mmc_config },
    282 	{ "allwinner,sun7i-a20-mmc",	(uintptr_t)&sun7i_a20_mmc_config },
    283 	{ "allwinner,sun8i-a83t-emmc",	(uintptr_t)&sun8i_a83t_emmc_config },
    284 	{ "allwinner,sun9i-a80-mmc",	(uintptr_t)&sun9i_a80_mmc_config },
    285 	{ "allwinner,sun50i-a64-mmc",	(uintptr_t)&sun50i_a64_mmc_config },
    286 	{ "allwinner,sun50i-a64-emmc",	(uintptr_t)&sun50i_a64_emmc_config },
    287 	{ "allwinner,sun50i-h6-mmc",	(uintptr_t)&sun50i_h6_mmc_config },
    288 	{ "allwinner,sun50i-h6-emmc",	(uintptr_t)&sun50i_h6_emmc_config },
    289 	{ NULL }
    290 };
    291 
    292 static int
    293 sunxi_mmc_match(device_t parent, cfdata_t cf, void *aux)
    294 {
    295 	struct fdt_attach_args * const faa = aux;
    296 
    297 	return of_match_compat_data(faa->faa_phandle, compat_data);
    298 }
    299 
    300 static void
    301 sunxi_mmc_attach(device_t parent, device_t self, void *aux)
    302 {
    303 	struct sunxi_mmc_softc * const sc = device_private(self);
    304 	struct fdt_attach_args * const faa = aux;
    305 	const int phandle = faa->faa_phandle;
    306 	char intrstr[128];
    307 	bus_addr_t addr;
    308 	bus_size_t size;
    309 
    310 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    311 		aprint_error(": couldn't get registers\n");
    312 		return;
    313 	}
    314 
    315 	sc->sc_clk_ahb = fdtbus_clock_get(phandle, "ahb");
    316 	sc->sc_clk_mmc = fdtbus_clock_get(phandle, "mmc");
    317 	sc->sc_clk_output = fdtbus_clock_get(phandle, "output");
    318 	sc->sc_clk_sample = fdtbus_clock_get(phandle, "sample");
    319 
    320 #if notyet
    321 	if (sc->sc_clk_ahb == NULL || sc->sc_clk_mmc == NULL ||
    322 	    sc->sc_clk_output == NULL || sc->sc_clk_sample == NULL) {
    323 #else
    324 	if (sc->sc_clk_ahb == NULL || sc->sc_clk_mmc == NULL) {
    325 #endif
    326 		aprint_error(": couldn't get clocks\n");
    327 		return;
    328 	}
    329 
    330 	sc->sc_rst_ahb = fdtbus_reset_get(phandle, "ahb");
    331 
    332 	sc->sc_pwrseq = fdtbus_mmc_pwrseq_get(phandle);
    333 
    334 	if (clk_enable(sc->sc_clk_ahb) != 0 ||
    335 	    clk_enable(sc->sc_clk_mmc) != 0) {
    336 		aprint_error(": couldn't enable clocks\n");
    337 		return;
    338 	}
    339 
    340 	if (sc->sc_rst_ahb != NULL) {
    341 		if (fdtbus_reset_deassert(sc->sc_rst_ahb) != 0) {
    342 			aprint_error(": couldn't de-assert resets\n");
    343 			return;
    344 		}
    345 	}
    346 
    347 	sc->sc_dev = self;
    348 	sc->sc_phandle = phandle;
    349 	sc->sc_config = (void *)of_search_compatible(phandle, compat_data)->data;
    350 	sc->sc_bst = faa->faa_bst;
    351 	sc->sc_dmat = faa->faa_dmat;
    352 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
    353 	cv_init(&sc->sc_intr_cv, "awinmmcirq");
    354 	cv_init(&sc->sc_idst_cv, "awinmmcdma");
    355 
    356 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    357 		aprint_error(": couldn't map registers\n");
    358 		return;
    359 	}
    360 
    361 	aprint_naive("\n");
    362 	aprint_normal(": SD/MMC controller\n");
    363 
    364 	sc->sc_reg_vmmc = fdtbus_regulator_acquire(phandle, "vmmc-supply");
    365 	sc->sc_reg_vqmmc = fdtbus_regulator_acquire(phandle, "vqmmc-supply");
    366 
    367 	sc->sc_gpio_cd = fdtbus_gpio_acquire(phandle, "cd-gpios",
    368 	    GPIO_PIN_INPUT);
    369 	sc->sc_gpio_wp = fdtbus_gpio_acquire(phandle, "wp-gpios",
    370 	    GPIO_PIN_INPUT);
    371 
    372 	sc->sc_gpio_cd_inverted = of_hasprop(phandle, "cd-inverted") ? 0 : 1;
    373 	sc->sc_gpio_wp_inverted = of_hasprop(phandle, "wp-inverted") ? 0 : 1;
    374 
    375 	sc->sc_non_removable = of_hasprop(phandle, "non-removable");
    376 	sc->sc_broken_cd = of_hasprop(phandle, "broken-cd");
    377 
    378 	if (of_getprop_uint32(phandle, "max-frequency", &sc->sc_max_frequency))
    379 		sc->sc_max_frequency = 52000000;
    380 
    381 	if (sunxi_mmc_dmabounce_setup(sc) != 0 ||
    382 	    sunxi_mmc_idma_setup(sc) != 0) {
    383 		aprint_error_dev(self, "failed to setup DMA\n");
    384 		return;
    385 	}
    386 
    387 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    388 		aprint_error_dev(self, "failed to decode interrupt\n");
    389 		return;
    390 	}
    391 
    392 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, FDT_INTR_MPSAFE,
    393 	    sunxi_mmc_intr, sc);
    394 	if (sc->sc_ih == NULL) {
    395 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    396 		    intrstr);
    397 		return;
    398 	}
    399 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    400 
    401 	config_interrupts(self, sunxi_mmc_attach_i);
    402 }
    403 
    404 static int
    405 sunxi_mmc_dmabounce_setup(struct sunxi_mmc_softc *sc)
    406 {
    407 	bus_dma_segment_t ds[1];
    408 	int error, rseg;
    409 
    410 	sc->sc_dmabounce_buflen = sunxi_mmc_host_maxblklen(sc);
    411 	error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmabounce_buflen, 0,
    412 	    sc->sc_dmabounce_buflen, ds, 1, &rseg, BUS_DMA_WAITOK);
    413 	if (error)
    414 		return error;
    415 	error = bus_dmamem_map(sc->sc_dmat, ds, 1, sc->sc_dmabounce_buflen,
    416 	    &sc->sc_dmabounce_buf, BUS_DMA_WAITOK);
    417 	if (error)
    418 		goto free;
    419 	error = bus_dmamap_create(sc->sc_dmat, sc->sc_dmabounce_buflen, 1,
    420 	    sc->sc_dmabounce_buflen, 0, BUS_DMA_WAITOK, &sc->sc_dmabounce_map);
    421 	if (error)
    422 		goto unmap;
    423 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmabounce_map,
    424 	    sc->sc_dmabounce_buf, sc->sc_dmabounce_buflen, NULL,
    425 	    BUS_DMA_WAITOK);
    426 	if (error)
    427 		goto destroy;
    428 	return 0;
    429 
    430 destroy:
    431 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmabounce_map);
    432 unmap:
    433 	bus_dmamem_unmap(sc->sc_dmat, sc->sc_dmabounce_buf,
    434 	    sc->sc_dmabounce_buflen);
    435 free:
    436 	bus_dmamem_free(sc->sc_dmat, ds, rseg);
    437 	return error;
    438 }
    439 
    440 static int
    441 sunxi_mmc_idma_setup(struct sunxi_mmc_softc *sc)
    442 {
    443 	int error;
    444 
    445 	sc->sc_idma_ndesc = SUNXI_MMC_NDESC;
    446 	sc->sc_idma_size = sizeof(struct sunxi_mmc_idma_descriptor) *
    447 	    sc->sc_idma_ndesc;
    448 	error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_idma_size, 0,
    449 	    sc->sc_idma_size, sc->sc_idma_segs, 1,
    450 	    &sc->sc_idma_nsegs, BUS_DMA_WAITOK);
    451 	if (error)
    452 		return error;
    453 	error = bus_dmamem_map(sc->sc_dmat, sc->sc_idma_segs,
    454 	    sc->sc_idma_nsegs, sc->sc_idma_size,
    455 	    &sc->sc_idma_desc, BUS_DMA_WAITOK);
    456 	if (error)
    457 		goto free;
    458 	error = bus_dmamap_create(sc->sc_dmat, sc->sc_idma_size, 1,
    459 	    sc->sc_idma_size, 0, BUS_DMA_WAITOK, &sc->sc_idma_map);
    460 	if (error)
    461 		goto unmap;
    462 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_idma_map,
    463 	    sc->sc_idma_desc, sc->sc_idma_size, NULL, BUS_DMA_WAITOK);
    464 	if (error)
    465 		goto destroy;
    466 	return 0;
    467 
    468 destroy:
    469 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_idma_map);
    470 unmap:
    471 	bus_dmamem_unmap(sc->sc_dmat, sc->sc_idma_desc, sc->sc_idma_size);
    472 free:
    473 	bus_dmamem_free(sc->sc_dmat, sc->sc_idma_segs, sc->sc_idma_nsegs);
    474 	return error;
    475 }
    476 
    477 static int
    478 sunxi_mmc_set_clock(struct sunxi_mmc_softc *sc, u_int freq, bool ddr)
    479 {
    480 	const struct sunxi_mmc_delay *delays;
    481 	int error, timing = SUNXI_MMC_TIMING_400K;
    482 
    483 	if (sc->sc_config->delays) {
    484 		if (freq <= 400) {
    485 			timing = SUNXI_MMC_TIMING_400K;
    486 		} else if (freq <= 25000) {
    487 			timing = SUNXI_MMC_TIMING_25M;
    488 		} else if (freq <= 52000) {
    489 			if (ddr) {
    490 				timing = sc->sc_mmc_width == 8 ?
    491 				    SUNXI_MMC_TIMING_50M_DDR_8BIT :
    492 				    SUNXI_MMC_TIMING_50M_DDR;
    493 			} else {
    494 				timing = SUNXI_MMC_TIMING_50M;
    495 			}
    496 		} else
    497 			return EINVAL;
    498 	}
    499 	if (sc->sc_max_frequency) {
    500 		if (freq * 1000 > sc->sc_max_frequency)
    501 			return EINVAL;
    502 	}
    503 
    504 	error = clk_set_rate(sc->sc_clk_mmc, (freq * 1000) << ddr);
    505 	if (error != 0)
    506 		return error;
    507 
    508 	if (sc->sc_config->delays == NULL)
    509 		return 0;
    510 
    511 	delays = &sc->sc_config->delays[timing];
    512 
    513 	if (sc->sc_clk_sample) {
    514 		error = clk_set_rate(sc->sc_clk_sample, delays->sample_phase);
    515 		if (error != 0)
    516 			return error;
    517 	}
    518 	if (sc->sc_clk_output) {
    519 		error = clk_set_rate(sc->sc_clk_output, delays->output_phase);
    520 		if (error != 0)
    521 			return error;
    522 	}
    523 
    524 	return 0;
    525 }
    526 
    527 static void
    528 sunxi_mmc_attach_i(device_t self)
    529 {
    530 	struct sunxi_mmc_softc *sc = device_private(self);
    531 	const u_int flags = sc->sc_config->flags;
    532 	struct sdmmcbus_attach_args saa;
    533 	uint32_t width;
    534 
    535 	if (sc->sc_pwrseq)
    536 		fdtbus_mmc_pwrseq_pre_power_on(sc->sc_pwrseq);
    537 
    538 	sunxi_mmc_host_reset(sc);
    539 	sunxi_mmc_bus_width(sc, 1);
    540 	sunxi_mmc_set_clock(sc, 400, false);
    541 
    542 	if (sc->sc_pwrseq)
    543 		fdtbus_mmc_pwrseq_post_power_on(sc->sc_pwrseq);
    544 
    545 	if (of_getprop_uint32(sc->sc_phandle, "bus-width", &width) != 0)
    546 		width = 4;
    547 
    548 	memset(&saa, 0, sizeof(saa));
    549 	saa.saa_busname = "sdmmc";
    550 	saa.saa_sct = &sunxi_mmc_chip_functions;
    551 	saa.saa_sch = sc;
    552 	saa.saa_dmat = sc->sc_dmat;
    553 	saa.saa_clkmin = 400;
    554 	saa.saa_clkmax = sc->sc_max_frequency / 1000;
    555 	saa.saa_caps = SMC_CAPS_DMA |
    556 		       SMC_CAPS_MULTI_SEG_DMA |
    557 		       SMC_CAPS_AUTO_STOP |
    558 		       SMC_CAPS_SD_HIGHSPEED |
    559 		       SMC_CAPS_MMC_HIGHSPEED |
    560 		       SMC_CAPS_POLLING;
    561 
    562 	if (sc->sc_config->delays || (flags & SUNXI_MMC_FLAG_NEW_TIMINGS))
    563 		saa.saa_caps |= SMC_CAPS_MMC_DDR52;
    564 
    565 	if (flags & SUNXI_MMC_FLAG_HS200)
    566 		saa.saa_caps |= SMC_CAPS_MMC_HS200;
    567 
    568 	if (width == 4)
    569 		saa.saa_caps |= SMC_CAPS_4BIT_MODE;
    570 	if (width == 8)
    571 		saa.saa_caps |= SMC_CAPS_8BIT_MODE;
    572 
    573 	if (sc->sc_gpio_cd)
    574 		saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
    575 
    576 	sc->sc_sdmmc_dev = config_found(self, &saa, NULL);
    577 }
    578 
    579 static int
    580 sunxi_mmc_intr(void *priv)
    581 {
    582 	struct sunxi_mmc_softc *sc = priv;
    583 	uint32_t idst, rint, imask;
    584 
    585 	mutex_enter(&sc->sc_intr_lock);
    586 	idst = MMC_READ(sc, SUNXI_MMC_IDST);
    587 	rint = MMC_READ(sc, SUNXI_MMC_RINT);
    588 	if (!idst && !rint) {
    589 		mutex_exit(&sc->sc_intr_lock);
    590 		return 0;
    591 	}
    592 	MMC_WRITE(sc, SUNXI_MMC_IDST, idst);
    593 	MMC_WRITE(sc, SUNXI_MMC_RINT, rint & ~SUNXI_MMC_INT_SDIO_INT);
    594 
    595 	DPRINTF(sc->sc_dev, "mmc intr idst=%08X rint=%08X\n",
    596 	    idst, rint);
    597 
    598 	if (idst != 0) {
    599 		MMC_WRITE(sc, SUNXI_MMC_IDIE, 0);
    600 		sc->sc_idma_idst |= idst;
    601 		cv_broadcast(&sc->sc_idst_cv);
    602 	}
    603 
    604 	if ((rint & ~SUNXI_MMC_INT_SDIO_INT) != 0) {
    605 		imask = MMC_READ(sc, SUNXI_MMC_IMASK);
    606 		MMC_WRITE(sc, SUNXI_MMC_IMASK, imask & ~SUNXI_MMC_INT_SDIO_INT);
    607 		sc->sc_intr_rint |= (rint & ~SUNXI_MMC_INT_SDIO_INT);
    608 		cv_broadcast(&sc->sc_intr_cv);
    609 	}
    610 
    611 	if ((rint & SUNXI_MMC_INT_SDIO_INT) != 0) {
    612 		sdmmc_card_intr(sc->sc_sdmmc_dev);
    613 	}
    614 
    615 	mutex_exit(&sc->sc_intr_lock);
    616 
    617 	return 1;
    618 }
    619 
    620 static int
    621 sunxi_mmc_wait_rint(struct sunxi_mmc_softc *sc, uint32_t mask,
    622     int timeout, bool poll)
    623 {
    624 	int retry;
    625 	int error;
    626 
    627 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    628 
    629 	if (sc->sc_intr_rint & mask)
    630 		return 0;
    631 
    632 	if (poll)
    633 		retry = timeout / hz * 1000;
    634 	else
    635 		retry = timeout / hz;
    636 
    637 	while (retry > 0) {
    638 		if (poll) {
    639 			sc->sc_intr_rint |= MMC_READ(sc, SUNXI_MMC_RINT);
    640 		} else {
    641 			error = cv_timedwait(&sc->sc_intr_cv,
    642 			    &sc->sc_intr_lock, hz);
    643 			if (error && error != EWOULDBLOCK)
    644 				return error;
    645 		}
    646 		if (sc->sc_intr_rint & mask)
    647 			return 0;
    648 		if (poll)
    649 			delay(1000);
    650 		--retry;
    651 	}
    652 
    653 	return ETIMEDOUT;
    654 }
    655 
    656 static int
    657 sunxi_mmc_host_reset(sdmmc_chipset_handle_t sch)
    658 {
    659 	struct sunxi_mmc_softc *sc = sch;
    660 	uint32_t gctrl;
    661 	int retry = 1000;
    662 
    663 	DPRINTF(sc->sc_dev, "host reset\n");
    664 
    665 	gctrl = MMC_READ(sc, SUNXI_MMC_GCTRL);
    666 	gctrl |= SUNXI_MMC_GCTRL_RESET;
    667 	MMC_WRITE(sc, SUNXI_MMC_GCTRL, gctrl);
    668 	while (--retry > 0) {
    669 		if (!(MMC_READ(sc, SUNXI_MMC_GCTRL) & SUNXI_MMC_GCTRL_RESET))
    670 			break;
    671 		delay(100);
    672 	}
    673 
    674 	MMC_WRITE(sc, SUNXI_MMC_TIMEOUT, 0xffffffff);
    675 
    676 	MMC_WRITE(sc, SUNXI_MMC_IMASK, 0);
    677 
    678 	MMC_WRITE(sc, SUNXI_MMC_RINT, 0xffffffff);
    679 
    680 	gctrl = MMC_READ(sc, SUNXI_MMC_GCTRL);
    681 	gctrl |= SUNXI_MMC_GCTRL_INTEN;
    682 	gctrl &= ~SUNXI_MMC_GCTRL_WAIT_MEM_ACCESS_DONE;
    683 	gctrl &= ~SUNXI_MMC_GCTRL_ACCESS_BY_AHB;
    684 	MMC_WRITE(sc, SUNXI_MMC_GCTRL, gctrl);
    685 
    686 	return 0;
    687 }
    688 
    689 static uint32_t
    690 sunxi_mmc_host_ocr(sdmmc_chipset_handle_t sch)
    691 {
    692 	return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V | MMC_OCR_HCS;
    693 }
    694 
    695 static int
    696 sunxi_mmc_host_maxblklen(sdmmc_chipset_handle_t sch)
    697 {
    698 	return 8192;
    699 }
    700 
    701 static int
    702 sunxi_mmc_card_detect(sdmmc_chipset_handle_t sch)
    703 {
    704 	struct sunxi_mmc_softc *sc = sch;
    705 
    706 	if (sc->sc_non_removable || sc->sc_broken_cd) {
    707 		/*
    708 		 * Non-removable or broken card detect flag set in
    709 		 * DT, assume always present
    710 		 */
    711 		return 1;
    712 	} else if (sc->sc_gpio_cd != NULL) {
    713 		/* Use card detect GPIO */
    714 		int v = 0, i;
    715 		for (i = 0; i < 5; i++) {
    716 			v += (fdtbus_gpio_read(sc->sc_gpio_cd) ^
    717 			    sc->sc_gpio_cd_inverted);
    718 			delay(1000);
    719 		}
    720 		if (v == 5)
    721 			sc->sc_mmc_present = 0;
    722 		else if (v == 0)
    723 			sc->sc_mmc_present = 1;
    724 		return sc->sc_mmc_present;
    725 	} else {
    726 		/* Use CARD_PRESENT field of SD_STATUS register */
    727 		const uint32_t present = MMC_READ(sc, SUNXI_MMC_STATUS) &
    728 		    SUNXI_MMC_STATUS_CARD_PRESENT;
    729 		return present != 0;
    730 	}
    731 }
    732 
    733 static int
    734 sunxi_mmc_write_protect(sdmmc_chipset_handle_t sch)
    735 {
    736 	struct sunxi_mmc_softc *sc = sch;
    737 
    738 	if (sc->sc_gpio_wp == NULL) {
    739 		return 0;	/* no write protect pin, assume rw */
    740 	} else {
    741 		return fdtbus_gpio_read(sc->sc_gpio_wp) ^
    742 		    sc->sc_gpio_wp_inverted;
    743 	}
    744 }
    745 
    746 static int
    747 sunxi_mmc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    748 {
    749 	return 0;
    750 }
    751 
    752 static int
    753 sunxi_mmc_update_clock(struct sunxi_mmc_softc *sc)
    754 {
    755 	uint32_t cmd;
    756 	int retry;
    757 
    758 	DPRINTF(sc->sc_dev, "update clock\n");
    759 
    760 	cmd = SUNXI_MMC_CMD_START |
    761 	      SUNXI_MMC_CMD_UPCLK_ONLY |
    762 	      SUNXI_MMC_CMD_WAIT_PRE_OVER;
    763 	MMC_WRITE(sc, SUNXI_MMC_CMD, cmd);
    764 	retry = 0xfffff;
    765 	while (--retry > 0) {
    766 		if (!(MMC_READ(sc, SUNXI_MMC_CMD) & SUNXI_MMC_CMD_START))
    767 			break;
    768 		delay(10);
    769 	}
    770 
    771 	if (retry == 0) {
    772 		aprint_error_dev(sc->sc_dev, "timeout updating clock\n");
    773 		DPRINTF(sc->sc_dev, "GCTRL: 0x%08x\n",
    774 		    MMC_READ(sc, SUNXI_MMC_GCTRL));
    775 		DPRINTF(sc->sc_dev, "CLKCR: 0x%08x\n",
    776 		    MMC_READ(sc, SUNXI_MMC_CLKCR));
    777 		DPRINTF(sc->sc_dev, "TIMEOUT: 0x%08x\n",
    778 		    MMC_READ(sc, SUNXI_MMC_TIMEOUT));
    779 		DPRINTF(sc->sc_dev, "WIDTH: 0x%08x\n",
    780 		    MMC_READ(sc, SUNXI_MMC_WIDTH));
    781 		DPRINTF(sc->sc_dev, "CMD: 0x%08x\n",
    782 		    MMC_READ(sc, SUNXI_MMC_CMD));
    783 		DPRINTF(sc->sc_dev, "MINT: 0x%08x\n",
    784 		    MMC_READ(sc, SUNXI_MMC_MINT));
    785 		DPRINTF(sc->sc_dev, "RINT: 0x%08x\n",
    786 		    MMC_READ(sc, SUNXI_MMC_RINT));
    787 		DPRINTF(sc->sc_dev, "STATUS: 0x%08x\n",
    788 		    MMC_READ(sc, SUNXI_MMC_STATUS));
    789 		return ETIMEDOUT;
    790 	}
    791 
    792 	return 0;
    793 }
    794 
    795 static int
    796 sunxi_mmc_bus_clock(sdmmc_chipset_handle_t sch, int freq, bool ddr)
    797 {
    798 	struct sunxi_mmc_softc *sc = sch;
    799 	uint32_t clkcr, gctrl, ntsr;
    800 	const u_int flags = sc->sc_config->flags;
    801 
    802 	clkcr = MMC_READ(sc, SUNXI_MMC_CLKCR);
    803 	if (clkcr & SUNXI_MMC_CLKCR_CARDCLKON) {
    804 		clkcr &= ~SUNXI_MMC_CLKCR_CARDCLKON;
    805 		if (flags & SUNXI_MMC_CLKCR_MASK_DATA0)
    806 			clkcr |= SUNXI_MMC_CLKCR_MASK_DATA0;
    807 		MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
    808 		if (sunxi_mmc_update_clock(sc) != 0)
    809 			return 1;
    810 		if (flags & SUNXI_MMC_CLKCR_MASK_DATA0) {
    811 			clkcr = MMC_READ(sc, SUNXI_MMC_CLKCR);
    812 			clkcr &= ~SUNXI_MMC_CLKCR_MASK_DATA0;
    813 			MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
    814 		}
    815 	}
    816 
    817 	if (freq) {
    818 
    819 		clkcr &= ~SUNXI_MMC_CLKCR_DIV;
    820 		clkcr |= __SHIFTIN(ddr, SUNXI_MMC_CLKCR_DIV);
    821 		MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
    822 
    823 		if (flags & SUNXI_MMC_FLAG_NEW_TIMINGS) {
    824 			ntsr = MMC_READ(sc, SUNXI_MMC_NTSR);
    825 			ntsr |= SUNXI_MMC_NTSR_MODE_SELECT;
    826 			MMC_WRITE(sc, SUNXI_MMC_NTSR, ntsr);
    827 		}
    828 
    829 		if (flags & SUNXI_MMC_FLAG_CALIB_REG)
    830 			MMC_WRITE(sc, SUNXI_MMC_SAMP_DL, SUNXI_MMC_SAMP_DL_SW_EN);
    831 
    832 		if (sunxi_mmc_update_clock(sc) != 0)
    833 			return 1;
    834 
    835 		gctrl = MMC_READ(sc, SUNXI_MMC_GCTRL);
    836 		if (ddr)
    837 			gctrl |= SUNXI_MMC_GCTRL_DDR_MODE;
    838 		else
    839 			gctrl &= ~SUNXI_MMC_GCTRL_DDR_MODE;
    840 		MMC_WRITE(sc, SUNXI_MMC_GCTRL, gctrl);
    841 
    842 		if (sunxi_mmc_set_clock(sc, freq, ddr) != 0)
    843 			return 1;
    844 
    845 		clkcr |= SUNXI_MMC_CLKCR_CARDCLKON;
    846 		if (flags & SUNXI_MMC_CLKCR_MASK_DATA0)
    847 			clkcr |= SUNXI_MMC_CLKCR_MASK_DATA0;
    848 		MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
    849 		if (sunxi_mmc_update_clock(sc) != 0)
    850 			return 1;
    851 		if (flags & SUNXI_MMC_CLKCR_MASK_DATA0) {
    852 			clkcr = MMC_READ(sc, SUNXI_MMC_CLKCR);
    853 			clkcr &= ~SUNXI_MMC_CLKCR_MASK_DATA0;
    854 			MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
    855 		}
    856 	}
    857 
    858 	return 0;
    859 }
    860 
    861 static int
    862 sunxi_mmc_bus_width(sdmmc_chipset_handle_t sch, int width)
    863 {
    864 	struct sunxi_mmc_softc *sc = sch;
    865 
    866 	DPRINTF(sc->sc_dev, "width = %d\n", width);
    867 
    868 	switch (width) {
    869 	case 1:
    870 		MMC_WRITE(sc, SUNXI_MMC_WIDTH, SUNXI_MMC_WIDTH_1);
    871 		break;
    872 	case 4:
    873 		MMC_WRITE(sc, SUNXI_MMC_WIDTH, SUNXI_MMC_WIDTH_4);
    874 		break;
    875 	case 8:
    876 		MMC_WRITE(sc, SUNXI_MMC_WIDTH, SUNXI_MMC_WIDTH_8);
    877 		break;
    878 	default:
    879 		return 1;
    880 	}
    881 
    882 	sc->sc_mmc_width = width;
    883 
    884 	return 0;
    885 }
    886 
    887 static int
    888 sunxi_mmc_bus_rod(sdmmc_chipset_handle_t sch, int on)
    889 {
    890 	return -1;
    891 }
    892 
    893 static int
    894 sunxi_mmc_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
    895 {
    896 	struct sunxi_mmc_softc *sc = sch;
    897 	u_int uvol;
    898 	int error;
    899 
    900 	if (sc->sc_reg_vqmmc == NULL)
    901 		return 0;
    902 
    903 	switch (signal_voltage) {
    904 	case SDMMC_SIGNAL_VOLTAGE_330:
    905 		uvol = 3300000;
    906 		break;
    907 	case SDMMC_SIGNAL_VOLTAGE_180:
    908 		uvol = 1800000;
    909 		break;
    910 	default:
    911 		return EINVAL;
    912 	}
    913 
    914 	error = fdtbus_regulator_supports_voltage(sc->sc_reg_vqmmc, uvol, uvol);
    915 	if (error != 0)
    916 		return 0;
    917 
    918 	error = fdtbus_regulator_set_voltage(sc->sc_reg_vqmmc, uvol, uvol);
    919 	if (error != 0)
    920 		return error;
    921 
    922 	return fdtbus_regulator_enable(sc->sc_reg_vqmmc);
    923 }
    924 
    925 static int
    926 sunxi_mmc_execute_tuning(sdmmc_chipset_handle_t sch, int timing)
    927 {
    928 	switch (timing) {
    929 	case SDMMC_TIMING_MMC_HS200:
    930 		break;
    931 	default:
    932 		return EINVAL;
    933 	}
    934 
    935 	return 0;
    936 }
    937 
    938 static int
    939 sunxi_mmc_dma_prepare(struct sunxi_mmc_softc *sc, struct sdmmc_command *cmd)
    940 {
    941 	struct sunxi_mmc_idma_descriptor *dma = sc->sc_idma_desc;
    942 	bus_addr_t desc_paddr = sc->sc_idma_map->dm_segs[0].ds_addr;
    943 	bus_dmamap_t map;
    944 	bus_size_t off;
    945 	int desc, resid, seg;
    946 	uint32_t val;
    947 
    948 	/*
    949 	 * If the command includes a dma map use it, otherwise we need to
    950 	 * bounce. This can happen for SDIO IO_RW_EXTENDED (CMD53) commands.
    951 	 */
    952 	if (cmd->c_dmamap) {
    953 		map = cmd->c_dmamap;
    954 	} else {
    955 		if (cmd->c_datalen > sc->sc_dmabounce_buflen)
    956 			return E2BIG;
    957 		map = sc->sc_dmabounce_map;
    958 
    959 		if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
    960 			memset(sc->sc_dmabounce_buf, 0, cmd->c_datalen);
    961 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
    962 			    0, cmd->c_datalen, BUS_DMASYNC_PREREAD);
    963 		} else {
    964 			memcpy(sc->sc_dmabounce_buf, cmd->c_data,
    965 			    cmd->c_datalen);
    966 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
    967 			    0, cmd->c_datalen, BUS_DMASYNC_PREWRITE);
    968 		}
    969 	}
    970 
    971 	desc = 0;
    972 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    973 		bus_addr_t paddr = map->dm_segs[seg].ds_addr;
    974 		bus_size_t len = map->dm_segs[seg].ds_len;
    975 		resid = uimin(len, cmd->c_resid);
    976 		off = 0;
    977 		while (resid > 0) {
    978 			if (desc == sc->sc_idma_ndesc)
    979 				break;
    980 			len = uimin(sc->sc_config->idma_xferlen, resid);
    981 			dma[desc].dma_buf_size = htole32(len);
    982 			dma[desc].dma_buf_addr = htole32(paddr + off);
    983 			dma[desc].dma_config = htole32(SUNXI_MMC_IDMA_CONFIG_CH |
    984 					       SUNXI_MMC_IDMA_CONFIG_OWN);
    985 			cmd->c_resid -= len;
    986 			resid -= len;
    987 			off += len;
    988 			if (desc == 0) {
    989 				dma[desc].dma_config |= htole32(SUNXI_MMC_IDMA_CONFIG_FD);
    990 			}
    991 			if (cmd->c_resid == 0) {
    992 				dma[desc].dma_config |= htole32(SUNXI_MMC_IDMA_CONFIG_LD);
    993 				dma[desc].dma_config |= htole32(SUNXI_MMC_IDMA_CONFIG_ER);
    994 				dma[desc].dma_next = 0;
    995 			} else {
    996 				dma[desc].dma_config |=
    997 				    htole32(SUNXI_MMC_IDMA_CONFIG_DIC);
    998 				dma[desc].dma_next = htole32(
    999 				    desc_paddr + ((desc+1) *
   1000 				    sizeof(struct sunxi_mmc_idma_descriptor)));
   1001 			}
   1002 			++desc;
   1003 		}
   1004 	}
   1005 	if (desc == sc->sc_idma_ndesc) {
   1006 		aprint_error_dev(sc->sc_dev,
   1007 		    "not enough descriptors for %d byte transfer! "
   1008 		    "there are %u segments with a max xfer length of %u\n",
   1009 		    cmd->c_datalen, map->dm_nsegs, sc->sc_config->idma_xferlen);
   1010 		return EIO;
   1011 	}
   1012 
   1013 	bus_dmamap_sync(sc->sc_dmat, sc->sc_idma_map, 0,
   1014 	    sc->sc_idma_size, BUS_DMASYNC_PREWRITE);
   1015 
   1016 	sc->sc_idma_idst = 0;
   1017 
   1018 	MMC_WRITE(sc, SUNXI_MMC_DLBA, desc_paddr);
   1019 	MMC_WRITE(sc, SUNXI_MMC_FTRGLEVEL, sc->sc_config->dma_ftrglevel);
   1020 
   1021 	val = MMC_READ(sc, SUNXI_MMC_GCTRL);
   1022 	val |= SUNXI_MMC_GCTRL_DMAEN;
   1023 	MMC_WRITE(sc, SUNXI_MMC_GCTRL, val);
   1024 	val |= SUNXI_MMC_GCTRL_DMARESET;
   1025 	MMC_WRITE(sc, SUNXI_MMC_GCTRL, val);
   1026 
   1027 	MMC_WRITE(sc, SUNXI_MMC_DMAC, SUNXI_MMC_DMAC_SOFTRESET);
   1028 	if (ISSET(cmd->c_flags, SCF_CMD_READ))
   1029 		val = SUNXI_MMC_IDST_RECEIVE_INT;
   1030 	else
   1031 		val = 0;
   1032 	MMC_WRITE(sc, SUNXI_MMC_IDIE, val);
   1033 	MMC_WRITE(sc, SUNXI_MMC_DMAC,
   1034 	    SUNXI_MMC_DMAC_IDMA_ON|SUNXI_MMC_DMAC_FIX_BURST);
   1035 
   1036 	return 0;
   1037 }
   1038 
   1039 static void
   1040 sunxi_mmc_dma_complete(struct sunxi_mmc_softc *sc, struct sdmmc_command *cmd)
   1041 {
   1042 	MMC_WRITE(sc, SUNXI_MMC_DMAC, 0);
   1043 
   1044 	bus_dmamap_sync(sc->sc_dmat, sc->sc_idma_map, 0,
   1045 	    sc->sc_idma_size, BUS_DMASYNC_POSTWRITE);
   1046 
   1047 	if (cmd->c_dmamap == NULL) {
   1048 		if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
   1049 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
   1050 			    0, cmd->c_datalen, BUS_DMASYNC_POSTREAD);
   1051 			memcpy(cmd->c_data, sc->sc_dmabounce_buf,
   1052 			    cmd->c_datalen);
   1053 		} else {
   1054 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
   1055 			    0, cmd->c_datalen, BUS_DMASYNC_POSTWRITE);
   1056 		}
   1057 	}
   1058 }
   1059 
   1060 static void
   1061 sunxi_mmc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
   1062 {
   1063 	struct sunxi_mmc_softc *sc = sch;
   1064 	uint32_t cmdval = SUNXI_MMC_CMD_START;
   1065 	uint32_t imask, oimask;
   1066 	const bool poll = (cmd->c_flags & SCF_POLL) != 0;
   1067 	int retry;
   1068 
   1069 	DPRINTF(sc->sc_dev,
   1070 	    "opcode %d flags 0x%x data %p datalen %d blklen %d poll %d\n",
   1071 	    cmd->c_opcode, cmd->c_flags, cmd->c_data, cmd->c_datalen,
   1072 	    cmd->c_blklen, poll);
   1073 
   1074 	mutex_enter(&sc->sc_intr_lock);
   1075 
   1076 	if (cmd->c_opcode == 0)
   1077 		cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
   1078 	if (cmd->c_flags & SCF_RSP_PRESENT)
   1079 		cmdval |= SUNXI_MMC_CMD_RSP_EXP;
   1080 	if (cmd->c_flags & SCF_RSP_136)
   1081 		cmdval |= SUNXI_MMC_CMD_LONG_RSP;
   1082 	if (cmd->c_flags & SCF_RSP_CRC)
   1083 		cmdval |= SUNXI_MMC_CMD_CHECK_RSP_CRC;
   1084 
   1085 	imask = oimask = MMC_READ(sc, SUNXI_MMC_IMASK);
   1086 	imask |= SUNXI_MMC_INT_ERROR;
   1087 
   1088 	if (cmd->c_datalen > 0) {
   1089 		unsigned int nblks;
   1090 
   1091 		cmdval |= SUNXI_MMC_CMD_DATA_EXP | SUNXI_MMC_CMD_WAIT_PRE_OVER;
   1092 		if (!ISSET(cmd->c_flags, SCF_CMD_READ)) {
   1093 			cmdval |= SUNXI_MMC_CMD_WRITE;
   1094 		}
   1095 
   1096 		nblks = cmd->c_datalen / cmd->c_blklen;
   1097 		if (nblks == 0 || (cmd->c_datalen % cmd->c_blklen) != 0)
   1098 			++nblks;
   1099 
   1100 		if (nblks > 1) {
   1101 			cmdval |= SUNXI_MMC_CMD_SEND_AUTO_STOP;
   1102 			imask |= SUNXI_MMC_INT_AUTO_CMD_DONE;
   1103 		} else {
   1104 			imask |= SUNXI_MMC_INT_DATA_OVER;
   1105 		}
   1106 
   1107 		MMC_WRITE(sc, SUNXI_MMC_BLKSZ, cmd->c_blklen);
   1108 		MMC_WRITE(sc, SUNXI_MMC_BYTECNT, nblks * cmd->c_blklen);
   1109 	} else {
   1110 		imask |= SUNXI_MMC_INT_CMD_DONE;
   1111 	}
   1112 
   1113 	MMC_WRITE(sc, SUNXI_MMC_IMASK, imask);
   1114 	MMC_WRITE(sc, SUNXI_MMC_RINT, 0xffff);
   1115 
   1116 	sc->sc_intr_rint = 0;
   1117 
   1118 	MMC_WRITE(sc, SUNXI_MMC_A12A,
   1119 	    (cmdval & SUNXI_MMC_CMD_SEND_AUTO_STOP) ? 0 : 0xffff);
   1120 
   1121 	MMC_WRITE(sc, SUNXI_MMC_ARG, cmd->c_arg);
   1122 
   1123 	DPRINTF(sc->sc_dev, "cmdval = %08x\n", cmdval);
   1124 
   1125 	if (cmd->c_datalen == 0) {
   1126 		MMC_WRITE(sc, SUNXI_MMC_CMD, cmdval | cmd->c_opcode);
   1127 	} else {
   1128 		cmd->c_resid = cmd->c_datalen;
   1129 		cmd->c_error = sunxi_mmc_dma_prepare(sc, cmd);
   1130 		MMC_WRITE(sc, SUNXI_MMC_CMD, cmdval | cmd->c_opcode);
   1131 		if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_CMD_READ)) {
   1132 			const uint32_t idst_mask = SUNXI_MMC_IDST_RECEIVE_INT;
   1133 
   1134 			retry = 10;
   1135 			while ((sc->sc_idma_idst & idst_mask) == 0) {
   1136 				if (retry-- == 0) {
   1137 					cmd->c_error = ETIMEDOUT;
   1138 					break;
   1139 				}
   1140 				cv_timedwait(&sc->sc_idst_cv,
   1141 				    &sc->sc_intr_lock, hz);
   1142 			}
   1143 		}
   1144 	}
   1145 
   1146 	cmd->c_error = sunxi_mmc_wait_rint(sc,
   1147 	    SUNXI_MMC_INT_ERROR|SUNXI_MMC_INT_CMD_DONE, hz * 10, poll);
   1148 	if (cmd->c_error == 0 && (sc->sc_intr_rint & SUNXI_MMC_INT_ERROR)) {
   1149 		if (sc->sc_intr_rint & SUNXI_MMC_INT_RESP_TIMEOUT) {
   1150 			cmd->c_error = ETIMEDOUT;
   1151 		} else {
   1152 			cmd->c_error = EIO;
   1153 		}
   1154 	}
   1155 	if (cmd->c_error) {
   1156 		DPRINTF(sc->sc_dev,
   1157 		    "cmd failed, error %d\n", cmd->c_error);
   1158 		goto done;
   1159 	}
   1160 
   1161 	if (cmd->c_datalen > 0) {
   1162 		sunxi_mmc_dma_complete(sc, cmd);
   1163 
   1164 		cmd->c_error = sunxi_mmc_wait_rint(sc,
   1165 		    SUNXI_MMC_INT_ERROR|
   1166 		    SUNXI_MMC_INT_AUTO_CMD_DONE|
   1167 		    SUNXI_MMC_INT_DATA_OVER,
   1168 		    hz*10, poll);
   1169 		if (cmd->c_error == 0 &&
   1170 		    (sc->sc_intr_rint & SUNXI_MMC_INT_ERROR)) {
   1171 			cmd->c_error = ETIMEDOUT;
   1172 		}
   1173 		if (cmd->c_error) {
   1174 			DPRINTF(sc->sc_dev,
   1175 			    "data timeout, rint = %08x\n",
   1176 			    sc->sc_intr_rint);
   1177 			cmd->c_error = ETIMEDOUT;
   1178 			goto done;
   1179 		}
   1180 	}
   1181 
   1182 	if (cmd->c_flags & SCF_RSP_PRESENT) {
   1183 		if (cmd->c_flags & SCF_RSP_136) {
   1184 			cmd->c_resp[0] = MMC_READ(sc, SUNXI_MMC_RESP0);
   1185 			cmd->c_resp[1] = MMC_READ(sc, SUNXI_MMC_RESP1);
   1186 			cmd->c_resp[2] = MMC_READ(sc, SUNXI_MMC_RESP2);
   1187 			cmd->c_resp[3] = MMC_READ(sc, SUNXI_MMC_RESP3);
   1188 			if (cmd->c_flags & SCF_RSP_CRC) {
   1189 				cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
   1190 				    (cmd->c_resp[1] << 24);
   1191 				cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
   1192 				    (cmd->c_resp[2] << 24);
   1193 				cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
   1194 				    (cmd->c_resp[3] << 24);
   1195 				cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
   1196 			}
   1197 		} else {
   1198 			cmd->c_resp[0] = MMC_READ(sc, SUNXI_MMC_RESP0);
   1199 		}
   1200 	}
   1201 
   1202 done:
   1203 	cmd->c_flags |= SCF_ITSDONE;
   1204 	MMC_WRITE(sc, SUNXI_MMC_IMASK, oimask);
   1205 	MMC_WRITE(sc, SUNXI_MMC_RINT, 0xffff);
   1206 	MMC_WRITE(sc, SUNXI_MMC_IDST, 0x337);
   1207 	mutex_exit(&sc->sc_intr_lock);
   1208 
   1209 	if (cmd->c_error) {
   1210 		DPRINTF(sc->sc_dev, "i/o error %d\n", cmd->c_error);
   1211 		MMC_WRITE(sc, SUNXI_MMC_GCTRL,
   1212 		    MMC_READ(sc, SUNXI_MMC_GCTRL) |
   1213 		      SUNXI_MMC_GCTRL_DMARESET | SUNXI_MMC_GCTRL_FIFORESET);
   1214 		for (retry = 0; retry < 1000; retry++) {
   1215 			if (!(MMC_READ(sc, SUNXI_MMC_GCTRL) & SUNXI_MMC_GCTRL_RESET))
   1216 				break;
   1217 			delay(10);
   1218 		}
   1219 		sunxi_mmc_update_clock(sc);
   1220 	}
   1221 
   1222 	MMC_WRITE(sc, SUNXI_MMC_GCTRL,
   1223 	    MMC_READ(sc, SUNXI_MMC_GCTRL) | SUNXI_MMC_GCTRL_FIFORESET);
   1224 }
   1225 
   1226 static void
   1227 sunxi_mmc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
   1228 {
   1229 	struct sunxi_mmc_softc *sc = sch;
   1230 	uint32_t imask;
   1231 
   1232 	imask = MMC_READ(sc, SUNXI_MMC_IMASK);
   1233 	if (enable)
   1234 		imask |= SUNXI_MMC_INT_SDIO_INT;
   1235 	else
   1236 		imask &= ~SUNXI_MMC_INT_SDIO_INT;
   1237 	MMC_WRITE(sc, SUNXI_MMC_IMASK, imask);
   1238 }
   1239 
   1240 static void
   1241 sunxi_mmc_card_intr_ack(sdmmc_chipset_handle_t sch)
   1242 {
   1243 	struct sunxi_mmc_softc *sc = sch;
   1244 
   1245 	MMC_WRITE(sc, SUNXI_MMC_RINT, SUNXI_MMC_INT_SDIO_INT);
   1246 }
   1247