Home | History | Annotate | Line # | Download | only in ic
dwc_mmc.c revision 1.8
      1 /* $NetBSD: dwc_mmc.c,v 1.8 2015/12/26 23:13:10 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 Jared D. 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_dwc_mmc.h"
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: dwc_mmc.c,v 1.8 2015/12/26 23:13:10 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 
     41 #include <dev/sdmmc/sdmmcvar.h>
     42 #include <dev/sdmmc/sdmmcchip.h>
     43 #include <dev/sdmmc/sdmmc_ioreg.h>
     44 
     45 #include <dev/ic/dwc_mmc_reg.h>
     46 #include <dev/ic/dwc_mmc_var.h>
     47 
     48 static int	dwc_mmc_host_reset(sdmmc_chipset_handle_t);
     49 static uint32_t	dwc_mmc_host_ocr(sdmmc_chipset_handle_t);
     50 static int	dwc_mmc_host_maxblklen(sdmmc_chipset_handle_t);
     51 static int	dwc_mmc_card_detect(sdmmc_chipset_handle_t);
     52 static int	dwc_mmc_write_protect(sdmmc_chipset_handle_t);
     53 static int	dwc_mmc_bus_power(sdmmc_chipset_handle_t, uint32_t);
     54 static int	dwc_mmc_bus_clock(sdmmc_chipset_handle_t, int);
     55 static int	dwc_mmc_bus_width(sdmmc_chipset_handle_t, int);
     56 static int	dwc_mmc_bus_rod(sdmmc_chipset_handle_t, int);
     57 static void	dwc_mmc_exec_command(sdmmc_chipset_handle_t,
     58 				     struct sdmmc_command *);
     59 static void	dwc_mmc_card_enable_intr(sdmmc_chipset_handle_t, int);
     60 static void	dwc_mmc_card_intr_ack(sdmmc_chipset_handle_t);
     61 
     62 static int	dwc_mmc_set_clock(struct dwc_mmc_softc *, u_int);
     63 static int	dwc_mmc_update_clock(struct dwc_mmc_softc *);
     64 static int	dwc_mmc_wait_rint(struct dwc_mmc_softc *, uint32_t, int);
     65 static int	dwc_mmc_pio_wait(struct dwc_mmc_softc *,
     66 				 struct sdmmc_command *);
     67 static int	dwc_mmc_pio_transfer(struct dwc_mmc_softc *,
     68 				     struct sdmmc_command *);
     69 
     70 #ifdef DWC_MMC_DEBUG
     71 static void	dwc_mmc_print_rint(struct dwc_mmc_softc *, const char *,
     72 				   uint32_t);
     73 #endif
     74 
     75 void		dwc_mmc_dump_regs(int);
     76 
     77 static struct sdmmc_chip_functions dwc_mmc_chip_functions = {
     78 	.host_reset = dwc_mmc_host_reset,
     79 	.host_ocr = dwc_mmc_host_ocr,
     80 	.host_maxblklen = dwc_mmc_host_maxblklen,
     81 	.card_detect = dwc_mmc_card_detect,
     82 	.write_protect = dwc_mmc_write_protect,
     83 	.bus_power = dwc_mmc_bus_power,
     84 	.bus_clock = dwc_mmc_bus_clock,
     85 	.bus_width = dwc_mmc_bus_width,
     86 	.bus_rod = dwc_mmc_bus_rod,
     87 	.exec_command = dwc_mmc_exec_command,
     88 	.card_enable_intr = dwc_mmc_card_enable_intr,
     89 	.card_intr_ack = dwc_mmc_card_intr_ack,
     90 };
     91 
     92 #define MMC_WRITE(sc, reg, val) \
     93 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     94 #define MMC_READ(sc, reg) \
     95 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     96 
     97 void
     98 dwc_mmc_init(struct dwc_mmc_softc *sc)
     99 {
    100 	struct sdmmcbus_attach_args saa;
    101 
    102 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
    103 	cv_init(&sc->sc_intr_cv, "dwcmmcirq");
    104 
    105 #ifdef DWC_MMC_DEBUG
    106 	const uint32_t verid = MMC_READ(sc, DWC_MMC_VERID_REG);
    107 	aprint_normal_dev(sc->sc_dev, "version 0x%04x\n", verid & 0xffff);
    108 #endif
    109 
    110 	dwc_mmc_host_reset(sc);
    111 	dwc_mmc_bus_width(sc, 1);
    112 
    113 	memset(&saa, 0, sizeof(saa));
    114 	saa.saa_busname = "sdmmc";
    115 	saa.saa_sct = &dwc_mmc_chip_functions;
    116 	saa.saa_sch = sc;
    117 	saa.saa_clkmin = 400;
    118 	if (sc->sc_clock_max) {
    119 		saa.saa_clkmax = sc->sc_clock_max;
    120 	} else {
    121 		saa.saa_clkmax = sc->sc_clock_freq / 1000;
    122 	}
    123 	saa.saa_caps = SMC_CAPS_4BIT_MODE|
    124 		       SMC_CAPS_8BIT_MODE|
    125 		       SMC_CAPS_SD_HIGHSPEED|
    126 		       SMC_CAPS_MMC_HIGHSPEED|
    127 		       SMC_CAPS_AUTO_STOP;
    128 
    129 #if notyet
    130 	saa.saa_dmat = sc->sc_dmat;
    131 	saa.saa_caps |= SMC_CAPS_DMA|
    132 			SMC_CAPS_MULTI_SEG_DMA;
    133 #endif
    134 
    135 	sc->sc_sdmmc_dev = config_found(sc->sc_dev, &saa, NULL);
    136 }
    137 
    138 int
    139 dwc_mmc_intr(void *priv)
    140 {
    141 	struct dwc_mmc_softc *sc = priv;
    142 	uint32_t mint, rint;
    143 
    144 	mutex_enter(&sc->sc_intr_lock);
    145 	rint = MMC_READ(sc, DWC_MMC_RINTSTS_REG);
    146 	mint = MMC_READ(sc, DWC_MMC_MINTSTS_REG);
    147 	if (!rint && !mint) {
    148 		mutex_exit(&sc->sc_intr_lock);
    149 		return 0;
    150 	}
    151 	MMC_WRITE(sc, DWC_MMC_RINTSTS_REG, rint);
    152 	MMC_WRITE(sc, DWC_MMC_MINTSTS_REG, mint);
    153 
    154 #ifdef DWC_MMC_DEBUG
    155 	dwc_mmc_print_rint(sc, "irq", rint);
    156 #endif
    157 
    158 	if (rint & DWC_MMC_INT_CARDDET) {
    159 		rint &= ~DWC_MMC_INT_CARDDET;
    160 		if (sc->sc_sdmmc_dev) {
    161 			sdmmc_needs_discover(sc->sc_sdmmc_dev);
    162 		}
    163 	}
    164 
    165 	if (rint) {
    166 		sc->sc_intr_rint |= rint;
    167 		cv_broadcast(&sc->sc_intr_cv);
    168 	}
    169 
    170 	mutex_exit(&sc->sc_intr_lock);
    171 
    172 	return 1;
    173 }
    174 
    175 static int
    176 dwc_mmc_set_clock(struct dwc_mmc_softc *sc, u_int freq)
    177 {
    178 	u_int pll_freq, clk_div;
    179 
    180 	pll_freq = sc->sc_clock_freq / 1000;
    181 	clk_div = (pll_freq / freq) >> 1;
    182 	if (pll_freq % freq)
    183 		clk_div++;
    184 
    185 #ifdef DWC_MMC_DEBUG
    186 	printf("%s: using clk_div %d for freq %d (act %u)\n",
    187 	    __func__, clk_div, freq, pll_freq / (clk_div * 2));
    188 #endif
    189 
    190 	MMC_WRITE(sc, DWC_MMC_CLKDIV_REG,
    191 	    __SHIFTIN(clk_div, DWC_MMC_CLKDIV_CLK_DIVIDER0));
    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 	if (cmd->c_opcode == 0)
    466 		cmdval |= DWC_MMC_CMD_SEND_INIT;
    467 	if (cmd->c_flags & SCF_RSP_PRESENT)
    468 		cmdval |= DWC_MMC_CMD_RESP_EXPECTED;
    469 	if (cmd->c_flags & SCF_RSP_136)
    470 		cmdval |= DWC_MMC_CMD_RESP_LEN;
    471 	if (cmd->c_flags & SCF_RSP_CRC)
    472 		cmdval |= DWC_MMC_CMD_CHECK_RESP_CRC;
    473 
    474 	if (cmd->c_datalen > 0) {
    475 		unsigned int nblks;
    476 
    477 		cmdval |= DWC_MMC_CMD_DATA_EXPECTED;
    478 		cmdval |= DWC_MMC_CMD_WAIT_PRVDATA_COMPLETE;
    479 		if (!ISSET(cmd->c_flags, SCF_CMD_READ)) {
    480 			cmdval |= DWC_MMC_CMD_WR;
    481 		}
    482 
    483 		nblks = cmd->c_datalen / cmd->c_blklen;
    484 		if (nblks == 0 || (cmd->c_datalen % cmd->c_blklen) != 0)
    485 			++nblks;
    486 
    487 		if (nblks > 1) {
    488 			cmdval |= DWC_MMC_CMD_SEND_AUTO_STOP;
    489 		}
    490 
    491 		MMC_WRITE(sc, DWC_MMC_BLKSIZ_REG, cmd->c_blklen);
    492 		MMC_WRITE(sc, DWC_MMC_BYTCNT_REG, nblks * cmd->c_blklen);
    493 	}
    494 
    495 	sc->sc_intr_rint = 0;
    496 
    497 	MMC_WRITE(sc, DWC_MMC_CMDARG_REG, cmd->c_arg);
    498 
    499 	cmd->c_resid = cmd->c_datalen;
    500 	MMC_WRITE(sc, DWC_MMC_CMD_REG, cmdval | cmd->c_opcode);
    501 
    502 	cmd->c_error = dwc_mmc_wait_rint(sc,
    503 	    DWC_MMC_INT_ERROR|DWC_MMC_INT_CD, hz * 10);
    504 	if (cmd->c_error == 0 && (sc->sc_intr_rint & DWC_MMC_INT_ERROR)) {
    505 #ifdef DWC_MMC_DEBUG
    506 		dwc_mmc_print_rint(sc, "exec1", sc->sc_intr_rint);
    507 #endif
    508 		if (sc->sc_intr_rint & DWC_MMC_INT_RTO) {
    509 			cmd->c_error = ETIMEDOUT;
    510 		} else {
    511 			cmd->c_error = EIO;
    512 		}
    513 	}
    514 	if (cmd->c_error) {
    515 		goto done;
    516 	}
    517 
    518 	if (cmd->c_datalen > 0) {
    519 		cmd->c_error = dwc_mmc_pio_transfer(sc, cmd);
    520 		if (cmd->c_error) {
    521 			goto done;
    522 		}
    523 
    524 		cmd->c_error = dwc_mmc_wait_rint(sc,
    525 		    DWC_MMC_INT_ERROR|DWC_MMC_INT_ACD|DWC_MMC_INT_DTO,
    526 		    hz * 10);
    527 		if (cmd->c_error == 0 &&
    528 		    (sc->sc_intr_rint & DWC_MMC_INT_ERROR)) {
    529 #ifdef DWC_MMC_DEBUG
    530 			dwc_mmc_print_rint(sc, "exec2", sc->sc_intr_rint);
    531 #endif
    532 			cmd->c_error = ETIMEDOUT;
    533 		}
    534 		if (cmd->c_error) {
    535 			goto done;
    536 		}
    537 	}
    538 
    539 	if (cmd->c_flags & SCF_RSP_PRESENT) {
    540 		if (cmd->c_flags & SCF_RSP_136) {
    541 			cmd->c_resp[0] = MMC_READ(sc, DWC_MMC_RESP0_REG);
    542 			cmd->c_resp[1] = MMC_READ(sc, DWC_MMC_RESP1_REG);
    543 			cmd->c_resp[2] = MMC_READ(sc, DWC_MMC_RESP2_REG);
    544 			cmd->c_resp[3] = MMC_READ(sc, DWC_MMC_RESP3_REG);
    545 			if (cmd->c_flags & SCF_RSP_CRC) {
    546 				cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
    547 				    (cmd->c_resp[1] << 24);
    548 				cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
    549 				    (cmd->c_resp[2] << 24);
    550 				cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
    551 				    (cmd->c_resp[3] << 24);
    552 				cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
    553 			}
    554 		} else {
    555 			cmd->c_resp[0] = MMC_READ(sc, DWC_MMC_RESP0_REG);
    556 		}
    557 	}
    558 
    559 done:
    560 	cmd->c_flags |= SCF_ITSDONE;
    561 	mutex_exit(&sc->sc_intr_lock);
    562 
    563 	if (cmd->c_error == ETIMEDOUT && !ISSET(cmd->c_flags, SCF_TOUT_OK)) {
    564 		device_printf(sc->sc_dev, "Device timeout!\n");
    565 		dwc_mmc_dump_regs(device_unit(sc->sc_dev));
    566 	}
    567 
    568 	ctrl = MMC_READ(sc, DWC_MMC_CTRL_REG);
    569 	ctrl |= DWC_MMC_CTRL_FIFO_RESET;
    570 	MMC_WRITE(sc, DWC_MMC_CTRL_REG, ctrl);
    571 }
    572 
    573 static void
    574 dwc_mmc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
    575 {
    576 }
    577 
    578 static void
    579 dwc_mmc_card_intr_ack(sdmmc_chipset_handle_t sch)
    580 {
    581 }
    582 
    583 #ifdef DWC_MMC_DEBUG
    584 static void
    585 dwc_mmc_print_rint(struct dwc_mmc_softc *sc, const char *tag, uint32_t rint)
    586 {
    587 	char buf[128];
    588 	snprintb(buf, sizeof(buf), DWC_MMC_INT_BITS, rint);
    589 	device_printf(sc->sc_dev, "[%s] rint %s\n", tag, buf);
    590 }
    591 #endif
    592 
    593 void
    594 dwc_mmc_dump_regs(int unit)
    595 {
    596 	static const struct {
    597 		const char *name;
    598 		unsigned int reg;
    599 	} regs[] = {
    600 		{ "CTRL", DWC_MMC_CTRL_REG },
    601 		{ "PWREN", DWC_MMC_PWREN_REG },
    602 		{ "CLKDIV", DWC_MMC_CLKDIV_REG },
    603 		{ "CLKENA", DWC_MMC_CLKENA_REG },
    604 		{ "TMOUT", DWC_MMC_TMOUT_REG },
    605 		{ "CTYPE", DWC_MMC_CTYPE_REG },
    606 		{ "BLKSIZ", DWC_MMC_BLKSIZ_REG },
    607 		{ "BYTCNT", DWC_MMC_BYTCNT_REG },
    608 		{ "INTMASK", DWC_MMC_INTMASK_REG },
    609 		{ "MINTSTS", DWC_MMC_MINTSTS_REG },
    610 		{ "RINTSTS", DWC_MMC_RINTSTS_REG },
    611 		{ "STATUS", DWC_MMC_STATUS_REG },
    612 		{ "CDETECT", DWC_MMC_CDETECT_REG },
    613 		{ "WRTPRT", DWC_MMC_WRTPRT_REG },
    614 		{ "USRID", DWC_MMC_USRID_REG },
    615 		{ "VERID", DWC_MMC_VERID_REG },
    616 		{ "RST", DWC_MMC_RST_REG },
    617 		{ "BACK_END_POWER", DWC_MMC_BACK_END_POWER_REG },
    618 	};
    619 	device_t self = device_find_by_driver_unit("dwcmmc", unit);
    620 	if (self == NULL)
    621 		return;
    622 	struct dwc_mmc_softc *sc = device_private(self);
    623 	int i;
    624 
    625 	for (i = 0; i < __arraycount(regs); i += 2) {
    626 		device_printf(sc->sc_dev, "  %s: 0x%08x\t%s: 0x%08x\n",
    627 		    regs[i+0].name, MMC_READ(sc, regs[i+0].reg),
    628 		    regs[i+1].name, MMC_READ(sc, regs[i+1].reg));
    629 	}
    630 }
    631