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