Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_sdhost.c revision 1.4
      1 /* $NetBSD: bcm2835_sdhost.c,v 1.4 2017/12/10 21:38:26 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: bcm2835_sdhost.c,v 1.4 2017/12/10 21:38:26 skrll Exp $");
     31 
     32 #include "bcmdmac.h"
     33 
     34 #include <sys/param.h>
     35 #include <sys/bus.h>
     36 #include <sys/device.h>
     37 #include <sys/intr.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/gpio.h>
     41 
     42 #include <arm/broadcom/bcm2835reg.h>
     43 #include <arm/broadcom/bcm2835_dmac.h>
     44 
     45 #include <dev/sdmmc/sdmmcvar.h>
     46 #include <dev/sdmmc/sdmmcchip.h>
     47 #include <dev/sdmmc/sdmmc_ioreg.h>
     48 
     49 #include <dev/fdt/fdtvar.h>
     50 
     51 #include <arm/fdt/arm_fdtvar.h>
     52 
     53 #define	SDCMD		0x00
     54 #define	 SDCMD_NEW	__BIT(15)
     55 #define	 SDCMD_FAIL	__BIT(14)
     56 #define	 SDCMD_BUSY	__BIT(11)
     57 #define	 SDCMD_NORESP	__BIT(10)
     58 #define	 SDCMD_LONGRESP	__BIT(9)
     59 #define	 SDCMD_WRITE	__BIT(7)
     60 #define	 SDCMD_READ	__BIT(6)
     61 #define	SDARG		0x04
     62 #define	SDTOUT		0x08
     63 #define	 SDTOUT_DEFAULT	0xf00000
     64 #define	SDCDIV		0x0c
     65 #define	 SDCDIV_MASK	__BITS(10,0)
     66 #define	SDRSP0		0x10
     67 #define	SDRSP1		0x14
     68 #define	SDRSP2		0x18
     69 #define	SDRSP3		0x1c
     70 #define	SDHSTS		0x20
     71 #define	 SDHSTS_BUSY	__BIT(10)
     72 #define	 SDHSTS_BLOCK	__BIT(9)
     73 #define	 SDHSTS_SDIO	__BIT(8)
     74 #define	 SDHSTS_REW_TO	__BIT(7)
     75 #define	 SDHSTS_CMD_TO	__BIT(6)
     76 #define	 SDHSTS_CRC16_E	__BIT(5)
     77 #define	 SDHSTS_CRC7_E	__BIT(4)
     78 #define	 SDHSTS_FIFO_E	__BIT(3)
     79 #define	 SDHSTS_DATA	__BIT(0)
     80 #define	SDVDD		0x30
     81 #define	 SDVDD_POWER	__BIT(0)
     82 #define	SDEDM		0x34
     83 #define	 SDEDM_RD_FIFO	__BITS(18,14)
     84 #define	 SDEDM_WR_FIFO	__BITS(13,9)
     85 #define	SDHCFG		0x38
     86 #define	 SDHCFG_BUSY_EN	__BIT(10)
     87 #define	 SDHCFG_BLOCK_EN __BIT(8)
     88 #define	 SDHCFG_SDIO_EN	__BIT(5)
     89 #define	 SDHCFG_DATA_EN	__BIT(4)
     90 #define	 SDHCFG_SLOW	__BIT(3)
     91 #define	 SDHCFG_WIDE_EXT __BIT(2)
     92 #define	 SDHCFG_WIDE_INT __BIT(1)
     93 #define	 SDHCFG_REL_CMD	__BIT(0)
     94 #define	SDHBCT		0x3c
     95 #define	SDDATA		0x40
     96 #define	SDHBLC		0x50
     97 
     98 struct sdhost_softc;
     99 
    100 static int	sdhost_match(device_t, cfdata_t, void *);
    101 static void	sdhost_attach(device_t, device_t, void *);
    102 static void	sdhost_attach_i(device_t);
    103 
    104 static int	sdhost_intr(void *);
    105 static int	sdhost_dma_setup(struct sdhost_softc *);
    106 static void	sdhost_dma_done(uint32_t, uint32_t, void *);
    107 
    108 static int	sdhost_host_reset(sdmmc_chipset_handle_t);
    109 static uint32_t	sdhost_host_ocr(sdmmc_chipset_handle_t);
    110 static int	sdhost_host_maxblklen(sdmmc_chipset_handle_t);
    111 static int	sdhost_card_detect(sdmmc_chipset_handle_t);
    112 static int	sdhost_write_protect(sdmmc_chipset_handle_t);
    113 static int	sdhost_bus_power(sdmmc_chipset_handle_t, uint32_t);
    114 static int	sdhost_bus_clock(sdmmc_chipset_handle_t, int, bool);
    115 static int	sdhost_bus_width(sdmmc_chipset_handle_t, int);
    116 static int	sdhost_bus_rod(sdmmc_chipset_handle_t, int);
    117 static void	sdhost_exec_command(sdmmc_chipset_handle_t,
    118 				      struct sdmmc_command *);
    119 static void	sdhost_card_enable_intr(sdmmc_chipset_handle_t, int);
    120 static void	sdhost_card_intr_ack(sdmmc_chipset_handle_t);
    121 
    122 static struct sdmmc_chip_functions sdhost_chip_functions = {
    123 	.host_reset = sdhost_host_reset,
    124 	.host_ocr = sdhost_host_ocr,
    125 	.host_maxblklen = sdhost_host_maxblklen,
    126 	.card_detect = sdhost_card_detect,
    127 	.write_protect = sdhost_write_protect,
    128 	.bus_power = sdhost_bus_power,
    129 	.bus_clock_ddr = sdhost_bus_clock,
    130 	.bus_width = sdhost_bus_width,
    131 	.bus_rod = sdhost_bus_rod,
    132 	.exec_command = sdhost_exec_command,
    133 	.card_enable_intr = sdhost_card_enable_intr,
    134 	.card_intr_ack = sdhost_card_intr_ack,
    135 };
    136 
    137 struct sdhost_softc {
    138 	device_t sc_dev;
    139 	bus_space_tag_t sc_bst;
    140 	bus_space_handle_t sc_bsh;
    141 	bus_dma_tag_t sc_dmat;
    142 
    143 	bus_addr_t sc_addr;
    144 
    145 	void *sc_ih;
    146 	kmutex_t sc_intr_lock;
    147 	kcondvar_t sc_intr_cv;
    148 	kcondvar_t sc_dma_cv;
    149 
    150 	u_int sc_rate;
    151 
    152 	int sc_mmc_width;
    153 	int sc_mmc_present;
    154 
    155 	device_t sc_sdmmc_dev;
    156 
    157 	struct bcm_dmac_channel *sc_dmac;
    158 
    159 	bus_dmamap_t sc_dmamap;
    160 	bus_dma_segment_t sc_segs[1];
    161 	struct bcm_dmac_conblk *sc_cblk;
    162 
    163 	uint32_t sc_intr_hsts;
    164 
    165 	uint32_t sc_dma_status;
    166 	uint32_t sc_dma_error;
    167 };
    168 
    169 CFATTACH_DECL_NEW(bcmsdhost, sizeof(struct sdhost_softc),
    170 	sdhost_match, sdhost_attach, NULL, NULL);
    171 
    172 #define SDHOST_WRITE(sc, reg, val)	\
    173 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    174 #define SDHOST_READ(sc, reg) \
    175 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    176 
    177 static int
    178 sdhost_match(device_t parent, cfdata_t cf, void *aux)
    179 {
    180 	const char * const compatible[] = {
    181 	    "brcm,bcm2835-sdhost",
    182 	    NULL
    183 	};
    184 	struct fdt_attach_args * const faa = aux;
    185 
    186 	return of_match_compatible(faa->faa_phandle, compatible);
    187 }
    188 
    189 static void
    190 sdhost_attach(device_t parent, device_t self, void *aux)
    191 {
    192 	struct sdhost_softc * const sc = device_private(self);
    193 	struct fdt_attach_args * const faa = aux;
    194 	prop_dictionary_t dict = device_properties(self);
    195 	bool disable = false;
    196 
    197 	sc->sc_dev = self;
    198 	sc->sc_bst = faa->faa_bst;
    199 	sc->sc_dmat = faa->faa_dmat;
    200 
    201 	const int phandle = faa->faa_phandle;
    202 	bus_addr_t addr;
    203 	bus_size_t size;
    204 
    205 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    206 		aprint_error(": missing 'reg' property\n");
    207 		return;
    208 	}
    209 
    210 	sc->sc_addr = addr;
    211 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
    212 	cv_init(&sc->sc_intr_cv, "sdhostintr");
    213 	cv_init(&sc->sc_dma_cv, "sdhostdma");
    214 
    215 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    216 		aprint_error(": couldn't map registers\n");
    217 		return;
    218 	}
    219 
    220 	aprint_naive("\n");
    221 	aprint_normal(": SD HOST controller\n");
    222 
    223 	prop_dictionary_get_bool(dict, "disable", &disable);
    224 	if (disable) {
    225 		aprint_naive(": disabled\n");
    226 		aprint_normal(": disabled\n");
    227 		return;
    228 	}
    229 	/* Enable clocks */
    230 	struct clk *clk;
    231 	for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++) {
    232 		if (clk_enable(clk) != 0) {
    233 			aprint_error(": failed to enable clock #%d\n", i);
    234 			return;
    235 		}
    236 		if (i == 0)
    237 			sc->sc_rate = clk_get_rate(clk);
    238 	}
    239 
    240 	aprint_debug_dev(self, "ref freq %u Hz\n", sc->sc_rate);
    241 
    242 	if (sdhost_dma_setup(sc) != 0) {
    243 		aprint_error_dev(self, "failed to setup DMA\n");
    244 		return;
    245 	}
    246 
    247 	char intrstr[128];
    248 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    249 		aprint_error(": failed to decode interrupt\n");
    250 		return;
    251 	}
    252 
    253 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SDMMC,
    254 	    FDT_INTR_MPSAFE, sdhost_intr, sc);
    255 	if (sc->sc_ih == NULL) {
    256 		aprint_error_dev(self, "failed to establish interrupt %s\n",
    257 		    intrstr);
    258 		return;
    259 	}
    260 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    261 
    262 	config_interrupts(self, sdhost_attach_i);
    263 }
    264 
    265 static int
    266 sdhost_dma_setup(struct sdhost_softc *sc)
    267 {
    268 	int error, rseg;
    269 
    270 	sc->sc_dmac = bcm_dmac_alloc(BCM_DMAC_TYPE_NORMAL, IPL_SDMMC,
    271 	    sdhost_dma_done, sc);
    272 	if (sc->sc_dmac == NULL)
    273 		return ENXIO;
    274 
    275 	error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
    276 	    PAGE_SIZE, sc->sc_segs, 1, &rseg, BUS_DMA_WAITOK);
    277 	if (error)
    278 		return error;
    279 
    280 	error = bus_dmamem_map(sc->sc_dmat, sc->sc_segs, rseg, PAGE_SIZE,
    281 	    (void **)&sc->sc_cblk, BUS_DMA_WAITOK);
    282 	if (error)
    283 		return error;
    284 
    285 	memset(sc->sc_cblk, 0, PAGE_SIZE);
    286 
    287 	error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
    288 	    BUS_DMA_WAITOK, &sc->sc_dmamap);
    289 	if (error)
    290 		return error;
    291 
    292 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cblk,
    293 	    PAGE_SIZE, NULL, BUS_DMA_WAITOK|BUS_DMA_WRITE);
    294 	if (error)
    295 		return error;
    296 
    297 	return 0;
    298 }
    299 
    300 static void
    301 sdhost_attach_i(device_t self)
    302 {
    303 	struct sdhost_softc *sc = device_private(self);
    304 	struct sdmmcbus_attach_args saa;
    305 
    306 	sdhost_host_reset(sc);
    307 	sdhost_bus_width(sc, 1);
    308 	sdhost_bus_clock(sc, 400, false);
    309 
    310 	memset(&saa, 0, sizeof(saa));
    311 	saa.saa_busname = "sdmmc";
    312 	saa.saa_sct = &sdhost_chip_functions;
    313 	saa.saa_sch = sc;
    314 	saa.saa_dmat = sc->sc_dmat;
    315 	saa.saa_clkmin = 400;
    316 	saa.saa_clkmax = 50000;
    317 	saa.saa_caps = SMC_CAPS_DMA |
    318 		       SMC_CAPS_MULTI_SEG_DMA |
    319 		       SMC_CAPS_SD_HIGHSPEED |
    320 		       SMC_CAPS_MMC_HIGHSPEED |
    321 		       SMC_CAPS_4BIT_MODE;
    322 
    323 	sc->sc_sdmmc_dev = config_found(self, &saa, NULL);
    324 }
    325 
    326 static int
    327 sdhost_intr(void *priv)
    328 {
    329 	struct sdhost_softc * const sc = priv;
    330 
    331 	mutex_enter(&sc->sc_intr_lock);
    332 	const uint32_t hsts = SDHOST_READ(sc, SDHSTS);
    333 	if (!hsts) {
    334 		mutex_exit(&sc->sc_intr_lock);
    335 		return 0;
    336 	}
    337 	SDHOST_WRITE(sc, SDHSTS, hsts);
    338 
    339 #ifdef SDHOST_DEBUG
    340 	device_printf(sc->sc_dev, "mmc intr hsts %#x\n", hsts);
    341 #endif
    342 
    343 	if (hsts) {
    344 		sc->sc_intr_hsts |= hsts;
    345 		cv_broadcast(&sc->sc_intr_cv);
    346 	}
    347 
    348 	mutex_exit(&sc->sc_intr_lock);
    349 
    350 	return 1;
    351 }
    352 
    353 static int
    354 sdhost_dma_transfer(struct sdhost_softc *sc, struct sdmmc_command *cmd)
    355 {
    356 	size_t seg;
    357 	int error;
    358 
    359 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    360 
    361 	for (seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
    362 		sc->sc_cblk[seg].cb_ti =
    363 		    __SHIFTIN(13, DMAC_TI_PERMAP); /* SD HOST */
    364 		sc->sc_cblk[seg].cb_txfr_len =
    365 		    cmd->c_dmamap->dm_segs[seg].ds_len;
    366 		const bus_addr_t ad_sddata = sc->sc_addr + SDDATA;
    367 
    368 		/*
    369 		 * All transfers are assumed to be multiples of 32-bits.
    370 		 */
    371 		KASSERTMSG((sc->sc_cblk[seg].cb_txfr_len & 0x3) == 0,
    372 		    "seg %zu len %d", seg, sc->sc_cblk[seg].cb_txfr_len);
    373 		if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
    374 			sc->sc_cblk[seg].cb_ti |= DMAC_TI_DEST_INC;
    375 			/*
    376 			 * Use 128-bit mode if transfer is a multiple of
    377 			 * 16-bytes.
    378 			 */
    379 			if ((sc->sc_cblk[seg].cb_txfr_len & 0xf) == 0)
    380 				sc->sc_cblk[seg].cb_ti |= DMAC_TI_DEST_WIDTH;
    381 			sc->sc_cblk[seg].cb_ti |= DMAC_TI_SRC_DREQ;
    382 			sc->sc_cblk[seg].cb_source_ad = ad_sddata;
    383 			sc->sc_cblk[seg].cb_dest_ad =
    384 			    cmd->c_dmamap->dm_segs[seg].ds_addr;
    385 		} else {
    386 			sc->sc_cblk[seg].cb_ti |= DMAC_TI_SRC_INC;
    387 			/*
    388 			 * Use 128-bit mode if transfer is a multiple of
    389 			 * 16-bytes.
    390 			 */
    391 			if ((sc->sc_cblk[seg].cb_txfr_len & 0xf) == 0)
    392 				sc->sc_cblk[seg].cb_ti |= DMAC_TI_SRC_WIDTH;
    393 			sc->sc_cblk[seg].cb_ti |= DMAC_TI_DEST_DREQ;
    394 			sc->sc_cblk[seg].cb_ti |= DMAC_TI_WAIT_RESP;
    395 			sc->sc_cblk[seg].cb_source_ad =
    396 			    cmd->c_dmamap->dm_segs[seg].ds_addr;
    397 			sc->sc_cblk[seg].cb_dest_ad = ad_sddata;
    398 		}
    399 		sc->sc_cblk[seg].cb_stride = 0;
    400 		if (seg == cmd->c_dmamap->dm_nsegs - 1) {
    401 			sc->sc_cblk[seg].cb_ti |= DMAC_TI_INTEN;
    402 			sc->sc_cblk[seg].cb_nextconbk = 0;
    403 		} else {
    404 			sc->sc_cblk[seg].cb_nextconbk =
    405 			    sc->sc_dmamap->dm_segs[0].ds_addr +
    406 			    sizeof(struct bcm_dmac_conblk) * (seg+1);
    407 		}
    408 		sc->sc_cblk[seg].cb_padding[0] = 0;
    409 		sc->sc_cblk[seg].cb_padding[1] = 0;
    410 	}
    411 
    412 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
    413 	    sc->sc_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
    414 
    415 	error = 0;
    416 
    417 	sc->sc_dma_status = 0;
    418 	sc->sc_dma_error = 0;
    419 
    420 	bcm_dmac_set_conblk_addr(sc->sc_dmac,
    421 	    sc->sc_dmamap->dm_segs[0].ds_addr);
    422 	error = bcm_dmac_transfer(sc->sc_dmac);
    423 	if (error)
    424 		return error;
    425 
    426 	return 0;
    427 }
    428 
    429 static int
    430 sdhost_dma_wait(struct sdhost_softc *sc, struct sdmmc_command *cmd)
    431 {
    432 	int error = 0;
    433 
    434 	while (sc->sc_dma_status == 0 && sc->sc_dma_error == 0) {
    435 		error = cv_timedwait(&sc->sc_dma_cv, &sc->sc_intr_lock, hz*5);
    436 		if (error == EWOULDBLOCK) {
    437 			device_printf(sc->sc_dev, "transfer timeout!\n");
    438 			bcm_dmac_halt(sc->sc_dmac);
    439 			error = ETIMEDOUT;
    440 			break;
    441 		}
    442 	}
    443 
    444 	if (sc->sc_dma_status & DMAC_CS_END) {
    445 		cmd->c_resid = 0;
    446 		error = 0;
    447 	} else {
    448 		error = EIO;
    449 	}
    450 
    451 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
    452 	    sc->sc_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    453 
    454 	return error;
    455 }
    456 
    457 static void
    458 sdhost_dma_done(uint32_t status, uint32_t error, void *arg)
    459 {
    460 	struct sdhost_softc * const sc = arg;
    461 
    462 	if (status != (DMAC_CS_INT|DMAC_CS_END))
    463 		device_printf(sc->sc_dev, "dma status %#x error %#x\n",
    464 		    status, error);
    465 
    466 	mutex_enter(&sc->sc_intr_lock);
    467 	sc->sc_dma_status = status;
    468 	sc->sc_dma_error = error;
    469 	cv_broadcast(&sc->sc_dma_cv);
    470 	mutex_exit(&sc->sc_intr_lock);
    471 }
    472 
    473 static int
    474 sdhost_wait_idle(struct sdhost_softc *sc, int timeout)
    475 {
    476 	int retry;
    477 
    478 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    479 
    480 	retry = timeout * 1000;
    481 
    482 	while (--retry > 0) {
    483 		const uint32_t cmd = SDHOST_READ(sc, SDCMD);
    484 		if ((cmd & SDCMD_NEW) == 0)
    485 			return 0;
    486 		delay(1);
    487 	}
    488 
    489 	return ETIMEDOUT;
    490 }
    491 
    492 static int
    493 sdhost_host_reset(sdmmc_chipset_handle_t sch)
    494 {
    495 	struct sdhost_softc * const sc = sch;
    496 	uint32_t edm;
    497 
    498 	SDHOST_WRITE(sc, SDVDD, 0);
    499 	SDHOST_WRITE(sc, SDCMD, 0);
    500 	SDHOST_WRITE(sc, SDARG, 0);
    501 	SDHOST_WRITE(sc, SDTOUT, SDTOUT_DEFAULT);
    502 	SDHOST_WRITE(sc, SDCDIV, 0);
    503 	SDHOST_WRITE(sc, SDHSTS, SDHOST_READ(sc, SDHSTS));
    504 	SDHOST_WRITE(sc, SDHCFG, 0);
    505 	SDHOST_WRITE(sc, SDHBCT, 0);
    506 	SDHOST_WRITE(sc, SDHBLC, 0);
    507 
    508 	edm = SDHOST_READ(sc, SDEDM);
    509 	edm &= ~(SDEDM_RD_FIFO|SDEDM_WR_FIFO);
    510 	edm |= __SHIFTIN(4, SDEDM_RD_FIFO);
    511 	edm |= __SHIFTIN(4, SDEDM_WR_FIFO);
    512 	SDHOST_WRITE(sc, SDEDM, edm);
    513 	delay(20000);
    514 	SDHOST_WRITE(sc, SDVDD, SDVDD_POWER);
    515 	delay(20000);
    516 
    517 	SDHOST_WRITE(sc, SDHCFG, 0);
    518 	SDHOST_WRITE(sc, SDCDIV, SDCDIV_MASK);
    519 
    520 	return 0;
    521 }
    522 
    523 static uint32_t
    524 sdhost_host_ocr(sdmmc_chipset_handle_t sch)
    525 {
    526 	return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V | MMC_OCR_HCS;
    527 }
    528 
    529 static int
    530 sdhost_host_maxblklen(sdmmc_chipset_handle_t sch)
    531 {
    532 	return 8192;
    533 }
    534 
    535 static int
    536 sdhost_card_detect(sdmmc_chipset_handle_t sch)
    537 {
    538 	return 1;	/* XXX */
    539 }
    540 
    541 static int
    542 sdhost_write_protect(sdmmc_chipset_handle_t sch)
    543 {
    544 	return 0;	/* no write protect pin, assume rw */
    545 }
    546 
    547 static int
    548 sdhost_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    549 {
    550 	return 0;
    551 }
    552 
    553 static int
    554 sdhost_bus_clock(sdmmc_chipset_handle_t sch, int freq, bool ddr)
    555 {
    556 	struct sdhost_softc * const sc = sch;
    557 	u_int target_rate = freq * 1000;
    558 	int div;
    559 
    560 	if (freq == 0)
    561 		div = SDCDIV_MASK;
    562 	else {
    563 		div = sc->sc_rate / target_rate;
    564 		if (div < 2)
    565 			div = 2;
    566 		if ((sc->sc_rate / div) > target_rate)
    567 			div++;
    568 		div -= 2;
    569 		if (div > SDCDIV_MASK)
    570 			div = SDCDIV_MASK;
    571 	}
    572 
    573 	SDHOST_WRITE(sc, SDCDIV, div);
    574 
    575 	return 0;
    576 }
    577 
    578 static int
    579 sdhost_bus_width(sdmmc_chipset_handle_t sch, int width)
    580 {
    581 	struct sdhost_softc * const sc = sch;
    582 	uint32_t hcfg;
    583 
    584 #ifdef SDHOST_DEBUG
    585 	aprint_normal_dev(sc->sc_dev, "width = %d\n", width);
    586 #endif
    587 
    588 	hcfg = SDHOST_READ(sc, SDHCFG);
    589 	if (width == 4)
    590 		hcfg |= SDHCFG_WIDE_EXT;
    591 	else
    592 		hcfg &= ~SDHCFG_WIDE_EXT;
    593 	hcfg |= (SDHCFG_WIDE_INT | SDHCFG_SLOW);
    594 	SDHOST_WRITE(sc, SDHCFG, hcfg);
    595 
    596 	return 0;
    597 }
    598 
    599 static int
    600 sdhost_bus_rod(sdmmc_chipset_handle_t sch, int on)
    601 {
    602 	return -1;
    603 }
    604 
    605 static void
    606 sdhost_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
    607 {
    608 	struct sdhost_softc * const sc = sch;
    609 	uint32_t cmdval, hcfg;
    610 	u_int nblks;
    611 
    612 #ifdef SDHOST_DEBUG
    613 	aprint_normal_dev(sc->sc_dev,
    614 	    "opcode %d flags 0x%x data %p datalen %d blklen %d\n",
    615 	    cmd->c_opcode, cmd->c_flags, cmd->c_data, cmd->c_datalen,
    616 	    cmd->c_blklen);
    617 #endif
    618 
    619 	mutex_enter(&sc->sc_intr_lock);
    620 
    621 	hcfg = SDHOST_READ(sc, SDHCFG);
    622 	SDHOST_WRITE(sc, SDHCFG, hcfg | SDHCFG_BUSY_EN);
    623 
    624 	sc->sc_intr_hsts = 0;
    625 
    626 	cmd->c_error = sdhost_wait_idle(sc, 5000);
    627 	if (cmd->c_error != 0) {
    628 #ifdef SDHOST_DEBUG
    629 		device_printf(sc->sc_dev, "device is busy\n");
    630 #endif
    631 		goto done;
    632 	}
    633 
    634 	cmdval = SDCMD_NEW;
    635 	if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
    636 		cmdval |= SDCMD_NORESP;
    637 	if (ISSET(cmd->c_flags, SCF_RSP_136))
    638 		cmdval |= SDCMD_LONGRESP;
    639 	if (ISSET(cmd->c_flags, SCF_RSP_BSY))
    640 		cmdval |= SDCMD_BUSY;
    641 
    642 	if (cmd->c_datalen > 0) {
    643 		if (ISSET(cmd->c_flags, SCF_CMD_READ))
    644 			cmdval |= SDCMD_READ;
    645 		else
    646 			cmdval |= SDCMD_WRITE;
    647 
    648 		nblks = cmd->c_datalen / cmd->c_blklen;
    649 		if (nblks == 0 || (cmd->c_datalen % cmd->c_blklen) != 0)
    650 			++nblks;
    651 
    652 		SDHOST_WRITE(sc, SDHBCT, cmd->c_blklen);
    653 		SDHOST_WRITE(sc, SDHBLC, nblks);
    654 
    655 		cmd->c_resid = cmd->c_datalen;
    656 		cmd->c_error = sdhost_dma_transfer(sc, cmd);
    657 		if (cmd->c_error != 0) {
    658 #ifdef SDHOST_DEBUG
    659 			device_printf(sc->sc_dev, "dma transfer failed: %d\n",
    660 			    cmd->c_error);
    661 #endif
    662 			goto done;
    663 		}
    664 	}
    665 
    666 	SDHOST_WRITE(sc, SDARG, cmd->c_arg);
    667 	SDHOST_WRITE(sc, SDCMD, cmdval | cmd->c_opcode);
    668 
    669 	if (cmd->c_datalen > 0) {
    670 		cmd->c_error = sdhost_dma_wait(sc, cmd);
    671 		if (cmd->c_error != 0) {
    672 #ifdef SDHOST_DEBUG
    673 			device_printf(sc->sc_dev,
    674 			    "wait dma failed: %d\n", cmd->c_error);
    675 #endif
    676 			goto done;
    677 		}
    678 	}
    679 
    680 	cmd->c_error = sdhost_wait_idle(sc, 5000);
    681 	if (cmd->c_error != 0) {
    682 #ifdef SDHOST_DEBUG
    683 		device_printf(sc->sc_dev,
    684 		    "wait cmd idle (%#x) failed: %d\n",
    685 		    SDHOST_READ(sc, SDCMD), cmd->c_error);
    686 #endif
    687 	}
    688 
    689 	if ((SDHOST_READ(sc, SDCMD) & SDCMD_FAIL) != 0) {
    690 #ifdef SDHOST_DEBUG
    691 		device_printf(sc->sc_dev, "SDCMD: %#x\n",
    692 		    SDHOST_READ(sc, SDCMD));
    693 #endif
    694 		cmd->c_error = EIO;
    695 		goto done;
    696 	}
    697 
    698 	if (ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
    699 		if (ISSET(cmd->c_flags, SCF_RSP_136)) {
    700 			cmd->c_resp[0] = SDHOST_READ(sc, SDRSP0);
    701 			cmd->c_resp[1] = SDHOST_READ(sc, SDRSP1);
    702 			cmd->c_resp[2] = SDHOST_READ(sc, SDRSP2);
    703 			cmd->c_resp[3] = SDHOST_READ(sc, SDRSP3);
    704 			if (ISSET(cmd->c_flags, SCF_RSP_CRC)) {
    705 				cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
    706 				    (cmd->c_resp[1] << 24);
    707 				cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
    708 				    (cmd->c_resp[2] << 24);
    709 				cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
    710 				    (cmd->c_resp[3] << 24);
    711 				cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
    712 			}
    713 		} else {
    714 			cmd->c_resp[0] = SDHOST_READ(sc, SDRSP0);
    715 		}
    716 	}
    717 
    718 done:
    719 	cmd->c_flags |= SCF_ITSDONE;
    720 	SDHOST_WRITE(sc, SDHCFG, hcfg);
    721 	SDHOST_WRITE(sc, SDHSTS, SDHOST_READ(sc, SDHSTS));
    722 	mutex_exit(&sc->sc_intr_lock);
    723 
    724 #ifdef SDHOST_DEBUG
    725 	if (cmd->c_error != 0)
    726 		device_printf(sc->sc_dev, "command failed with error %d\n",
    727 		    cmd->c_error);
    728 #endif
    729 }
    730 
    731 static void
    732 sdhost_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
    733 {
    734 }
    735 
    736 static void
    737 sdhost_card_intr_ack(sdmmc_chipset_handle_t sch)
    738 {
    739 }
    740