Home | History | Annotate | Line # | Download | only in ic
dwc_mmc.c revision 1.9
      1 /* $NetBSD: dwc_mmc.c,v 1.9 2015/12/27 18:35:12 jmcneill Exp $ */
      2 
      3 #define DWC_MMC_DEBUG
      4 
      5 /*-
      6  * Copyright (c) 2014 Jared D. McNeill <jmcneill (at) invisible.ca>
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include "opt_dwc_mmc.h"
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: dwc_mmc.c,v 1.9 2015/12/27 18:35:12 jmcneill Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/bus.h>
     38 #include <sys/device.h>
     39 #include <sys/intr.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 
     43 #include <dev/sdmmc/sdmmcvar.h>
     44 #include <dev/sdmmc/sdmmcchip.h>
     45 #include <dev/sdmmc/sdmmc_ioreg.h>
     46 
     47 #include <dev/ic/dwc_mmc_reg.h>
     48 #include <dev/ic/dwc_mmc_var.h>
     49 
     50 static int	dwc_mmc_host_reset(sdmmc_chipset_handle_t);
     51 static uint32_t	dwc_mmc_host_ocr(sdmmc_chipset_handle_t);
     52 static int	dwc_mmc_host_maxblklen(sdmmc_chipset_handle_t);
     53 static int	dwc_mmc_card_detect(sdmmc_chipset_handle_t);
     54 static int	dwc_mmc_write_protect(sdmmc_chipset_handle_t);
     55 static int	dwc_mmc_bus_power(sdmmc_chipset_handle_t, uint32_t);
     56 static int	dwc_mmc_bus_clock(sdmmc_chipset_handle_t, int);
     57 static int	dwc_mmc_bus_width(sdmmc_chipset_handle_t, int);
     58 static int	dwc_mmc_bus_rod(sdmmc_chipset_handle_t, int);
     59 static void	dwc_mmc_exec_command(sdmmc_chipset_handle_t,
     60 				     struct sdmmc_command *);
     61 static void	dwc_mmc_card_enable_intr(sdmmc_chipset_handle_t, int);
     62 static void	dwc_mmc_card_intr_ack(sdmmc_chipset_handle_t);
     63 
     64 static int	dwc_mmc_set_clock(struct dwc_mmc_softc *, u_int);
     65 static int	dwc_mmc_update_clock(struct dwc_mmc_softc *);
     66 static int	dwc_mmc_wait_rint(struct dwc_mmc_softc *, uint32_t, int);
     67 static int	dwc_mmc_pio_wait(struct dwc_mmc_softc *,
     68 				 struct sdmmc_command *);
     69 static int	dwc_mmc_pio_transfer(struct dwc_mmc_softc *,
     70 				     struct sdmmc_command *);
     71 
     72 #ifdef DWC_MMC_DEBUG
     73 static void	dwc_mmc_print_rint(struct dwc_mmc_softc *, const char *,
     74 				   uint32_t);
     75 #endif
     76 
     77 void		dwc_mmc_dump_regs(int);
     78 
     79 static struct sdmmc_chip_functions dwc_mmc_chip_functions = {
     80 	.host_reset = dwc_mmc_host_reset,
     81 	.host_ocr = dwc_mmc_host_ocr,
     82 	.host_maxblklen = dwc_mmc_host_maxblklen,
     83 	.card_detect = dwc_mmc_card_detect,
     84 	.write_protect = dwc_mmc_write_protect,
     85 	.bus_power = dwc_mmc_bus_power,
     86 	.bus_clock = dwc_mmc_bus_clock,
     87 	.bus_width = dwc_mmc_bus_width,
     88 	.bus_rod = dwc_mmc_bus_rod,
     89 	.exec_command = dwc_mmc_exec_command,
     90 	.card_enable_intr = dwc_mmc_card_enable_intr,
     91 	.card_intr_ack = dwc_mmc_card_intr_ack,
     92 };
     93 
     94 #define MMC_WRITE(sc, reg, val) \
     95 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     96 #define MMC_READ(sc, reg) \
     97 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     98 
     99 void
    100 dwc_mmc_init(struct dwc_mmc_softc *sc)
    101 {
    102 	struct sdmmcbus_attach_args saa;
    103 
    104 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
    105 	cv_init(&sc->sc_intr_cv, "dwcmmcirq");
    106 
    107 #ifdef DWC_MMC_DEBUG
    108 	const uint32_t verid = MMC_READ(sc, DWC_MMC_VERID_REG);
    109 	aprint_normal_dev(sc->sc_dev, "version 0x%04x\n", verid & 0xffff);
    110 #endif
    111 
    112 	dwc_mmc_host_reset(sc);
    113 	dwc_mmc_bus_width(sc, 1);
    114 
    115 	memset(&saa, 0, sizeof(saa));
    116 	saa.saa_busname = "sdmmc";
    117 	saa.saa_sct = &dwc_mmc_chip_functions;
    118 	saa.saa_sch = sc;
    119 	saa.saa_clkmin = 400;
    120 	if (sc->sc_clock_max) {
    121 		saa.saa_clkmax = sc->sc_clock_max;
    122 	} else {
    123 		saa.saa_clkmax = sc->sc_clock_freq / 1000;
    124 	}
    125 	saa.saa_caps = SMC_CAPS_4BIT_MODE|
    126 		       SMC_CAPS_8BIT_MODE|
    127 		       SMC_CAPS_SD_HIGHSPEED|
    128 		       SMC_CAPS_MMC_HIGHSPEED|
    129 		       SMC_CAPS_AUTO_STOP;
    130 
    131 #if notyet
    132 	saa.saa_dmat = sc->sc_dmat;
    133 	saa.saa_caps |= SMC_CAPS_DMA|
    134 			SMC_CAPS_MULTI_SEG_DMA;
    135 #endif
    136 
    137 	sc->sc_sdmmc_dev = config_found(sc->sc_dev, &saa, NULL);
    138 }
    139 
    140 int
    141 dwc_mmc_intr(void *priv)
    142 {
    143 	struct dwc_mmc_softc *sc = priv;
    144 	uint32_t mint, rint;
    145 
    146 	mutex_enter(&sc->sc_intr_lock);
    147 	rint = MMC_READ(sc, DWC_MMC_RINTSTS_REG);
    148 	mint = MMC_READ(sc, DWC_MMC_MINTSTS_REG);
    149 	if (!rint && !mint) {
    150 		mutex_exit(&sc->sc_intr_lock);
    151 		return 0;
    152 	}
    153 	MMC_WRITE(sc, DWC_MMC_RINTSTS_REG, rint);
    154 	MMC_WRITE(sc, DWC_MMC_MINTSTS_REG, mint);
    155 
    156 #ifdef DWC_MMC_DEBUG
    157 	dwc_mmc_print_rint(sc, "irq", rint);
    158 #endif
    159 
    160 	if (rint & DWC_MMC_INT_CARDDET) {
    161 		rint &= ~DWC_MMC_INT_CARDDET;
    162 		if (sc->sc_sdmmc_dev) {
    163 			sdmmc_needs_discover(sc->sc_sdmmc_dev);
    164 		}
    165 	}
    166 
    167 	if (rint) {
    168 		sc->sc_intr_rint |= rint;
    169 		cv_broadcast(&sc->sc_intr_cv);
    170 	}
    171 
    172 	mutex_exit(&sc->sc_intr_lock);
    173 
    174 	return 1;
    175 }
    176 
    177 static int
    178 dwc_mmc_set_clock(struct dwc_mmc_softc *sc, u_int freq)
    179 {
    180 	const u_int pll_freq = sc->sc_clock_freq / 1000;
    181 	const u_int clk_div = howmany(pll_freq, freq * 2);
    182 
    183 #ifdef DWC_MMC_DEBUG
    184 	printf("%s: using clk_div %d for freq %d (act %u)\n",
    185 	    __func__, clk_div, freq, pll_freq / (clk_div * 2));
    186 #endif
    187 
    188 	MMC_WRITE(sc, DWC_MMC_CLKDIV_REG,
    189 	    __SHIFTIN(clk_div, DWC_MMC_CLKDIV_CLK_DIVIDER0));
    190 	MMC_WRITE(sc, DWC_MMC_CLKSRC_REG, 0);	/* clock divider 0 */
    191 
    192 	return dwc_mmc_update_clock(sc);
    193 }
    194 
    195 static int
    196 dwc_mmc_update_clock(struct dwc_mmc_softc *sc)
    197 {
    198 	uint32_t cmd;
    199 	int retry;
    200 
    201 	cmd = DWC_MMC_CMD_START_CMD |
    202 	      DWC_MMC_CMD_UPDATE_CLOCK_REGS_ONLY |
    203 	      DWC_MMC_CMD_WAIT_PRVDATA_COMPLETE;
    204 
    205 	if (sc->sc_flags & DWC_MMC_F_USE_HOLD_REG)
    206 		cmd |= DWC_MMC_CMD_USE_HOLD_REG;
    207 
    208 	MMC_WRITE(sc, DWC_MMC_CMD_REG, cmd);
    209 	retry = 0xfffff;
    210 	while (--retry > 0) {
    211 		cmd = MMC_READ(sc, DWC_MMC_CMD_REG);
    212 		if ((cmd & DWC_MMC_CMD_START_CMD) == 0)
    213 			break;
    214 		delay(10);
    215 	}
    216 
    217 	if (retry == 0) {
    218 		device_printf(sc->sc_dev, "timeout updating clock\n");
    219 		return ETIMEDOUT;
    220 	}
    221 
    222 	return 0;
    223 }
    224 
    225 static int
    226 dwc_mmc_wait_rint(struct dwc_mmc_softc *sc, uint32_t mask, int timeout)
    227 {
    228 	int retry, error;
    229 
    230 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    231 
    232 	if (sc->sc_intr_rint & mask)
    233 		return 0;
    234 
    235 	retry = timeout / hz;
    236 
    237 	while (retry > 0) {
    238 		error = cv_timedwait(&sc->sc_intr_cv, &sc->sc_intr_lock, hz);
    239 		if (error && error != EWOULDBLOCK)
    240 			return error;
    241 		if (sc->sc_intr_rint & mask)
    242 			return 0;
    243 		--retry;
    244 	}
    245 
    246 	return ETIMEDOUT;
    247 }
    248 
    249 static int
    250 dwc_mmc_pio_wait(struct dwc_mmc_softc *sc, struct sdmmc_command *cmd)
    251 {
    252 	int retry = 0xfffff;
    253 	uint32_t bit = (cmd->c_flags & SCF_CMD_READ) ?
    254 	    DWC_MMC_STATUS_FIFO_EMPTY : DWC_MMC_STATUS_FIFO_FULL;
    255 
    256 	while (--retry > 0) {
    257 		uint32_t status = MMC_READ(sc, DWC_MMC_STATUS_REG);
    258 		if (!(status & bit))
    259 			return 0;
    260 		delay(10);
    261 	}
    262 
    263 #ifdef DWC_MMC_DEBUG
    264 	device_printf(sc->sc_dev, "%s: timed out\n", __func__);
    265 #endif
    266 
    267 	return ETIMEDOUT;
    268 }
    269 
    270 static int
    271 dwc_mmc_pio_transfer(struct dwc_mmc_softc *sc, struct sdmmc_command *cmd)
    272 {
    273 	uint32_t *datap = (uint32_t *)cmd->c_data;
    274 	int i;
    275 
    276 	for (i = 0; i < (cmd->c_resid >> 2); i++) {
    277 		if (dwc_mmc_pio_wait(sc, cmd))
    278 			return ETIMEDOUT;
    279 		if (cmd->c_flags & SCF_CMD_READ) {
    280 			datap[i] = MMC_READ(sc, DWC_MMC_FIFO_BASE_REG);
    281 		} else {
    282 			MMC_WRITE(sc, DWC_MMC_FIFO_BASE_REG, datap[i]);
    283 		}
    284 	}
    285 
    286 	return 0;
    287 }
    288 
    289 static int
    290 dwc_mmc_host_reset(sdmmc_chipset_handle_t sch)
    291 {
    292 	struct dwc_mmc_softc *sc = sch;
    293 	int retry = 1000;
    294 	uint32_t ctrl, fifoth;
    295 	uint32_t rx_wmark, tx_wmark;
    296 
    297 	if (sc->sc_flags & DWC_MMC_F_PWREN_CLEAR) {
    298 		MMC_WRITE(sc, DWC_MMC_PWREN_REG, 0);
    299 	} else {
    300 		MMC_WRITE(sc, DWC_MMC_PWREN_REG, DWC_MMC_PWREN_POWER_ENABLE);
    301 	}
    302 
    303 	MMC_WRITE(sc, DWC_MMC_CTRL_REG, DWC_MMC_CTRL_RESET_ALL);
    304 	while (--retry > 0) {
    305 		ctrl = MMC_READ(sc, DWC_MMC_CTRL_REG);
    306 		if ((ctrl & DWC_MMC_CTRL_RESET_ALL) == 0)
    307 			break;
    308 		delay(100);
    309 	}
    310 
    311 	MMC_WRITE(sc, DWC_MMC_CLKSRC_REG, 0);
    312 
    313 	MMC_WRITE(sc, DWC_MMC_TMOUT_REG, 0xffffff40);
    314 	MMC_WRITE(sc, DWC_MMC_RINTSTS_REG, 0xffffffff);
    315 
    316 	MMC_WRITE(sc, DWC_MMC_INTMASK_REG,
    317 	    DWC_MMC_INT_CD | DWC_MMC_INT_ACD | DWC_MMC_INT_DTO |
    318 	    DWC_MMC_INT_ERROR | DWC_MMC_INT_CARDDET |
    319 	    DWC_MMC_INT_RXDR | DWC_MMC_INT_TXDR);
    320 
    321 	rx_wmark = (sc->sc_fifo_depth / 2) - 1;
    322 	tx_wmark = sc->sc_fifo_depth / 2;
    323 	fifoth = __SHIFTIN(DWC_MMC_FIFOTH_DMA_MULTIPLE_TXN_SIZE_16,
    324 			   DWC_MMC_FIFOTH_DMA_MULTIPLE_TXN_SIZE);
    325 	fifoth |= __SHIFTIN(rx_wmark, DWC_MMC_FIFOTH_RX_WMARK);
    326 	fifoth |= __SHIFTIN(tx_wmark, DWC_MMC_FIFOTH_TX_WMARK);
    327 	MMC_WRITE(sc, DWC_MMC_FIFOTH_REG, fifoth);
    328 
    329 	ctrl = MMC_READ(sc, DWC_MMC_CTRL_REG);
    330 	ctrl |= DWC_MMC_CTRL_INT_ENABLE;
    331 	MMC_WRITE(sc, DWC_MMC_CTRL_REG, ctrl);
    332 
    333 	return 0;
    334 }
    335 
    336 static uint32_t
    337 dwc_mmc_host_ocr(sdmmc_chipset_handle_t sch)
    338 {
    339 	return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V;
    340 }
    341 
    342 static int
    343 dwc_mmc_host_maxblklen(sdmmc_chipset_handle_t sch)
    344 {
    345 	return 32768;
    346 }
    347 
    348 static int
    349 dwc_mmc_card_detect(sdmmc_chipset_handle_t sch)
    350 {
    351 	struct dwc_mmc_softc *sc = sch;
    352 	uint32_t cdetect;
    353 
    354 	if (sc->sc_flags & DWC_MMC_F_BROKEN_CD) {
    355 		return 1;
    356 	} else if (sc->sc_card_detect) {
    357 		return sc->sc_card_detect(sc);
    358 	} else {
    359 		cdetect = MMC_READ(sc, DWC_MMC_CDETECT_REG);
    360 		return !(cdetect & DWC_MMC_CDETECT_CARD_DETECT_N);
    361 	}
    362 }
    363 
    364 static int
    365 dwc_mmc_write_protect(sdmmc_chipset_handle_t sch)
    366 {
    367 	struct dwc_mmc_softc *sc = sch;
    368 	uint32_t wrtprt;
    369 
    370 	wrtprt = MMC_READ(sc, DWC_MMC_WRTPRT_REG);
    371 	return !!(wrtprt & DWC_MMC_WRTPRT_WRITE_PROTECT);
    372 }
    373 
    374 static int
    375 dwc_mmc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    376 {
    377 	return 0;
    378 }
    379 
    380 static int
    381 dwc_mmc_bus_clock(sdmmc_chipset_handle_t sch, int freq)
    382 {
    383 	struct dwc_mmc_softc *sc = sch;
    384 	uint32_t clkena;
    385 
    386 #ifdef DWC_MMC_DEBUG
    387 	device_printf(sc->sc_dev, "%s: freq %d\n", __func__, freq);
    388 #endif
    389 
    390 	MMC_WRITE(sc, DWC_MMC_CLKENA_REG, 0);
    391 	if (dwc_mmc_update_clock(sc) != 0)
    392 		return ETIMEDOUT;
    393 
    394 	if (freq) {
    395 		if (dwc_mmc_set_clock(sc, freq) != 0)
    396 			return EIO;
    397 
    398 		clkena = DWC_MMC_CLKENA_CCLK_ENABLE;
    399 		clkena |= DWC_MMC_CLKENA_CCLK_LOW_POWER; /* XXX SD/MMC only */
    400 		MMC_WRITE(sc, DWC_MMC_CLKENA_REG, clkena);
    401 		if (dwc_mmc_update_clock(sc) != 0)
    402 			return ETIMEDOUT;
    403 	}
    404 
    405 	delay(1000);
    406 
    407 	sc->sc_cur_freq = freq;
    408 
    409 	return 0;
    410 }
    411 
    412 static int
    413 dwc_mmc_bus_width(sdmmc_chipset_handle_t sch, int width)
    414 {
    415 	struct dwc_mmc_softc *sc = sch;
    416 	uint32_t ctype;
    417 
    418 	switch (width) {
    419 	case 1:
    420 		ctype = DWC_MMC_CTYPE_CARD_WIDTH_1;
    421 		break;
    422 	case 4:
    423 		ctype = DWC_MMC_CTYPE_CARD_WIDTH_4;
    424 		break;
    425 	case 8:
    426 		ctype = DWC_MMC_CTYPE_CARD_WIDTH_8;
    427 		break;
    428 	default:
    429 		return EINVAL;
    430 	}
    431 
    432 	MMC_WRITE(sc, DWC_MMC_CTYPE_REG, ctype);
    433 
    434 	return 0;
    435 }
    436 
    437 static int
    438 dwc_mmc_bus_rod(sdmmc_chipset_handle_t sch, int on)
    439 {
    440 	return ENOTSUP;
    441 }
    442 
    443 static void
    444 dwc_mmc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
    445 {
    446 	struct dwc_mmc_softc *sc = sch;
    447 	uint32_t cmdval = DWC_MMC_CMD_START_CMD;
    448 	uint32_t ctrl;
    449 
    450 #ifdef DWC_MMC_DEBUG
    451 	device_printf(sc->sc_dev, "exec opcode=%d flags=%#x\n",
    452 	    cmd->c_opcode, cmd->c_flags);
    453 #endif
    454 
    455 	if (sc->sc_flags & DWC_MMC_F_FORCE_CLK) {
    456 		cmd->c_error = dwc_mmc_bus_clock(sc, sc->sc_cur_freq);
    457 		if (cmd->c_error)
    458 			return;
    459 	}
    460 
    461 	if (sc->sc_flags & DWC_MMC_F_USE_HOLD_REG)
    462 		cmdval |= DWC_MMC_CMD_USE_HOLD_REG;
    463 
    464 	mutex_enter(&sc->sc_intr_lock);
    465 
    466 	MMC_WRITE(sc, DWC_MMC_RINTSTS_REG, 0xffffffff);
    467 
    468 	if (cmd->c_opcode == 0)
    469 		cmdval |= DWC_MMC_CMD_SEND_INIT;
    470 	if (cmd->c_flags & SCF_RSP_PRESENT)
    471 		cmdval |= DWC_MMC_CMD_RESP_EXPECTED;
    472 	if (cmd->c_flags & SCF_RSP_136)
    473 		cmdval |= DWC_MMC_CMD_RESP_LEN;
    474 	if (cmd->c_flags & SCF_RSP_CRC)
    475 		cmdval |= DWC_MMC_CMD_CHECK_RESP_CRC;
    476 
    477 	if (cmd->c_datalen > 0) {
    478 		unsigned int nblks;
    479 
    480 		cmdval |= DWC_MMC_CMD_DATA_EXPECTED;
    481 		cmdval |= DWC_MMC_CMD_WAIT_PRVDATA_COMPLETE;
    482 		if (!ISSET(cmd->c_flags, SCF_CMD_READ)) {
    483 			cmdval |= DWC_MMC_CMD_WR;
    484 		}
    485 
    486 		nblks = cmd->c_datalen / cmd->c_blklen;
    487 		if (nblks == 0 || (cmd->c_datalen % cmd->c_blklen) != 0)
    488 			++nblks;
    489 
    490 		if (nblks > 1) {
    491 			cmdval |= DWC_MMC_CMD_SEND_AUTO_STOP;
    492 		}
    493 
    494 		MMC_WRITE(sc, DWC_MMC_BLKSIZ_REG, cmd->c_blklen);
    495 		MMC_WRITE(sc, DWC_MMC_BYTCNT_REG, nblks * cmd->c_blklen);
    496 	}
    497 
    498 	sc->sc_intr_rint = 0;
    499 
    500 	MMC_WRITE(sc, DWC_MMC_CMDARG_REG, cmd->c_arg);
    501 
    502 	cmd->c_resid = cmd->c_datalen;
    503 	MMC_WRITE(sc, DWC_MMC_CMD_REG, cmdval | cmd->c_opcode);
    504 
    505 	cmd->c_error = dwc_mmc_wait_rint(sc,
    506 	    DWC_MMC_INT_ERROR|DWC_MMC_INT_CD, hz * 5);
    507 	if (cmd->c_error == 0 && (sc->sc_intr_rint & DWC_MMC_INT_ERROR)) {
    508 #ifdef DWC_MMC_DEBUG
    509 		dwc_mmc_print_rint(sc, "exec1", sc->sc_intr_rint);
    510 #endif
    511 		if (sc->sc_intr_rint & DWC_MMC_INT_RTO) {
    512 			cmd->c_error = ETIMEDOUT;
    513 		} else {
    514 			cmd->c_error = EIO;
    515 		}
    516 	}
    517 	if (cmd->c_error) {
    518 		goto done;
    519 	}
    520 
    521 	if (cmd->c_datalen > 0) {
    522 		cmd->c_error = dwc_mmc_pio_transfer(sc, cmd);
    523 		if (cmd->c_error) {
    524 			goto done;
    525 		}
    526 
    527 		cmd->c_error = dwc_mmc_wait_rint(sc,
    528 		    DWC_MMC_INT_ERROR|DWC_MMC_INT_ACD|DWC_MMC_INT_DTO,
    529 		    hz * 5);
    530 		if (cmd->c_error == 0 &&
    531 		    (sc->sc_intr_rint & DWC_MMC_INT_ERROR)) {
    532 #ifdef DWC_MMC_DEBUG
    533 			dwc_mmc_print_rint(sc, "exec2", sc->sc_intr_rint);
    534 #endif
    535 			cmd->c_error = ETIMEDOUT;
    536 		}
    537 		if (cmd->c_error) {
    538 			goto done;
    539 		}
    540 	}
    541 
    542 	if (cmd->c_flags & SCF_RSP_PRESENT) {
    543 		if (cmd->c_flags & SCF_RSP_136) {
    544 			cmd->c_resp[0] = MMC_READ(sc, DWC_MMC_RESP0_REG);
    545 			cmd->c_resp[1] = MMC_READ(sc, DWC_MMC_RESP1_REG);
    546 			cmd->c_resp[2] = MMC_READ(sc, DWC_MMC_RESP2_REG);
    547 			cmd->c_resp[3] = MMC_READ(sc, DWC_MMC_RESP3_REG);
    548 			if (cmd->c_flags & SCF_RSP_CRC) {
    549 				cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
    550 				    (cmd->c_resp[1] << 24);
    551 				cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
    552 				    (cmd->c_resp[2] << 24);
    553 				cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
    554 				    (cmd->c_resp[3] << 24);
    555 				cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
    556 			}
    557 		} else {
    558 			cmd->c_resp[0] = MMC_READ(sc, DWC_MMC_RESP0_REG);
    559 		}
    560 	}
    561 
    562 done:
    563 	cmd->c_flags |= SCF_ITSDONE;
    564 	mutex_exit(&sc->sc_intr_lock);
    565 
    566 	if (cmd->c_error == ETIMEDOUT && !ISSET(cmd->c_flags, SCF_TOUT_OK)) {
    567 		device_printf(sc->sc_dev, "Device timeout!\n");
    568 		dwc_mmc_dump_regs(device_unit(sc->sc_dev));
    569 	}
    570 
    571 	ctrl = MMC_READ(sc, DWC_MMC_CTRL_REG);
    572 	ctrl |= DWC_MMC_CTRL_FIFO_RESET;
    573 	MMC_WRITE(sc, DWC_MMC_CTRL_REG, ctrl);
    574 }
    575 
    576 static void
    577 dwc_mmc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
    578 {
    579 }
    580 
    581 static void
    582 dwc_mmc_card_intr_ack(sdmmc_chipset_handle_t sch)
    583 {
    584 }
    585 
    586 #ifdef DWC_MMC_DEBUG
    587 static void
    588 dwc_mmc_print_rint(struct dwc_mmc_softc *sc, const char *tag, uint32_t rint)
    589 {
    590 	char buf[128];
    591 	snprintb(buf, sizeof(buf), DWC_MMC_INT_BITS, rint);
    592 	device_printf(sc->sc_dev, "[%s] rint %s\n", tag, buf);
    593 }
    594 #endif
    595 
    596 void
    597 dwc_mmc_dump_regs(int unit)
    598 {
    599 	static const struct {
    600 		const char *name;
    601 		unsigned int reg;
    602 	} regs[] = {
    603 		{ "CTRL", DWC_MMC_CTRL_REG },
    604 		{ "PWREN", DWC_MMC_PWREN_REG },
    605 		{ "CLKDIV", DWC_MMC_CLKDIV_REG },
    606 		{ "CLKENA", DWC_MMC_CLKENA_REG },
    607 		{ "TMOUT", DWC_MMC_TMOUT_REG },
    608 		{ "CTYPE", DWC_MMC_CTYPE_REG },
    609 		{ "BLKSIZ", DWC_MMC_BLKSIZ_REG },
    610 		{ "BYTCNT", DWC_MMC_BYTCNT_REG },
    611 		{ "INTMASK", DWC_MMC_INTMASK_REG },
    612 		{ "MINTSTS", DWC_MMC_MINTSTS_REG },
    613 		{ "RINTSTS", DWC_MMC_RINTSTS_REG },
    614 		{ "STATUS", DWC_MMC_STATUS_REG },
    615 		{ "CDETECT", DWC_MMC_CDETECT_REG },
    616 		{ "WRTPRT", DWC_MMC_WRTPRT_REG },
    617 		{ "USRID", DWC_MMC_USRID_REG },
    618 		{ "VERID", DWC_MMC_VERID_REG },
    619 		{ "RST", DWC_MMC_RST_REG },
    620 		{ "BACK_END_POWER", DWC_MMC_BACK_END_POWER_REG },
    621 	};
    622 	device_t self = device_find_by_driver_unit("dwcmmc", unit);
    623 	if (self == NULL)
    624 		return;
    625 	struct dwc_mmc_softc *sc = device_private(self);
    626 	int i;
    627 
    628 	for (i = 0; i < __arraycount(regs); i += 2) {
    629 		device_printf(sc->sc_dev, "  %s: 0x%08x\t%s: 0x%08x\n",
    630 		    regs[i+0].name, MMC_READ(sc, regs[i+0].reg),
    631 		    regs[i+1].name, MMC_READ(sc, regs[i+1].reg));
    632 	}
    633 }
    634