Home | History | Annotate | Line # | Download | only in sdmmc
sdhc.c revision 1.44.2.8
      1  1.44.2.8   msaitoh /*	$NetBSD: sdhc.c,v 1.44.2.8 2015/04/19 04:31:40 msaitoh Exp $	*/
      2       1.1    nonaka /*	$OpenBSD: sdhc.c,v 1.25 2009/01/13 19:44:20 grange Exp $	*/
      3       1.1    nonaka 
      4       1.1    nonaka /*
      5       1.1    nonaka  * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
      6       1.1    nonaka  *
      7       1.1    nonaka  * Permission to use, copy, modify, and distribute this software for any
      8       1.1    nonaka  * purpose with or without fee is hereby granted, provided that the above
      9       1.1    nonaka  * copyright notice and this permission notice appear in all copies.
     10       1.1    nonaka  *
     11       1.1    nonaka  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12       1.1    nonaka  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13       1.1    nonaka  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14       1.1    nonaka  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15       1.1    nonaka  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16       1.1    nonaka  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17       1.1    nonaka  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18       1.1    nonaka  */
     19       1.1    nonaka 
     20       1.1    nonaka /*
     21       1.1    nonaka  * SD Host Controller driver based on the SD Host Controller Standard
     22       1.1    nonaka  * Simplified Specification Version 1.00 (www.sdcard.com).
     23       1.1    nonaka  */
     24       1.1    nonaka 
     25       1.1    nonaka #include <sys/cdefs.h>
     26  1.44.2.8   msaitoh __KERNEL_RCSID(0, "$NetBSD: sdhc.c,v 1.44.2.8 2015/04/19 04:31:40 msaitoh Exp $");
     27      1.10    nonaka 
     28      1.10    nonaka #ifdef _KERNEL_OPT
     29      1.10    nonaka #include "opt_sdmmc.h"
     30      1.10    nonaka #endif
     31       1.1    nonaka 
     32       1.1    nonaka #include <sys/param.h>
     33       1.1    nonaka #include <sys/device.h>
     34       1.1    nonaka #include <sys/kernel.h>
     35       1.1    nonaka #include <sys/kthread.h>
     36       1.1    nonaka #include <sys/malloc.h>
     37       1.1    nonaka #include <sys/systm.h>
     38       1.1    nonaka #include <sys/mutex.h>
     39       1.1    nonaka #include <sys/condvar.h>
     40       1.1    nonaka 
     41       1.1    nonaka #include <dev/sdmmc/sdhcreg.h>
     42       1.1    nonaka #include <dev/sdmmc/sdhcvar.h>
     43       1.1    nonaka #include <dev/sdmmc/sdmmcchip.h>
     44       1.1    nonaka #include <dev/sdmmc/sdmmcreg.h>
     45       1.1    nonaka #include <dev/sdmmc/sdmmcvar.h>
     46       1.1    nonaka 
     47       1.1    nonaka #ifdef SDHC_DEBUG
     48       1.1    nonaka int sdhcdebug = 1;
     49       1.1    nonaka #define DPRINTF(n,s)	do { if ((n) <= sdhcdebug) printf s; } while (0)
     50       1.1    nonaka void	sdhc_dump_regs(struct sdhc_host *);
     51       1.1    nonaka #else
     52       1.1    nonaka #define DPRINTF(n,s)	do {} while (0)
     53       1.1    nonaka #endif
     54       1.1    nonaka 
     55       1.1    nonaka #define SDHC_COMMAND_TIMEOUT	hz
     56       1.1    nonaka #define SDHC_BUFFER_TIMEOUT	hz
     57       1.1    nonaka #define SDHC_TRANSFER_TIMEOUT	hz
     58       1.1    nonaka #define SDHC_DMA_TIMEOUT	hz
     59       1.1    nonaka 
     60       1.1    nonaka struct sdhc_host {
     61       1.1    nonaka 	struct sdhc_softc *sc;		/* host controller device */
     62       1.1    nonaka 
     63       1.1    nonaka 	bus_space_tag_t iot;		/* host register set tag */
     64       1.1    nonaka 	bus_space_handle_t ioh;		/* host register set handle */
     65      1.36  jakllsch 	bus_size_t ios;			/* host register space size */
     66       1.1    nonaka 	bus_dma_tag_t dmat;		/* host DMA tag */
     67       1.1    nonaka 
     68       1.1    nonaka 	device_t sdmmc;			/* generic SD/MMC device */
     69       1.1    nonaka 
     70       1.1    nonaka 	struct kmutex host_mtx;
     71       1.1    nonaka 
     72       1.1    nonaka 	u_int clkbase;			/* base clock frequency in KHz */
     73       1.1    nonaka 	int maxblklen;			/* maximum block length */
     74       1.1    nonaka 	uint32_t ocr;			/* OCR value from capabilities */
     75       1.1    nonaka 
     76       1.1    nonaka 	uint8_t regs[14];		/* host controller state */
     77       1.1    nonaka 
     78       1.1    nonaka 	uint16_t intr_status;		/* soft interrupt status */
     79       1.1    nonaka 	uint16_t intr_error_status;	/* soft error status */
     80       1.1    nonaka 	struct kmutex intr_mtx;
     81       1.1    nonaka 	struct kcondvar intr_cv;
     82       1.1    nonaka 
     83      1.12    nonaka 	int specver;			/* spec. version */
     84      1.12    nonaka 
     85       1.1    nonaka 	uint32_t flags;			/* flags for this host */
     86       1.1    nonaka #define SHF_USE_DMA		0x0001
     87       1.1    nonaka #define SHF_USE_4BIT_MODE	0x0002
     88      1.11      matt #define SHF_USE_8BIT_MODE	0x0004
     89  1.44.2.8   msaitoh #define SHF_MODE_DMAEN		0x0008 /* needs SDHC_DMA_ENABLE in mode */
     90       1.1    nonaka };
     91       1.1    nonaka 
     92       1.1    nonaka #define HDEVNAME(hp)	(device_xname((hp)->sc->sc_dev))
     93       1.1    nonaka 
     94      1.11      matt static uint8_t
     95      1.11      matt hread1(struct sdhc_host *hp, bus_size_t reg)
     96      1.11      matt {
     97      1.12    nonaka 
     98      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
     99      1.11      matt 		return bus_space_read_1(hp->iot, hp->ioh, reg);
    100      1.11      matt 	return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 3));
    101      1.11      matt }
    102      1.11      matt 
    103      1.11      matt static uint16_t
    104      1.11      matt hread2(struct sdhc_host *hp, bus_size_t reg)
    105      1.11      matt {
    106      1.12    nonaka 
    107      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
    108      1.11      matt 		return bus_space_read_2(hp->iot, hp->ioh, reg);
    109      1.11      matt 	return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 2));
    110      1.11      matt }
    111      1.11      matt 
    112      1.11      matt #define HREAD1(hp, reg)		hread1(hp, reg)
    113      1.11      matt #define HREAD2(hp, reg)		hread2(hp, reg)
    114      1.11      matt #define HREAD4(hp, reg)		\
    115       1.1    nonaka 	(bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
    116      1.11      matt 
    117      1.11      matt 
    118      1.11      matt static void
    119      1.11      matt hwrite1(struct sdhc_host *hp, bus_size_t o, uint8_t val)
    120      1.11      matt {
    121      1.12    nonaka 
    122      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    123      1.11      matt 		bus_space_write_1(hp->iot, hp->ioh, o, val);
    124      1.11      matt 	} else {
    125      1.11      matt 		const size_t shift = 8 * (o & 3);
    126      1.11      matt 		o &= -4;
    127      1.11      matt 		uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
    128      1.11      matt 		tmp = (val << shift) | (tmp & ~(0xff << shift));
    129      1.11      matt 		bus_space_write_4(hp->iot, hp->ioh, o, tmp);
    130      1.11      matt 	}
    131      1.11      matt }
    132      1.11      matt 
    133      1.11      matt static void
    134      1.11      matt hwrite2(struct sdhc_host *hp, bus_size_t o, uint16_t val)
    135      1.11      matt {
    136      1.12    nonaka 
    137      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    138      1.11      matt 		bus_space_write_2(hp->iot, hp->ioh, o, val);
    139      1.11      matt 	} else {
    140      1.11      matt 		const size_t shift = 8 * (o & 2);
    141      1.11      matt 		o &= -4;
    142      1.11      matt 		uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
    143      1.11      matt 		tmp = (val << shift) | (tmp & ~(0xffff << shift));
    144      1.11      matt 		bus_space_write_4(hp->iot, hp->ioh, o, tmp);
    145      1.11      matt 	}
    146      1.11      matt }
    147      1.11      matt 
    148      1.11      matt #define HWRITE1(hp, reg, val)		hwrite1(hp, reg, val)
    149      1.11      matt #define HWRITE2(hp, reg, val)		hwrite2(hp, reg, val)
    150       1.1    nonaka #define HWRITE4(hp, reg, val)						\
    151       1.1    nonaka 	bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val))
    152      1.11      matt 
    153       1.1    nonaka #define HCLR1(hp, reg, bits)						\
    154      1.11      matt 	do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits)); while (0)
    155       1.1    nonaka #define HCLR2(hp, reg, bits)						\
    156      1.11      matt 	do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits)); while (0)
    157      1.11      matt #define HCLR4(hp, reg, bits)						\
    158      1.11      matt 	do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) & ~(bits)); while (0)
    159       1.1    nonaka #define HSET1(hp, reg, bits)						\
    160      1.11      matt 	do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits)); while (0)
    161       1.1    nonaka #define HSET2(hp, reg, bits)						\
    162      1.11      matt 	do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits)); while (0)
    163      1.11      matt #define HSET4(hp, reg, bits)						\
    164      1.11      matt 	do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) | (bits)); while (0)
    165       1.1    nonaka 
    166       1.1    nonaka static int	sdhc_host_reset(sdmmc_chipset_handle_t);
    167       1.1    nonaka static int	sdhc_host_reset1(sdmmc_chipset_handle_t);
    168       1.1    nonaka static uint32_t	sdhc_host_ocr(sdmmc_chipset_handle_t);
    169       1.1    nonaka static int	sdhc_host_maxblklen(sdmmc_chipset_handle_t);
    170       1.1    nonaka static int	sdhc_card_detect(sdmmc_chipset_handle_t);
    171       1.1    nonaka static int	sdhc_write_protect(sdmmc_chipset_handle_t);
    172       1.1    nonaka static int	sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t);
    173       1.1    nonaka static int	sdhc_bus_clock(sdmmc_chipset_handle_t, int);
    174       1.1    nonaka static int	sdhc_bus_width(sdmmc_chipset_handle_t, int);
    175       1.8  kiyohara static int	sdhc_bus_rod(sdmmc_chipset_handle_t, int);
    176       1.1    nonaka static void	sdhc_card_enable_intr(sdmmc_chipset_handle_t, int);
    177       1.1    nonaka static void	sdhc_card_intr_ack(sdmmc_chipset_handle_t);
    178       1.1    nonaka static void	sdhc_exec_command(sdmmc_chipset_handle_t,
    179       1.1    nonaka 		    struct sdmmc_command *);
    180       1.1    nonaka static int	sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
    181       1.1    nonaka static int	sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t);
    182       1.1    nonaka static int	sdhc_soft_reset(struct sdhc_host *, int);
    183       1.1    nonaka static int	sdhc_wait_intr(struct sdhc_host *, int, int);
    184       1.1    nonaka static void	sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
    185       1.7    nonaka static int	sdhc_transfer_data_dma(struct sdhc_host *, struct sdmmc_command *);
    186       1.1    nonaka static int	sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *);
    187      1.11      matt static void	sdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
    188      1.11      matt static void	sdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
    189      1.11      matt static void	esdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
    190      1.11      matt static void	esdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
    191      1.11      matt 
    192       1.1    nonaka 
    193       1.1    nonaka static struct sdmmc_chip_functions sdhc_functions = {
    194       1.1    nonaka 	/* host controller reset */
    195       1.1    nonaka 	sdhc_host_reset,
    196       1.1    nonaka 
    197       1.1    nonaka 	/* host controller capabilities */
    198       1.1    nonaka 	sdhc_host_ocr,
    199       1.1    nonaka 	sdhc_host_maxblklen,
    200       1.1    nonaka 
    201       1.1    nonaka 	/* card detection */
    202       1.1    nonaka 	sdhc_card_detect,
    203       1.1    nonaka 
    204       1.1    nonaka 	/* write protect */
    205       1.1    nonaka 	sdhc_write_protect,
    206       1.1    nonaka 
    207       1.1    nonaka 	/* bus power, clock frequency and width */
    208       1.1    nonaka 	sdhc_bus_power,
    209       1.1    nonaka 	sdhc_bus_clock,
    210       1.1    nonaka 	sdhc_bus_width,
    211       1.8  kiyohara 	sdhc_bus_rod,
    212       1.1    nonaka 
    213       1.1    nonaka 	/* command execution */
    214       1.1    nonaka 	sdhc_exec_command,
    215       1.1    nonaka 
    216       1.1    nonaka 	/* card interrupt */
    217       1.1    nonaka 	sdhc_card_enable_intr,
    218       1.1    nonaka 	sdhc_card_intr_ack
    219       1.1    nonaka };
    220       1.1    nonaka 
    221      1.17  jakllsch static int
    222      1.17  jakllsch sdhc_cfprint(void *aux, const char *pnp)
    223      1.17  jakllsch {
    224      1.31     joerg 	const struct sdmmcbus_attach_args * const saa = aux;
    225      1.17  jakllsch 	const struct sdhc_host * const hp = saa->saa_sch;
    226  1.44.2.3    martin 
    227      1.17  jakllsch 	if (pnp) {
    228      1.17  jakllsch 		aprint_normal("sdmmc at %s", pnp);
    229      1.17  jakllsch 	}
    230      1.41  jakllsch 	for (size_t host = 0; host < hp->sc->sc_nhosts; host++) {
    231      1.41  jakllsch 		if (hp->sc->sc_host[host] == hp) {
    232      1.41  jakllsch 			aprint_normal(" slot %zu", host);
    233      1.41  jakllsch 		}
    234      1.41  jakllsch 	}
    235      1.17  jakllsch 
    236      1.17  jakllsch 	return UNCONF;
    237      1.17  jakllsch }
    238      1.17  jakllsch 
    239       1.1    nonaka /*
    240       1.1    nonaka  * Called by attachment driver.  For each SD card slot there is one SD
    241       1.1    nonaka  * host controller standard register set. (1.3)
    242       1.1    nonaka  */
    243       1.1    nonaka int
    244       1.1    nonaka sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
    245       1.1    nonaka     bus_space_handle_t ioh, bus_size_t iosize)
    246       1.1    nonaka {
    247       1.1    nonaka 	struct sdmmcbus_attach_args saa;
    248       1.1    nonaka 	struct sdhc_host *hp;
    249       1.1    nonaka 	uint32_t caps;
    250       1.1    nonaka 	uint16_t sdhcver;
    251       1.1    nonaka 
    252      1.33  riastrad 	/* Allocate one more host structure. */
    253      1.33  riastrad 	hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO);
    254      1.33  riastrad 	if (hp == NULL) {
    255      1.33  riastrad 		aprint_error_dev(sc->sc_dev,
    256      1.33  riastrad 		    "couldn't alloc memory (sdhc host)\n");
    257      1.33  riastrad 		goto err1;
    258      1.33  riastrad 	}
    259      1.33  riastrad 	sc->sc_host[sc->sc_nhosts++] = hp;
    260      1.33  riastrad 
    261      1.33  riastrad 	/* Fill in the new host structure. */
    262      1.33  riastrad 	hp->sc = sc;
    263      1.33  riastrad 	hp->iot = iot;
    264      1.33  riastrad 	hp->ioh = ioh;
    265      1.36  jakllsch 	hp->ios = iosize;
    266      1.33  riastrad 	hp->dmat = sc->sc_dmat;
    267      1.33  riastrad 
    268      1.33  riastrad 	mutex_init(&hp->host_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    269      1.33  riastrad 	mutex_init(&hp->intr_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    270      1.33  riastrad 	cv_init(&hp->intr_cv, "sdhcintr");
    271      1.33  riastrad 
    272  1.44.2.6    martin 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    273  1.44.2.6    martin 		sdhcver = HREAD4(hp, SDHC_ESDHC_HOST_CTL_VERSION);
    274  1.44.2.6    martin 	} else {
    275  1.44.2.6    martin 		sdhcver = HREAD2(hp, SDHC_HOST_CTL_VERSION);
    276  1.44.2.6    martin 	}
    277      1.12    nonaka 	aprint_normal_dev(sc->sc_dev, "SD Host Specification ");
    278      1.33  riastrad 	hp->specver = SDHC_SPEC_VERSION(sdhcver);
    279       1.1    nonaka 	switch (SDHC_SPEC_VERSION(sdhcver)) {
    280      1.12    nonaka 	case SDHC_SPEC_VERS_100:
    281      1.12    nonaka 		aprint_normal("1.0");
    282      1.12    nonaka 		break;
    283      1.12    nonaka 
    284      1.12    nonaka 	case SDHC_SPEC_VERS_200:
    285      1.12    nonaka 		aprint_normal("2.0");
    286       1.1    nonaka 		break;
    287       1.1    nonaka 
    288      1.12    nonaka 	case SDHC_SPEC_VERS_300:
    289      1.12    nonaka 		aprint_normal("3.0");
    290       1.9      matt 		break;
    291       1.9      matt 
    292       1.1    nonaka 	default:
    293      1.12    nonaka 		aprint_normal("unknown version(0x%x)",
    294      1.12    nonaka 		    SDHC_SPEC_VERSION(sdhcver));
    295       1.1    nonaka 		break;
    296       1.1    nonaka 	}
    297      1.12    nonaka 	aprint_normal(", rev.%u\n", SDHC_VENDOR_VERSION(sdhcver));
    298       1.1    nonaka 
    299       1.1    nonaka 	/*
    300       1.3  uebayasi 	 * Reset the host controller and enable interrupts.
    301       1.1    nonaka 	 */
    302       1.1    nonaka 	(void)sdhc_host_reset(hp);
    303       1.1    nonaka 
    304       1.1    nonaka 	/* Determine host capabilities. */
    305      1.24     skrll 	if (ISSET(sc->sc_flags, SDHC_FLAG_HOSTCAPS)) {
    306      1.24     skrll 		caps = sc->sc_caps;
    307      1.24     skrll 	} else {
    308      1.24     skrll 		mutex_enter(&hp->host_mtx);
    309      1.24     skrll 		caps = HREAD4(hp, SDHC_CAPABILITIES);
    310      1.24     skrll 		mutex_exit(&hp->host_mtx);
    311      1.24     skrll 	}
    312       1.1    nonaka 
    313  1.44.2.8   msaitoh 	/*
    314  1.44.2.8   msaitoh 	 * Use DMA if the host system and the controller support it.
    315  1.44.2.8   msaitoh 	 * Suports integrated or external DMA egine, with or without
    316  1.44.2.8   msaitoh 	 * SDHC_DMA_ENABLE in the command.
    317  1.44.2.8   msaitoh 	 */
    318      1.28      matt 	if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA) ||
    319      1.27  jakllsch 	    (ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA &&
    320      1.28      matt 	     ISSET(caps, SDHC_DMA_SUPPORT)))) {
    321       1.1    nonaka 		SET(hp->flags, SHF_USE_DMA);
    322  1.44.2.8   msaitoh 		if (!ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA) ||
    323  1.44.2.8   msaitoh 		    ISSET(sc->sc_flags, SDHC_FLAG_EXTDMA_DMAEN))
    324  1.44.2.8   msaitoh 			SET(hp->flags, SHF_MODE_DMAEN);
    325  1.44.2.8   msaitoh 
    326       1.1    nonaka 		aprint_normal_dev(sc->sc_dev, "using DMA transfer\n");
    327       1.1    nonaka 	}
    328       1.1    nonaka 
    329       1.1    nonaka 	/*
    330       1.1    nonaka 	 * Determine the base clock frequency. (2.2.24)
    331       1.1    nonaka 	 */
    332      1.30      matt 	if (hp->specver == SDHC_SPEC_VERS_300) {
    333      1.30      matt 		hp->clkbase = SDHC_BASE_V3_FREQ_KHZ(caps);
    334      1.30      matt 	} else {
    335      1.30      matt 		hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
    336      1.30      matt 	}
    337       1.1    nonaka 	if (hp->clkbase == 0) {
    338       1.9      matt 		if (sc->sc_clkbase == 0) {
    339       1.9      matt 			/* The attachment driver must tell us. */
    340      1.12    nonaka 			aprint_error_dev(sc->sc_dev,
    341      1.12    nonaka 			    "unknown base clock frequency\n");
    342       1.9      matt 			goto err;
    343       1.9      matt 		}
    344       1.9      matt 		hp->clkbase = sc->sc_clkbase;
    345       1.9      matt 	}
    346       1.9      matt 	if (hp->clkbase < 10000 || hp->clkbase > 10000 * 256) {
    347       1.1    nonaka 		/* SDHC 1.0 supports only 10-63 MHz. */
    348       1.1    nonaka 		aprint_error_dev(sc->sc_dev,
    349       1.1    nonaka 		    "base clock frequency out of range: %u MHz\n",
    350       1.1    nonaka 		    hp->clkbase / 1000);
    351       1.1    nonaka 		goto err;
    352       1.1    nonaka 	}
    353       1.1    nonaka 	DPRINTF(1,("%s: base clock frequency %u MHz\n",
    354       1.1    nonaka 	    device_xname(sc->sc_dev), hp->clkbase / 1000));
    355       1.1    nonaka 
    356       1.1    nonaka 	/*
    357       1.1    nonaka 	 * XXX Set the data timeout counter value according to
    358       1.1    nonaka 	 * capabilities. (2.2.15)
    359       1.1    nonaka 	 */
    360       1.1    nonaka 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    361      1.29      matt #if 1
    362      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    363      1.11      matt 		HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
    364      1.11      matt #endif
    365       1.1    nonaka 
    366       1.1    nonaka 	/*
    367       1.1    nonaka 	 * Determine SD bus voltage levels supported by the controller.
    368       1.1    nonaka 	 */
    369  1.44.2.1    martin 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V) &&
    370  1.44.2.1    martin 	    (hp->specver < SDHC_SPEC_VERS_300 ||
    371  1.44.2.1    martin 	     ISSET(caps, SDHC_EMBEDDED_SLOT))) {
    372       1.1    nonaka 		SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V);
    373      1.11      matt 	}
    374      1.11      matt 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V)) {
    375       1.1    nonaka 		SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
    376      1.11      matt 	}
    377      1.11      matt 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V)) {
    378       1.1    nonaka 		SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
    379      1.11      matt 	}
    380       1.1    nonaka 
    381       1.1    nonaka 	/*
    382       1.1    nonaka 	 * Determine the maximum block length supported by the host
    383       1.1    nonaka 	 * controller. (2.2.24)
    384       1.1    nonaka 	 */
    385       1.1    nonaka 	switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
    386       1.1    nonaka 	case SDHC_MAX_BLK_LEN_512:
    387       1.1    nonaka 		hp->maxblklen = 512;
    388       1.1    nonaka 		break;
    389       1.1    nonaka 
    390       1.1    nonaka 	case SDHC_MAX_BLK_LEN_1024:
    391       1.1    nonaka 		hp->maxblklen = 1024;
    392       1.1    nonaka 		break;
    393       1.1    nonaka 
    394       1.1    nonaka 	case SDHC_MAX_BLK_LEN_2048:
    395       1.1    nonaka 		hp->maxblklen = 2048;
    396       1.1    nonaka 		break;
    397       1.1    nonaka 
    398       1.9      matt 	case SDHC_MAX_BLK_LEN_4096:
    399       1.9      matt 		hp->maxblklen = 4096;
    400       1.9      matt 		break;
    401       1.9      matt 
    402       1.1    nonaka 	default:
    403       1.1    nonaka 		aprint_error_dev(sc->sc_dev, "max block length unknown\n");
    404       1.1    nonaka 		goto err;
    405       1.1    nonaka 	}
    406       1.1    nonaka 	DPRINTF(1, ("%s: max block length %u byte%s\n",
    407       1.1    nonaka 	    device_xname(sc->sc_dev), hp->maxblklen,
    408       1.1    nonaka 	    hp->maxblklen > 1 ? "s" : ""));
    409       1.1    nonaka 
    410       1.1    nonaka 	/*
    411       1.1    nonaka 	 * Attach the generic SD/MMC bus driver.  (The bus driver must
    412       1.1    nonaka 	 * not invoke any chipset functions before it is attached.)
    413       1.1    nonaka 	 */
    414       1.1    nonaka 	memset(&saa, 0, sizeof(saa));
    415       1.1    nonaka 	saa.saa_busname = "sdmmc";
    416       1.1    nonaka 	saa.saa_sct = &sdhc_functions;
    417       1.1    nonaka 	saa.saa_sch = hp;
    418       1.1    nonaka 	saa.saa_dmat = hp->dmat;
    419       1.1    nonaka 	saa.saa_clkmax = hp->clkbase;
    420      1.11      matt 	if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_CGM))
    421      1.38  jakllsch 		saa.saa_clkmin = hp->clkbase / 256 / 2046;
    422      1.11      matt 	else if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_DVS))
    423      1.38  jakllsch 		saa.saa_clkmin = hp->clkbase / 256 / 16;
    424      1.38  jakllsch 	else if (hp->sc->sc_clkmsk != 0)
    425      1.38  jakllsch 		saa.saa_clkmin = hp->clkbase / (hp->sc->sc_clkmsk >>
    426      1.38  jakllsch 		    (ffs(hp->sc->sc_clkmsk) - 1));
    427      1.38  jakllsch 	else if (hp->specver == SDHC_SPEC_VERS_300)
    428      1.38  jakllsch 		saa.saa_clkmin = hp->clkbase / 0x3ff;
    429      1.38  jakllsch 	else
    430      1.38  jakllsch 		saa.saa_clkmin = hp->clkbase / 256;
    431       1.1    nonaka 	saa.saa_caps = SMC_CAPS_4BIT_MODE|SMC_CAPS_AUTO_STOP;
    432      1.11      matt 	if (ISSET(sc->sc_flags, SDHC_FLAG_8BIT_MODE))
    433      1.11      matt 		saa.saa_caps |= SMC_CAPS_8BIT_MODE;
    434      1.11      matt 	if (ISSET(caps, SDHC_HIGH_SPEED_SUPP))
    435      1.11      matt 		saa.saa_caps |= SMC_CAPS_SD_HIGHSPEED;
    436      1.26      matt 	if (ISSET(hp->flags, SHF_USE_DMA)) {
    437  1.44.2.7       snj 		saa.saa_caps |= SMC_CAPS_DMA;
    438  1.44.2.7       snj 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    439  1.44.2.7       snj 			saa.saa_caps |= SMC_CAPS_MULTI_SEG_DMA;
    440      1.26      matt 	}
    441      1.32  kiyohara 	if (ISSET(sc->sc_flags, SDHC_FLAG_SINGLE_ONLY))
    442      1.32  kiyohara 		saa.saa_caps |= SMC_CAPS_SINGLE_ONLY;
    443      1.17  jakllsch 	hp->sdmmc = config_found(sc->sc_dev, &saa, sdhc_cfprint);
    444       1.1    nonaka 
    445       1.1    nonaka 	return 0;
    446       1.1    nonaka 
    447       1.1    nonaka err:
    448       1.1    nonaka 	cv_destroy(&hp->intr_cv);
    449       1.1    nonaka 	mutex_destroy(&hp->intr_mtx);
    450       1.1    nonaka 	mutex_destroy(&hp->host_mtx);
    451       1.1    nonaka 	free(hp, M_DEVBUF);
    452       1.1    nonaka 	sc->sc_host[--sc->sc_nhosts] = NULL;
    453       1.1    nonaka err1:
    454       1.1    nonaka 	return 1;
    455       1.1    nonaka }
    456       1.1    nonaka 
    457       1.7    nonaka int
    458      1.36  jakllsch sdhc_detach(struct sdhc_softc *sc, int flags)
    459       1.7    nonaka {
    460      1.36  jakllsch 	struct sdhc_host *hp;
    461       1.7    nonaka 	int rv = 0;
    462       1.7    nonaka 
    463      1.36  jakllsch 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    464      1.36  jakllsch 		hp = sc->sc_host[n];
    465      1.36  jakllsch 		if (hp == NULL)
    466      1.36  jakllsch 			continue;
    467      1.36  jakllsch 		if (hp->sdmmc != NULL) {
    468      1.36  jakllsch 			rv = config_detach(hp->sdmmc, flags);
    469      1.36  jakllsch 			if (rv)
    470      1.36  jakllsch 				break;
    471      1.36  jakllsch 			hp->sdmmc = NULL;
    472      1.36  jakllsch 		}
    473      1.36  jakllsch 		/* disable interrupts */
    474      1.36  jakllsch 		if ((flags & DETACH_FORCE) == 0) {
    475      1.36  jakllsch 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    476      1.36  jakllsch 				HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
    477      1.36  jakllsch 			} else {
    478      1.36  jakllsch 				HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
    479      1.36  jakllsch 			}
    480      1.36  jakllsch 			sdhc_soft_reset(hp, SDHC_RESET_ALL);
    481      1.36  jakllsch 		}
    482      1.36  jakllsch 		cv_destroy(&hp->intr_cv);
    483      1.36  jakllsch 		mutex_destroy(&hp->intr_mtx);
    484      1.36  jakllsch 		mutex_destroy(&hp->host_mtx);
    485      1.36  jakllsch 		if (hp->ios > 0) {
    486      1.36  jakllsch 			bus_space_unmap(hp->iot, hp->ioh, hp->ios);
    487      1.36  jakllsch 			hp->ios = 0;
    488      1.36  jakllsch 		}
    489      1.36  jakllsch 		free(hp, M_DEVBUF);
    490      1.36  jakllsch 		sc->sc_host[n] = NULL;
    491      1.36  jakllsch 	}
    492       1.7    nonaka 
    493       1.7    nonaka 	return rv;
    494       1.7    nonaka }
    495       1.7    nonaka 
    496       1.1    nonaka bool
    497       1.6    dyoung sdhc_suspend(device_t dev, const pmf_qual_t *qual)
    498       1.1    nonaka {
    499       1.1    nonaka 	struct sdhc_softc *sc = device_private(dev);
    500       1.1    nonaka 	struct sdhc_host *hp;
    501      1.12    nonaka 	size_t i;
    502       1.1    nonaka 
    503       1.1    nonaka 	/* XXX poll for command completion or suspend command
    504       1.1    nonaka 	 * in progress */
    505       1.1    nonaka 
    506       1.1    nonaka 	/* Save the host controller state. */
    507      1.11      matt 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    508       1.1    nonaka 		hp = sc->sc_host[n];
    509      1.11      matt 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    510      1.12    nonaka 			for (i = 0; i < sizeof hp->regs; i += 4) {
    511      1.11      matt 				uint32_t v = HREAD4(hp, i);
    512      1.12    nonaka 				hp->regs[i + 0] = (v >> 0);
    513      1.12    nonaka 				hp->regs[i + 1] = (v >> 8);
    514      1.13    bouyer 				if (i + 3 < sizeof hp->regs) {
    515      1.13    bouyer 					hp->regs[i + 2] = (v >> 16);
    516      1.13    bouyer 					hp->regs[i + 3] = (v >> 24);
    517      1.13    bouyer 				}
    518      1.11      matt 			}
    519      1.11      matt 		} else {
    520      1.12    nonaka 			for (i = 0; i < sizeof hp->regs; i++) {
    521      1.11      matt 				hp->regs[i] = HREAD1(hp, i);
    522      1.11      matt 			}
    523      1.11      matt 		}
    524       1.1    nonaka 	}
    525       1.1    nonaka 	return true;
    526       1.1    nonaka }
    527       1.1    nonaka 
    528       1.1    nonaka bool
    529       1.6    dyoung sdhc_resume(device_t dev, const pmf_qual_t *qual)
    530       1.1    nonaka {
    531       1.1    nonaka 	struct sdhc_softc *sc = device_private(dev);
    532       1.1    nonaka 	struct sdhc_host *hp;
    533      1.12    nonaka 	size_t i;
    534       1.1    nonaka 
    535       1.1    nonaka 	/* Restore the host controller state. */
    536      1.11      matt 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    537       1.1    nonaka 		hp = sc->sc_host[n];
    538       1.1    nonaka 		(void)sdhc_host_reset(hp);
    539      1.11      matt 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    540      1.12    nonaka 			for (i = 0; i < sizeof hp->regs; i += 4) {
    541      1.13    bouyer 				if (i + 3 < sizeof hp->regs) {
    542      1.13    bouyer 					HWRITE4(hp, i,
    543      1.13    bouyer 					    (hp->regs[i + 0] << 0)
    544      1.13    bouyer 					    | (hp->regs[i + 1] << 8)
    545      1.13    bouyer 					    | (hp->regs[i + 2] << 16)
    546      1.13    bouyer 					    | (hp->regs[i + 3] << 24));
    547      1.13    bouyer 				} else {
    548      1.13    bouyer 					HWRITE4(hp, i,
    549      1.13    bouyer 					    (hp->regs[i + 0] << 0)
    550      1.13    bouyer 					    | (hp->regs[i + 1] << 8));
    551      1.13    bouyer 				}
    552      1.11      matt 			}
    553      1.11      matt 		} else {
    554      1.12    nonaka 			for (i = 0; i < sizeof hp->regs; i++) {
    555      1.11      matt 				HWRITE1(hp, i, hp->regs[i]);
    556      1.11      matt 			}
    557      1.11      matt 		}
    558       1.1    nonaka 	}
    559       1.1    nonaka 	return true;
    560       1.1    nonaka }
    561       1.1    nonaka 
    562       1.1    nonaka bool
    563       1.1    nonaka sdhc_shutdown(device_t dev, int flags)
    564       1.1    nonaka {
    565       1.1    nonaka 	struct sdhc_softc *sc = device_private(dev);
    566       1.1    nonaka 	struct sdhc_host *hp;
    567       1.1    nonaka 
    568       1.1    nonaka 	/* XXX chip locks up if we don't disable it before reboot. */
    569      1.11      matt 	for (size_t i = 0; i < sc->sc_nhosts; i++) {
    570       1.1    nonaka 		hp = sc->sc_host[i];
    571       1.1    nonaka 		(void)sdhc_host_reset(hp);
    572       1.1    nonaka 	}
    573       1.1    nonaka 	return true;
    574       1.1    nonaka }
    575       1.1    nonaka 
    576       1.1    nonaka /*
    577       1.1    nonaka  * Reset the host controller.  Called during initialization, when
    578       1.1    nonaka  * cards are removed, upon resume, and during error recovery.
    579       1.1    nonaka  */
    580       1.1    nonaka static int
    581       1.1    nonaka sdhc_host_reset1(sdmmc_chipset_handle_t sch)
    582       1.1    nonaka {
    583       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    584      1.11      matt 	uint32_t sdhcimask;
    585       1.1    nonaka 	int error;
    586       1.1    nonaka 
    587       1.1    nonaka 	/* Don't lock. */
    588       1.1    nonaka 
    589       1.1    nonaka 	/* Disable all interrupts. */
    590      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    591      1.11      matt 		HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
    592      1.11      matt 	} else {
    593      1.11      matt 		HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
    594      1.11      matt 	}
    595       1.1    nonaka 
    596       1.1    nonaka 	/*
    597       1.1    nonaka 	 * Reset the entire host controller and wait up to 100ms for
    598       1.1    nonaka 	 * the controller to clear the reset bit.
    599       1.1    nonaka 	 */
    600       1.1    nonaka 	error = sdhc_soft_reset(hp, SDHC_RESET_ALL);
    601       1.1    nonaka 	if (error)
    602       1.1    nonaka 		goto out;
    603       1.1    nonaka 
    604       1.1    nonaka 	/* Set data timeout counter value to max for now. */
    605       1.1    nonaka 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    606      1.29      matt #if 1
    607      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    608      1.11      matt 		HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
    609      1.11      matt #endif
    610       1.1    nonaka 
    611       1.1    nonaka 	/* Enable interrupts. */
    612      1.29      matt 	mutex_enter(&hp->intr_mtx);
    613       1.1    nonaka 	sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
    614       1.1    nonaka 	    SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
    615       1.1    nonaka 	    SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
    616       1.1    nonaka 	    SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
    617      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    618      1.11      matt 		sdhcimask |= SDHC_EINTR_STATUS_MASK << 16;
    619      1.11      matt 		HWRITE4(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
    620      1.11      matt 		sdhcimask ^=
    621      1.11      matt 		    (SDHC_EINTR_STATUS_MASK ^ SDHC_EINTR_SIGNAL_MASK) << 16;
    622      1.11      matt 		sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
    623      1.11      matt 		HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
    624      1.11      matt 	} else {
    625      1.11      matt 		HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
    626      1.11      matt 		HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
    627      1.11      matt 		sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
    628      1.11      matt 		HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
    629      1.11      matt 		HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
    630      1.11      matt 	}
    631      1.29      matt 	mutex_exit(&hp->intr_mtx);
    632       1.1    nonaka 
    633       1.1    nonaka out:
    634       1.1    nonaka 	return error;
    635       1.1    nonaka }
    636       1.1    nonaka 
    637       1.1    nonaka static int
    638       1.1    nonaka sdhc_host_reset(sdmmc_chipset_handle_t sch)
    639       1.1    nonaka {
    640       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    641       1.1    nonaka 	int error;
    642       1.1    nonaka 
    643       1.1    nonaka 	mutex_enter(&hp->host_mtx);
    644       1.1    nonaka 	error = sdhc_host_reset1(sch);
    645       1.1    nonaka 	mutex_exit(&hp->host_mtx);
    646       1.1    nonaka 
    647       1.1    nonaka 	return error;
    648       1.1    nonaka }
    649       1.1    nonaka 
    650       1.1    nonaka static uint32_t
    651       1.1    nonaka sdhc_host_ocr(sdmmc_chipset_handle_t sch)
    652       1.1    nonaka {
    653       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    654       1.1    nonaka 
    655       1.1    nonaka 	return hp->ocr;
    656       1.1    nonaka }
    657       1.1    nonaka 
    658       1.1    nonaka static int
    659       1.1    nonaka sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
    660       1.1    nonaka {
    661       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    662       1.1    nonaka 
    663       1.1    nonaka 	return hp->maxblklen;
    664       1.1    nonaka }
    665       1.1    nonaka 
    666       1.1    nonaka /*
    667       1.1    nonaka  * Return non-zero if the card is currently inserted.
    668       1.1    nonaka  */
    669       1.1    nonaka static int
    670       1.1    nonaka sdhc_card_detect(sdmmc_chipset_handle_t sch)
    671       1.1    nonaka {
    672       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    673       1.1    nonaka 	int r;
    674       1.1    nonaka 
    675      1.32  kiyohara 	if (hp->sc->sc_vendor_card_detect)
    676      1.32  kiyohara 		return (*hp->sc->sc_vendor_card_detect)(hp->sc);
    677      1.32  kiyohara 
    678       1.1    nonaka 	mutex_enter(&hp->host_mtx);
    679       1.1    nonaka 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED);
    680       1.1    nonaka 	mutex_exit(&hp->host_mtx);
    681       1.1    nonaka 
    682      1.11      matt 	return r ? 1 : 0;
    683       1.1    nonaka }
    684       1.1    nonaka 
    685       1.1    nonaka /*
    686       1.1    nonaka  * Return non-zero if the card is currently write-protected.
    687       1.1    nonaka  */
    688       1.1    nonaka static int
    689       1.1    nonaka sdhc_write_protect(sdmmc_chipset_handle_t sch)
    690       1.1    nonaka {
    691       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    692       1.1    nonaka 	int r;
    693       1.1    nonaka 
    694      1.32  kiyohara 	if (hp->sc->sc_vendor_write_protect)
    695      1.32  kiyohara 		return (*hp->sc->sc_vendor_write_protect)(hp->sc);
    696      1.32  kiyohara 
    697       1.1    nonaka 	mutex_enter(&hp->host_mtx);
    698       1.1    nonaka 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH);
    699       1.1    nonaka 	mutex_exit(&hp->host_mtx);
    700       1.1    nonaka 
    701      1.12    nonaka 	return r ? 0 : 1;
    702       1.1    nonaka }
    703       1.1    nonaka 
    704       1.1    nonaka /*
    705       1.1    nonaka  * Set or change SD bus voltage and enable or disable SD bus power.
    706       1.1    nonaka  * Return zero on success.
    707       1.1    nonaka  */
    708       1.1    nonaka static int
    709       1.1    nonaka sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    710       1.1    nonaka {
    711       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    712       1.1    nonaka 	uint8_t vdd;
    713       1.1    nonaka 	int error = 0;
    714      1.32  kiyohara 	const uint32_t pcmask =
    715      1.32  kiyohara 	    ~(SDHC_BUS_POWER | (SDHC_VOLTAGE_MASK << SDHC_VOLTAGE_SHIFT));
    716       1.1    nonaka 
    717       1.1    nonaka 	mutex_enter(&hp->host_mtx);
    718       1.1    nonaka 
    719       1.1    nonaka 	/*
    720       1.1    nonaka 	 * Disable bus power before voltage change.
    721       1.1    nonaka 	 */
    722      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)
    723      1.11      matt 	    && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_PWR0))
    724       1.1    nonaka 		HWRITE1(hp, SDHC_POWER_CTL, 0);
    725       1.1    nonaka 
    726       1.1    nonaka 	/* If power is disabled, reset the host and return now. */
    727       1.1    nonaka 	if (ocr == 0) {
    728       1.1    nonaka 		(void)sdhc_host_reset1(hp);
    729       1.1    nonaka 		goto out;
    730       1.1    nonaka 	}
    731       1.1    nonaka 
    732       1.1    nonaka 	/*
    733       1.1    nonaka 	 * Select the lowest voltage according to capabilities.
    734       1.1    nonaka 	 */
    735       1.1    nonaka 	ocr &= hp->ocr;
    736      1.11      matt 	if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V)) {
    737       1.1    nonaka 		vdd = SDHC_VOLTAGE_1_8V;
    738      1.11      matt 	} else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V)) {
    739       1.1    nonaka 		vdd = SDHC_VOLTAGE_3_0V;
    740      1.11      matt 	} else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) {
    741       1.1    nonaka 		vdd = SDHC_VOLTAGE_3_3V;
    742      1.11      matt 	} else {
    743       1.1    nonaka 		/* Unsupported voltage level requested. */
    744       1.1    nonaka 		error = EINVAL;
    745       1.1    nonaka 		goto out;
    746       1.1    nonaka 	}
    747       1.1    nonaka 
    748      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    749      1.11      matt 		/*
    750      1.11      matt 		 * Enable bus power.  Wait at least 1 ms (or 74 clocks) plus
    751      1.11      matt 		 * voltage ramp until power rises.
    752      1.11      matt 		 */
    753      1.11      matt 		HWRITE1(hp, SDHC_POWER_CTL,
    754      1.32  kiyohara 		    HREAD1(hp, SDHC_POWER_CTL) & pcmask);
    755      1.32  kiyohara 		sdmmc_delay(1);
    756      1.32  kiyohara 		HWRITE1(hp, SDHC_POWER_CTL, (vdd << SDHC_VOLTAGE_SHIFT));
    757      1.32  kiyohara 		sdmmc_delay(1);
    758      1.32  kiyohara 		HSET1(hp, SDHC_POWER_CTL, SDHC_BUS_POWER);
    759      1.11      matt 		sdmmc_delay(10000);
    760       1.1    nonaka 
    761      1.11      matt 		/*
    762      1.11      matt 		 * The host system may not power the bus due to battery low,
    763      1.11      matt 		 * etc.  In that case, the host controller should clear the
    764      1.11      matt 		 * bus power bit.
    765      1.11      matt 		 */
    766      1.11      matt 		if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
    767      1.11      matt 			error = ENXIO;
    768      1.11      matt 			goto out;
    769      1.11      matt 		}
    770       1.1    nonaka 	}
    771       1.1    nonaka 
    772       1.1    nonaka out:
    773       1.1    nonaka 	mutex_exit(&hp->host_mtx);
    774       1.1    nonaka 
    775       1.1    nonaka 	return error;
    776       1.1    nonaka }
    777       1.1    nonaka 
    778       1.1    nonaka /*
    779       1.1    nonaka  * Return the smallest possible base clock frequency divisor value
    780       1.1    nonaka  * for the CLOCK_CTL register to produce `freq' (KHz).
    781       1.1    nonaka  */
    782      1.11      matt static bool
    783      1.11      matt sdhc_clock_divisor(struct sdhc_host *hp, u_int freq, u_int *divp)
    784       1.1    nonaka {
    785      1.11      matt 	u_int div;
    786       1.1    nonaka 
    787      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_CGM)) {
    788      1.11      matt 		for (div = hp->clkbase / freq; div <= 0x3ff; div++) {
    789      1.11      matt 			if ((hp->clkbase / div) <= freq) {
    790      1.11      matt 				*divp = SDHC_SDCLK_CGM
    791      1.11      matt 				    | ((div & 0x300) << SDHC_SDCLK_XDIV_SHIFT)
    792      1.11      matt 				    | ((div & 0x0ff) << SDHC_SDCLK_DIV_SHIFT);
    793      1.18  jakllsch 				//freq = hp->clkbase / div;
    794      1.11      matt 				return true;
    795      1.11      matt 			}
    796      1.11      matt 		}
    797      1.11      matt 		/* No divisor found. */
    798      1.11      matt 		return false;
    799      1.11      matt 	}
    800      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_DVS)) {
    801      1.11      matt 		u_int dvs = (hp->clkbase + freq - 1) / freq;
    802      1.11      matt 		u_int roundup = dvs & 1;
    803      1.11      matt 		for (dvs >>= 1, div = 1; div <= 256; div <<= 1, dvs >>= 1) {
    804      1.11      matt 			if (dvs + roundup <= 16) {
    805      1.11      matt 				dvs += roundup - 1;
    806      1.11      matt 				*divp = (div << SDHC_SDCLK_DIV_SHIFT)
    807      1.11      matt 				    |   (dvs << SDHC_SDCLK_DVS_SHIFT);
    808      1.11      matt 				DPRINTF(2,
    809      1.11      matt 				    ("%s: divisor for freq %u is %u * %u\n",
    810      1.11      matt 				    HDEVNAME(hp), freq, div * 2, dvs + 1));
    811      1.18  jakllsch 				//freq = hp->clkbase / (div * 2) * (dvs + 1);
    812      1.11      matt 				return true;
    813       1.9      matt 			}
    814      1.11      matt 			/*
    815      1.11      matt 			 * If we drop bits, we need to round up the divisor.
    816      1.11      matt 			 */
    817      1.11      matt 			roundup |= dvs & 1;
    818       1.9      matt 		}
    819      1.18  jakllsch 		/* No divisor found. */
    820      1.18  jakllsch 		return false;
    821      1.38  jakllsch 	}
    822      1.38  jakllsch 	if (hp->sc->sc_clkmsk != 0) {
    823      1.38  jakllsch 		div = howmany(hp->clkbase, freq);
    824      1.38  jakllsch 		if (div > (hp->sc->sc_clkmsk >> (ffs(hp->sc->sc_clkmsk) - 1)))
    825      1.38  jakllsch 			return false;
    826      1.38  jakllsch 		*divp = div << (ffs(hp->sc->sc_clkmsk) - 1);
    827      1.38  jakllsch 		//freq = hp->clkbase / div;
    828      1.38  jakllsch 		return true;
    829      1.38  jakllsch 	}
    830      1.38  jakllsch 	if (hp->specver == SDHC_SPEC_VERS_300) {
    831      1.38  jakllsch 		div = howmany(hp->clkbase, freq);
    832  1.44.2.4    martin 		div = div > 1 ? howmany(div, 2) : 0;
    833      1.38  jakllsch 		if (div > 0x3ff)
    834      1.38  jakllsch 			return false;
    835      1.38  jakllsch 		*divp = (((div >> 8) & SDHC_SDCLK_XDIV_MASK)
    836      1.38  jakllsch 			 << SDHC_SDCLK_XDIV_SHIFT) |
    837      1.38  jakllsch 			(((div >> 0) & SDHC_SDCLK_DIV_MASK)
    838      1.38  jakllsch 			 << SDHC_SDCLK_DIV_SHIFT);
    839      1.38  jakllsch 		//freq = hp->clkbase / div;
    840      1.38  jakllsch 		return true;
    841       1.9      matt 	} else {
    842      1.38  jakllsch 		for (div = 1; div <= 256; div *= 2) {
    843      1.38  jakllsch 			if ((hp->clkbase / div) <= freq) {
    844      1.38  jakllsch 				*divp = (div / 2) << SDHC_SDCLK_DIV_SHIFT;
    845      1.38  jakllsch 				//freq = hp->clkbase / div;
    846      1.38  jakllsch 				return true;
    847      1.38  jakllsch 			}
    848      1.38  jakllsch 		}
    849      1.38  jakllsch 		/* No divisor found. */
    850      1.38  jakllsch 		return false;
    851       1.9      matt 	}
    852       1.1    nonaka 	/* No divisor found. */
    853      1.11      matt 	return false;
    854       1.1    nonaka }
    855       1.1    nonaka 
    856       1.1    nonaka /*
    857       1.1    nonaka  * Set or change SDCLK frequency or disable the SD clock.
    858       1.1    nonaka  * Return zero on success.
    859       1.1    nonaka  */
    860       1.1    nonaka static int
    861       1.1    nonaka sdhc_bus_clock(sdmmc_chipset_handle_t sch, int freq)
    862       1.1    nonaka {
    863       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    864      1.11      matt 	u_int div;
    865      1.11      matt 	u_int timo;
    866      1.32  kiyohara 	int16_t reg;
    867       1.1    nonaka 	int error = 0;
    868       1.2    cegger #ifdef DIAGNOSTIC
    869      1.12    nonaka 	bool present;
    870       1.1    nonaka 
    871       1.1    nonaka 	mutex_enter(&hp->host_mtx);
    872      1.12    nonaka 	present = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK);
    873       1.2    cegger 	mutex_exit(&hp->host_mtx);
    874       1.1    nonaka 
    875       1.1    nonaka 	/* Must not stop the clock if commands are in progress. */
    876      1.12    nonaka 	if (present && sdhc_card_detect(hp)) {
    877      1.26      matt 		aprint_normal_dev(hp->sc->sc_dev,
    878      1.26      matt 		    "%s: command in progress\n", __func__);
    879      1.12    nonaka 	}
    880       1.1    nonaka #endif
    881       1.1    nonaka 
    882       1.2    cegger 	mutex_enter(&hp->host_mtx);
    883       1.2    cegger 
    884      1.34      matt 	if (hp->sc->sc_vendor_bus_clock) {
    885      1.34      matt 		error = (*hp->sc->sc_vendor_bus_clock)(hp->sc, freq);
    886      1.34      matt 		if (error != 0)
    887      1.34      matt 			goto out;
    888      1.34      matt 	}
    889      1.34      matt 
    890       1.1    nonaka 	/*
    891       1.1    nonaka 	 * Stop SD clock before changing the frequency.
    892       1.1    nonaka 	 */
    893      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    894      1.11      matt 		HCLR4(hp, SDHC_CLOCK_CTL, 0xfff8);
    895      1.11      matt 		if (freq == SDMMC_SDCLK_OFF) {
    896      1.11      matt 			HSET4(hp, SDHC_CLOCK_CTL, 0x80f0);
    897      1.11      matt 			goto out;
    898      1.11      matt 		}
    899      1.11      matt 	} else {
    900      1.32  kiyohara 		HCLR2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
    901      1.11      matt 		if (freq == SDMMC_SDCLK_OFF)
    902      1.11      matt 			goto out;
    903      1.11      matt 	}
    904       1.1    nonaka 
    905       1.1    nonaka 	/*
    906       1.1    nonaka 	 * Set the minimum base clock frequency divisor.
    907       1.1    nonaka 	 */
    908      1.11      matt 	if (!sdhc_clock_divisor(hp, freq, &div)) {
    909       1.1    nonaka 		/* Invalid base clock frequency or `freq' value. */
    910       1.1    nonaka 		error = EINVAL;
    911       1.1    nonaka 		goto out;
    912       1.1    nonaka 	}
    913      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    914      1.11      matt 		HWRITE4(hp, SDHC_CLOCK_CTL,
    915      1.11      matt 		    div | (SDHC_TIMEOUT_MAX << 16));
    916      1.11      matt 	} else {
    917      1.32  kiyohara 		reg = HREAD2(hp, SDHC_CLOCK_CTL);
    918      1.32  kiyohara 		reg &= (SDHC_INTCLK_STABLE | SDHC_INTCLK_ENABLE);
    919      1.32  kiyohara 		HWRITE2(hp, SDHC_CLOCK_CTL, reg | div);
    920      1.11      matt 	}
    921       1.1    nonaka 
    922       1.1    nonaka 	/*
    923       1.1    nonaka 	 * Start internal clock.  Wait 10ms for stabilization.
    924       1.1    nonaka 	 */
    925      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    926      1.11      matt 		sdmmc_delay(10000);
    927      1.12    nonaka 		HSET4(hp, SDHC_CLOCK_CTL,
    928      1.12    nonaka 		    8 | SDHC_INTCLK_ENABLE | SDHC_INTCLK_STABLE);
    929      1.11      matt 	} else {
    930      1.11      matt 		HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
    931      1.11      matt 		for (timo = 1000; timo > 0; timo--) {
    932      1.12    nonaka 			if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL),
    933      1.12    nonaka 			    SDHC_INTCLK_STABLE))
    934      1.11      matt 				break;
    935      1.11      matt 			sdmmc_delay(10);
    936      1.11      matt 		}
    937      1.11      matt 		if (timo == 0) {
    938      1.11      matt 			error = ETIMEDOUT;
    939      1.11      matt 			goto out;
    940      1.11      matt 		}
    941       1.1    nonaka 	}
    942       1.1    nonaka 
    943      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    944      1.11      matt 		HSET1(hp, SDHC_SOFTWARE_RESET, SDHC_INIT_ACTIVE);
    945      1.11      matt 		/*
    946      1.11      matt 		 * Sending 80 clocks at 400kHz takes 200us.
    947      1.11      matt 		 * So delay for that time + slop and then
    948      1.11      matt 		 * check a few times for completion.
    949      1.11      matt 		 */
    950      1.11      matt 		sdmmc_delay(210);
    951      1.11      matt 		for (timo = 10; timo > 0; timo--) {
    952      1.11      matt 			if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET),
    953      1.11      matt 			    SDHC_INIT_ACTIVE))
    954      1.11      matt 				break;
    955      1.11      matt 			sdmmc_delay(10);
    956      1.11      matt 		}
    957      1.11      matt 		DPRINTF(2,("%s: %u init spins\n", __func__, 10 - timo));
    958      1.12    nonaka 
    959      1.11      matt 		/*
    960      1.11      matt 		 * Enable SD clock.
    961      1.11      matt 		 */
    962      1.11      matt 		HSET4(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
    963      1.11      matt 	} else {
    964      1.11      matt 		/*
    965      1.11      matt 		 * Enable SD clock.
    966      1.11      matt 		 */
    967      1.11      matt 		HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
    968       1.1    nonaka 
    969      1.43  jmcneill 		if (freq > 25000 &&
    970      1.43  jmcneill 		    !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_HS_BIT))
    971      1.11      matt 			HSET1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
    972      1.11      matt 		else
    973      1.11      matt 			HCLR1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
    974      1.11      matt 	}
    975       1.8  kiyohara 
    976       1.1    nonaka out:
    977       1.1    nonaka 	mutex_exit(&hp->host_mtx);
    978       1.1    nonaka 
    979       1.1    nonaka 	return error;
    980       1.1    nonaka }
    981       1.1    nonaka 
    982       1.1    nonaka static int
    983       1.1    nonaka sdhc_bus_width(sdmmc_chipset_handle_t sch, int width)
    984       1.1    nonaka {
    985       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    986       1.1    nonaka 	int reg;
    987       1.1    nonaka 
    988       1.1    nonaka 	switch (width) {
    989       1.1    nonaka 	case 1:
    990       1.1    nonaka 	case 4:
    991       1.1    nonaka 		break;
    992       1.1    nonaka 
    993      1.11      matt 	case 8:
    994      1.11      matt 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_8BIT_MODE))
    995      1.11      matt 			break;
    996      1.11      matt 		/* FALLTHROUGH */
    997       1.1    nonaka 	default:
    998       1.1    nonaka 		DPRINTF(0,("%s: unsupported bus width (%d)\n",
    999       1.1    nonaka 		    HDEVNAME(hp), width));
   1000       1.1    nonaka 		return 1;
   1001       1.1    nonaka 	}
   1002       1.1    nonaka 
   1003       1.1    nonaka 	mutex_enter(&hp->host_mtx);
   1004       1.5  uebayasi 	reg = HREAD1(hp, SDHC_HOST_CTL);
   1005      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1006      1.12    nonaka 		reg &= ~(SDHC_4BIT_MODE|SDHC_ESDHC_8BIT_MODE);
   1007      1.11      matt 		if (width == 4)
   1008      1.11      matt 			reg |= SDHC_4BIT_MODE;
   1009      1.11      matt 		else if (width == 8)
   1010      1.12    nonaka 			reg |= SDHC_ESDHC_8BIT_MODE;
   1011      1.11      matt 	} else {
   1012      1.11      matt 		reg &= ~SDHC_4BIT_MODE;
   1013      1.11      matt 		if (width == 4)
   1014      1.11      matt 			reg |= SDHC_4BIT_MODE;
   1015      1.11      matt 	}
   1016       1.5  uebayasi 	HWRITE1(hp, SDHC_HOST_CTL, reg);
   1017       1.1    nonaka 	mutex_exit(&hp->host_mtx);
   1018       1.1    nonaka 
   1019       1.1    nonaka 	return 0;
   1020       1.1    nonaka }
   1021       1.1    nonaka 
   1022       1.8  kiyohara static int
   1023       1.8  kiyohara sdhc_bus_rod(sdmmc_chipset_handle_t sch, int on)
   1024       1.8  kiyohara {
   1025      1.32  kiyohara 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1026      1.32  kiyohara 
   1027      1.32  kiyohara 	if (hp->sc->sc_vendor_rod)
   1028      1.32  kiyohara 		return (*hp->sc->sc_vendor_rod)(hp->sc, on);
   1029       1.8  kiyohara 
   1030       1.8  kiyohara 	return 0;
   1031       1.8  kiyohara }
   1032       1.8  kiyohara 
   1033       1.1    nonaka static void
   1034       1.1    nonaka sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
   1035       1.1    nonaka {
   1036       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1037       1.1    nonaka 
   1038      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1039      1.29      matt 		mutex_enter(&hp->intr_mtx);
   1040      1.11      matt 		if (enable) {
   1041      1.11      matt 			HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1042      1.11      matt 			HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
   1043      1.11      matt 		} else {
   1044      1.11      matt 			HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
   1045      1.11      matt 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1046      1.11      matt 		}
   1047      1.29      matt 		mutex_exit(&hp->intr_mtx);
   1048       1.1    nonaka 	}
   1049       1.1    nonaka }
   1050       1.1    nonaka 
   1051  1.44.2.3    martin static void
   1052       1.1    nonaka sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
   1053       1.1    nonaka {
   1054       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1055       1.1    nonaka 
   1056      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1057      1.29      matt 		mutex_enter(&hp->intr_mtx);
   1058      1.11      matt 		HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1059      1.29      matt 		mutex_exit(&hp->intr_mtx);
   1060      1.11      matt 	}
   1061       1.1    nonaka }
   1062       1.1    nonaka 
   1063       1.1    nonaka static int
   1064       1.1    nonaka sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value)
   1065       1.1    nonaka {
   1066       1.1    nonaka 	uint32_t state;
   1067       1.1    nonaka 	int timeout;
   1068       1.1    nonaka 
   1069       1.1    nonaka 	for (timeout = 10; timeout > 0; timeout--) {
   1070       1.1    nonaka 		if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value)
   1071       1.1    nonaka 			return 0;
   1072       1.1    nonaka 		sdmmc_delay(10000);
   1073       1.1    nonaka 	}
   1074       1.1    nonaka 	DPRINTF(0,("%s: timeout waiting for %x (state=%x)\n", HDEVNAME(hp),
   1075       1.1    nonaka 	    value, state));
   1076       1.1    nonaka 	return ETIMEDOUT;
   1077       1.1    nonaka }
   1078       1.1    nonaka 
   1079       1.1    nonaka static void
   1080       1.1    nonaka sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
   1081       1.1    nonaka {
   1082       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1083       1.1    nonaka 	int error;
   1084       1.1    nonaka 
   1085      1.26      matt 	if (cmd->c_data && ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1086      1.11      matt 		const uint16_t ready = SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY;
   1087      1.29      matt 		mutex_enter(&hp->intr_mtx);
   1088      1.11      matt 		if (ISSET(hp->flags, SHF_USE_DMA)) {
   1089      1.11      matt 			HCLR2(hp, SDHC_NINTR_SIGNAL_EN, ready);
   1090      1.11      matt 			HCLR2(hp, SDHC_NINTR_STATUS_EN, ready);
   1091      1.11      matt 		} else {
   1092      1.11      matt 			HSET2(hp, SDHC_NINTR_SIGNAL_EN, ready);
   1093      1.11      matt 			HSET2(hp, SDHC_NINTR_STATUS_EN, ready);
   1094  1.44.2.3    martin 		}
   1095      1.29      matt 		mutex_exit(&hp->intr_mtx);
   1096      1.11      matt 	}
   1097      1.11      matt 
   1098       1.1    nonaka 	/*
   1099       1.1    nonaka 	 * Start the MMC command, or mark `cmd' as failed and return.
   1100       1.1    nonaka 	 */
   1101       1.1    nonaka 	error = sdhc_start_command(hp, cmd);
   1102       1.1    nonaka 	if (error) {
   1103       1.1    nonaka 		cmd->c_error = error;
   1104       1.1    nonaka 		goto out;
   1105       1.1    nonaka 	}
   1106       1.1    nonaka 
   1107       1.1    nonaka 	/*
   1108       1.1    nonaka 	 * Wait until the command phase is done, or until the command
   1109       1.1    nonaka 	 * is marked done for any other reason.
   1110       1.1    nonaka 	 */
   1111       1.1    nonaka 	if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT)) {
   1112       1.1    nonaka 		cmd->c_error = ETIMEDOUT;
   1113       1.1    nonaka 		goto out;
   1114       1.1    nonaka 	}
   1115       1.1    nonaka 
   1116       1.1    nonaka 	/*
   1117       1.1    nonaka 	 * The host controller removes bits [0:7] from the response
   1118       1.1    nonaka 	 * data (CRC) and we pass the data up unchanged to the bus
   1119       1.1    nonaka 	 * driver (without padding).
   1120       1.1    nonaka 	 */
   1121       1.1    nonaka 	mutex_enter(&hp->host_mtx);
   1122       1.1    nonaka 	if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
   1123      1.23      matt 		cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE + 0);
   1124      1.23      matt 		if (ISSET(cmd->c_flags, SCF_RSP_136)) {
   1125      1.23      matt 			cmd->c_resp[1] = HREAD4(hp, SDHC_RESPONSE + 4);
   1126      1.23      matt 			cmd->c_resp[2] = HREAD4(hp, SDHC_RESPONSE + 8);
   1127      1.23      matt 			cmd->c_resp[3] = HREAD4(hp, SDHC_RESPONSE + 12);
   1128      1.32  kiyohara 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_RSP136_CRC)) {
   1129      1.32  kiyohara 				cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
   1130      1.32  kiyohara 				    (cmd->c_resp[1] << 24);
   1131      1.32  kiyohara 				cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
   1132      1.32  kiyohara 				    (cmd->c_resp[2] << 24);
   1133      1.32  kiyohara 				cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
   1134      1.32  kiyohara 				    (cmd->c_resp[3] << 24);
   1135      1.32  kiyohara 				cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
   1136      1.32  kiyohara 			}
   1137       1.1    nonaka 		}
   1138       1.1    nonaka 	}
   1139       1.1    nonaka 	mutex_exit(&hp->host_mtx);
   1140      1.25      matt 	DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0]));
   1141       1.1    nonaka 
   1142       1.1    nonaka 	/*
   1143       1.1    nonaka 	 * If the command has data to transfer in any direction,
   1144       1.1    nonaka 	 * execute the transfer now.
   1145       1.1    nonaka 	 */
   1146       1.1    nonaka 	if (cmd->c_error == 0 && cmd->c_data != NULL)
   1147       1.1    nonaka 		sdhc_transfer_data(hp, cmd);
   1148      1.42  jakllsch 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY)) {
   1149      1.42  jakllsch 		if (!sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE, hz * 10)) {
   1150      1.42  jakllsch 			cmd->c_error = ETIMEDOUT;
   1151      1.42  jakllsch 			goto out;
   1152      1.42  jakllsch 		}
   1153      1.42  jakllsch 	}
   1154       1.1    nonaka 
   1155       1.1    nonaka out:
   1156      1.14      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)
   1157      1.14      matt 	    && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_LED_ON)) {
   1158      1.11      matt 		mutex_enter(&hp->host_mtx);
   1159      1.11      matt 		/* Turn off the LED. */
   1160      1.11      matt 		HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
   1161      1.11      matt 		mutex_exit(&hp->host_mtx);
   1162      1.11      matt 	}
   1163       1.1    nonaka 	SET(cmd->c_flags, SCF_ITSDONE);
   1164       1.1    nonaka 
   1165       1.1    nonaka 	DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp),
   1166       1.1    nonaka 	    cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort",
   1167       1.1    nonaka 	    cmd->c_flags, cmd->c_error));
   1168       1.1    nonaka }
   1169       1.1    nonaka 
   1170       1.1    nonaka static int
   1171       1.1    nonaka sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1172       1.1    nonaka {
   1173      1.11      matt 	struct sdhc_softc * const sc = hp->sc;
   1174       1.1    nonaka 	uint16_t blksize = 0;
   1175       1.1    nonaka 	uint16_t blkcount = 0;
   1176       1.1    nonaka 	uint16_t mode;
   1177       1.1    nonaka 	uint16_t command;
   1178       1.1    nonaka 	int error;
   1179       1.1    nonaka 
   1180      1.11      matt 	DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x, status=%#x\n",
   1181       1.7    nonaka 	    HDEVNAME(hp), cmd->c_opcode, cmd->c_arg, cmd->c_data,
   1182      1.11      matt 	    cmd->c_datalen, cmd->c_flags, HREAD4(hp, SDHC_NINTR_STATUS)));
   1183       1.1    nonaka 
   1184       1.1    nonaka 	/*
   1185       1.1    nonaka 	 * The maximum block length for commands should be the minimum
   1186       1.1    nonaka 	 * of the host buffer size and the card buffer size. (1.7.2)
   1187       1.1    nonaka 	 */
   1188       1.1    nonaka 
   1189       1.1    nonaka 	/* Fragment the data into proper blocks. */
   1190       1.1    nonaka 	if (cmd->c_datalen > 0) {
   1191       1.1    nonaka 		blksize = MIN(cmd->c_datalen, cmd->c_blklen);
   1192       1.1    nonaka 		blkcount = cmd->c_datalen / blksize;
   1193       1.1    nonaka 		if (cmd->c_datalen % blksize > 0) {
   1194       1.1    nonaka 			/* XXX: Split this command. (1.7.4) */
   1195      1.11      matt 			aprint_error_dev(sc->sc_dev,
   1196       1.1    nonaka 			    "data not a multiple of %u bytes\n", blksize);
   1197       1.1    nonaka 			return EINVAL;
   1198       1.1    nonaka 		}
   1199       1.1    nonaka 	}
   1200       1.1    nonaka 
   1201       1.1    nonaka 	/* Check limit imposed by 9-bit block count. (1.7.2) */
   1202       1.1    nonaka 	if (blkcount > SDHC_BLOCK_COUNT_MAX) {
   1203      1.11      matt 		aprint_error_dev(sc->sc_dev, "too much data\n");
   1204       1.1    nonaka 		return EINVAL;
   1205       1.1    nonaka 	}
   1206       1.1    nonaka 
   1207       1.1    nonaka 	/* Prepare transfer mode register value. (2.2.5) */
   1208      1.15  jakllsch 	mode = SDHC_BLOCK_COUNT_ENABLE;
   1209       1.1    nonaka 	if (ISSET(cmd->c_flags, SCF_CMD_READ))
   1210       1.1    nonaka 		mode |= SDHC_READ_MODE;
   1211      1.15  jakllsch 	if (blkcount > 1) {
   1212      1.15  jakllsch 		mode |= SDHC_MULTI_BLOCK_MODE;
   1213      1.15  jakllsch 		/* XXX only for memory commands? */
   1214      1.15  jakllsch 		mode |= SDHC_AUTO_CMD12_ENABLE;
   1215       1.1    nonaka 	}
   1216  1.44.2.3    martin 	if (cmd->c_dmamap != NULL && cmd->c_datalen > 0 &&
   1217  1.44.2.8   msaitoh 	    ISSET(hp->flags,  SHF_MODE_DMAEN)) {
   1218      1.19  jakllsch 		mode |= SDHC_DMA_ENABLE;
   1219       1.7    nonaka 	}
   1220       1.1    nonaka 
   1221       1.1    nonaka 	/*
   1222       1.1    nonaka 	 * Prepare command register value. (2.2.6)
   1223       1.1    nonaka 	 */
   1224      1.12    nonaka 	command = (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT;
   1225       1.1    nonaka 
   1226       1.1    nonaka 	if (ISSET(cmd->c_flags, SCF_RSP_CRC))
   1227       1.1    nonaka 		command |= SDHC_CRC_CHECK_ENABLE;
   1228       1.1    nonaka 	if (ISSET(cmd->c_flags, SCF_RSP_IDX))
   1229       1.1    nonaka 		command |= SDHC_INDEX_CHECK_ENABLE;
   1230       1.1    nonaka 	if (cmd->c_data != NULL)
   1231       1.1    nonaka 		command |= SDHC_DATA_PRESENT_SELECT;
   1232       1.1    nonaka 
   1233       1.1    nonaka 	if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
   1234       1.1    nonaka 		command |= SDHC_NO_RESPONSE;
   1235       1.1    nonaka 	else if (ISSET(cmd->c_flags, SCF_RSP_136))
   1236       1.1    nonaka 		command |= SDHC_RESP_LEN_136;
   1237       1.1    nonaka 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
   1238       1.1    nonaka 		command |= SDHC_RESP_LEN_48_CHK_BUSY;
   1239       1.1    nonaka 	else
   1240       1.1    nonaka 		command |= SDHC_RESP_LEN_48;
   1241       1.1    nonaka 
   1242       1.1    nonaka 	/* Wait until command and data inhibit bits are clear. (1.5) */
   1243       1.1    nonaka 	error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0);
   1244       1.1    nonaka 	if (error)
   1245       1.1    nonaka 		return error;
   1246       1.1    nonaka 
   1247       1.1    nonaka 	DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n",
   1248       1.1    nonaka 	    HDEVNAME(hp), blksize, blkcount, mode, command));
   1249       1.1    nonaka 
   1250      1.44   hkenken 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1251      1.44   hkenken 		blksize |= (MAX(0, PAGE_SHIFT - 12) & SDHC_DMA_BOUNDARY_MASK) <<
   1252      1.44   hkenken 		    SDHC_DMA_BOUNDARY_SHIFT;	/* PAGE_SIZE DMA boundary */
   1253      1.44   hkenken 	}
   1254      1.19  jakllsch 
   1255       1.1    nonaka 	mutex_enter(&hp->host_mtx);
   1256       1.1    nonaka 
   1257      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1258      1.11      matt 		/* Alert the user not to remove the card. */
   1259      1.11      matt 		HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
   1260      1.11      matt 	}
   1261       1.1    nonaka 
   1262       1.7    nonaka 	/* Set DMA start address. */
   1263  1.44.2.8   msaitoh 	if (ISSET(mode, SDHC_DMA_ENABLE) &&
   1264  1.44.2.8   msaitoh 	    !ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA))
   1265       1.7    nonaka 		HWRITE4(hp, SDHC_DMA_ADDR, cmd->c_dmamap->dm_segs[0].ds_addr);
   1266       1.7    nonaka 
   1267       1.1    nonaka 	/*
   1268       1.1    nonaka 	 * Start a CPU data transfer.  Writing to the high order byte
   1269       1.1    nonaka 	 * of the SDHC_COMMAND register triggers the SD command. (1.5)
   1270       1.1    nonaka 	 */
   1271      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   1272      1.11      matt 		HWRITE4(hp, SDHC_BLOCK_SIZE, blksize | (blkcount << 16));
   1273      1.11      matt 		HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
   1274      1.11      matt 		HWRITE4(hp, SDHC_TRANSFER_MODE, mode | (command << 16));
   1275      1.11      matt 	} else {
   1276      1.11      matt 		HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
   1277      1.15  jakllsch 		HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
   1278      1.11      matt 		HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
   1279      1.15  jakllsch 		HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
   1280      1.11      matt 		HWRITE2(hp, SDHC_COMMAND, command);
   1281      1.11      matt 	}
   1282       1.1    nonaka 
   1283       1.1    nonaka 	mutex_exit(&hp->host_mtx);
   1284       1.1    nonaka 
   1285       1.1    nonaka 	return 0;
   1286       1.1    nonaka }
   1287       1.1    nonaka 
   1288       1.1    nonaka static void
   1289       1.1    nonaka sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1290       1.1    nonaka {
   1291  1.44.2.5    martin 	struct sdhc_softc *sc = hp->sc;
   1292       1.1    nonaka 	int error;
   1293       1.1    nonaka 
   1294       1.1    nonaka 	DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp),
   1295       1.1    nonaka 	    MMC_R1(cmd->c_resp), cmd->c_datalen));
   1296       1.1    nonaka 
   1297       1.1    nonaka #ifdef SDHC_DEBUG
   1298       1.1    nonaka 	/* XXX I forgot why I wanted to know when this happens :-( */
   1299       1.1    nonaka 	if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
   1300       1.1    nonaka 	    ISSET(MMC_R1(cmd->c_resp), 0xcb00)) {
   1301       1.1    nonaka 		aprint_error_dev(hp->sc->sc_dev,
   1302       1.1    nonaka 		    "CMD52/53 error response flags %#x\n",
   1303       1.1    nonaka 		    MMC_R1(cmd->c_resp) & 0xff00);
   1304       1.1    nonaka 	}
   1305       1.1    nonaka #endif
   1306       1.1    nonaka 
   1307  1.44.2.3    martin 	if (cmd->c_dmamap != NULL) {
   1308  1.44.2.3    martin 		if (hp->sc->sc_vendor_transfer_data_dma != NULL) {
   1309  1.44.2.5    martin 			error = hp->sc->sc_vendor_transfer_data_dma(sc, cmd);
   1310  1.44.2.3    martin 			if (error == 0 && !sdhc_wait_intr(hp,
   1311  1.44.2.3    martin 			    SDHC_TRANSFER_COMPLETE, SDHC_TRANSFER_TIMEOUT)) {
   1312  1.44.2.3    martin 				error = ETIMEDOUT;
   1313  1.44.2.3    martin 			}
   1314  1.44.2.3    martin 		} else {
   1315  1.44.2.3    martin 			error = sdhc_transfer_data_dma(hp, cmd);
   1316  1.44.2.3    martin 		}
   1317  1.44.2.3    martin 	} else
   1318       1.7    nonaka 		error = sdhc_transfer_data_pio(hp, cmd);
   1319       1.1    nonaka 	if (error)
   1320       1.1    nonaka 		cmd->c_error = error;
   1321       1.1    nonaka 	SET(cmd->c_flags, SCF_ITSDONE);
   1322       1.1    nonaka 
   1323       1.1    nonaka 	DPRINTF(1,("%s: data transfer done (error=%d)\n",
   1324       1.1    nonaka 	    HDEVNAME(hp), cmd->c_error));
   1325       1.1    nonaka }
   1326       1.1    nonaka 
   1327       1.1    nonaka static int
   1328       1.7    nonaka sdhc_transfer_data_dma(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1329       1.7    nonaka {
   1330      1.19  jakllsch 	bus_dma_segment_t *dm_segs = cmd->c_dmamap->dm_segs;
   1331      1.19  jakllsch 	bus_addr_t posaddr;
   1332      1.19  jakllsch 	bus_addr_t segaddr;
   1333      1.19  jakllsch 	bus_size_t seglen;
   1334      1.19  jakllsch 	u_int seg = 0;
   1335       1.7    nonaka 	int error = 0;
   1336      1.19  jakllsch 	int status;
   1337       1.7    nonaka 
   1338      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_DMA_INTERRUPT);
   1339      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_DMA_INTERRUPT);
   1340      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
   1341      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
   1342      1.11      matt 
   1343       1.7    nonaka 	for (;;) {
   1344      1.19  jakllsch 		status = sdhc_wait_intr(hp,
   1345       1.7    nonaka 		    SDHC_DMA_INTERRUPT|SDHC_TRANSFER_COMPLETE,
   1346      1.19  jakllsch 		    SDHC_DMA_TIMEOUT);
   1347      1.19  jakllsch 
   1348      1.19  jakllsch 		if (status & SDHC_TRANSFER_COMPLETE) {
   1349      1.19  jakllsch 			break;
   1350      1.19  jakllsch 		}
   1351      1.19  jakllsch 		if (!status) {
   1352       1.7    nonaka 			error = ETIMEDOUT;
   1353       1.7    nonaka 			break;
   1354       1.7    nonaka 		}
   1355      1.19  jakllsch 		if ((status & SDHC_DMA_INTERRUPT) == 0) {
   1356      1.19  jakllsch 			continue;
   1357      1.19  jakllsch 		}
   1358      1.19  jakllsch 
   1359      1.19  jakllsch 		/* DMA Interrupt (boundary crossing) */
   1360       1.7    nonaka 
   1361      1.19  jakllsch 		segaddr = dm_segs[seg].ds_addr;
   1362      1.19  jakllsch 		seglen = dm_segs[seg].ds_len;
   1363      1.19  jakllsch 		mutex_enter(&hp->host_mtx);
   1364      1.19  jakllsch 		posaddr = HREAD4(hp, SDHC_DMA_ADDR);
   1365      1.19  jakllsch 		mutex_exit(&hp->host_mtx);
   1366       1.7    nonaka 
   1367      1.19  jakllsch 		if ((seg == (cmd->c_dmamap->dm_nsegs-1)) && (posaddr == (segaddr + seglen))) {
   1368      1.37  jakllsch 			continue;
   1369      1.19  jakllsch 		}
   1370      1.19  jakllsch 		mutex_enter(&hp->host_mtx);
   1371      1.19  jakllsch 		if ((posaddr >= segaddr) && (posaddr < (segaddr + seglen)))
   1372      1.19  jakllsch 			HWRITE4(hp, SDHC_DMA_ADDR, posaddr);
   1373      1.19  jakllsch 		else if ((posaddr >= segaddr) && (posaddr == (segaddr + seglen)) && (seg + 1) < cmd->c_dmamap->dm_nsegs)
   1374      1.19  jakllsch 			HWRITE4(hp, SDHC_DMA_ADDR, dm_segs[++seg].ds_addr);
   1375      1.19  jakllsch 		mutex_exit(&hp->host_mtx);
   1376      1.19  jakllsch 		KASSERT(seg < cmd->c_dmamap->dm_nsegs);
   1377       1.7    nonaka 	}
   1378       1.7    nonaka 
   1379       1.7    nonaka 	return error;
   1380       1.7    nonaka }
   1381       1.7    nonaka 
   1382       1.7    nonaka static int
   1383       1.1    nonaka sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1384       1.1    nonaka {
   1385       1.1    nonaka 	uint8_t *data = cmd->c_data;
   1386      1.12    nonaka 	void (*pio_func)(struct sdhc_host *, uint8_t *, u_int);
   1387      1.11      matt 	u_int len, datalen;
   1388      1.11      matt 	u_int imask;
   1389      1.11      matt 	u_int pmask;
   1390       1.1    nonaka 	int error = 0;
   1391       1.1    nonaka 
   1392      1.11      matt 	if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
   1393      1.11      matt 		imask = SDHC_BUFFER_READ_READY;
   1394      1.11      matt 		pmask = SDHC_BUFFER_READ_ENABLE;
   1395      1.11      matt 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1396      1.11      matt 			pio_func = esdhc_read_data_pio;
   1397      1.11      matt 		} else {
   1398      1.11      matt 			pio_func = sdhc_read_data_pio;
   1399      1.11      matt 		}
   1400      1.11      matt 	} else {
   1401      1.11      matt 		imask = SDHC_BUFFER_WRITE_READY;
   1402      1.11      matt 		pmask = SDHC_BUFFER_WRITE_ENABLE;
   1403      1.11      matt 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1404      1.11      matt 			pio_func = esdhc_write_data_pio;
   1405      1.11      matt 		} else {
   1406      1.11      matt 			pio_func = sdhc_write_data_pio;
   1407      1.11      matt 		}
   1408      1.11      matt 	}
   1409       1.1    nonaka 	datalen = cmd->c_datalen;
   1410       1.1    nonaka 
   1411      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & imask);
   1412      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
   1413      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
   1414      1.11      matt 
   1415       1.1    nonaka 	while (datalen > 0) {
   1416      1.11      matt 		if (!ISSET(HREAD4(hp, SDHC_PRESENT_STATE), imask)) {
   1417      1.29      matt 			mutex_enter(&hp->intr_mtx);
   1418      1.11      matt 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   1419      1.11      matt 				HSET4(hp, SDHC_NINTR_SIGNAL_EN, imask);
   1420      1.11      matt 			} else {
   1421      1.11      matt 				HSET2(hp, SDHC_NINTR_SIGNAL_EN, imask);
   1422      1.11      matt 			}
   1423      1.29      matt 			mutex_exit(&hp->intr_mtx);
   1424      1.11      matt 			if (!sdhc_wait_intr(hp, imask, SDHC_BUFFER_TIMEOUT)) {
   1425      1.11      matt 				error = ETIMEDOUT;
   1426      1.11      matt 				break;
   1427      1.11      matt 			}
   1428      1.11      matt 
   1429      1.11      matt 			error = sdhc_wait_state(hp, pmask, pmask);
   1430      1.11      matt 			if (error)
   1431      1.11      matt 				break;
   1432       1.1    nonaka 		}
   1433       1.1    nonaka 
   1434       1.1    nonaka 		len = MIN(datalen, cmd->c_blklen);
   1435      1.11      matt 		(*pio_func)(hp, data, len);
   1436      1.11      matt 		DPRINTF(2,("%s: pio data transfer %u @ %p\n",
   1437      1.11      matt 		    HDEVNAME(hp), len, data));
   1438       1.1    nonaka 
   1439       1.1    nonaka 		data += len;
   1440       1.1    nonaka 		datalen -= len;
   1441       1.1    nonaka 	}
   1442       1.1    nonaka 
   1443       1.1    nonaka 	if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
   1444       1.1    nonaka 	    SDHC_TRANSFER_TIMEOUT))
   1445       1.1    nonaka 		error = ETIMEDOUT;
   1446       1.1    nonaka 
   1447       1.1    nonaka 	return error;
   1448       1.1    nonaka }
   1449       1.1    nonaka 
   1450       1.1    nonaka static void
   1451      1.11      matt sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1452       1.1    nonaka {
   1453       1.1    nonaka 
   1454       1.1    nonaka 	if (((__uintptr_t)data & 3) == 0) {
   1455       1.1    nonaka 		while (datalen > 3) {
   1456      1.29      matt 			*(uint32_t *)data = le32toh(HREAD4(hp, SDHC_DATA));
   1457       1.1    nonaka 			data += 4;
   1458       1.1    nonaka 			datalen -= 4;
   1459       1.1    nonaka 		}
   1460       1.1    nonaka 		if (datalen > 1) {
   1461      1.29      matt 			*(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
   1462       1.1    nonaka 			data += 2;
   1463       1.1    nonaka 			datalen -= 2;
   1464       1.1    nonaka 		}
   1465       1.1    nonaka 		if (datalen > 0) {
   1466       1.1    nonaka 			*data = HREAD1(hp, SDHC_DATA);
   1467       1.1    nonaka 			data += 1;
   1468       1.1    nonaka 			datalen -= 1;
   1469       1.1    nonaka 		}
   1470       1.1    nonaka 	} else if (((__uintptr_t)data & 1) == 0) {
   1471       1.1    nonaka 		while (datalen > 1) {
   1472      1.29      matt 			*(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
   1473       1.1    nonaka 			data += 2;
   1474       1.1    nonaka 			datalen -= 2;
   1475       1.1    nonaka 		}
   1476       1.1    nonaka 		if (datalen > 0) {
   1477       1.1    nonaka 			*data = HREAD1(hp, SDHC_DATA);
   1478       1.1    nonaka 			data += 1;
   1479       1.1    nonaka 			datalen -= 1;
   1480       1.1    nonaka 		}
   1481       1.1    nonaka 	} else {
   1482       1.1    nonaka 		while (datalen > 0) {
   1483       1.1    nonaka 			*data = HREAD1(hp, SDHC_DATA);
   1484       1.1    nonaka 			data += 1;
   1485       1.1    nonaka 			datalen -= 1;
   1486       1.1    nonaka 		}
   1487       1.1    nonaka 	}
   1488       1.1    nonaka }
   1489       1.1    nonaka 
   1490       1.1    nonaka static void
   1491      1.11      matt sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1492       1.1    nonaka {
   1493       1.1    nonaka 
   1494       1.1    nonaka 	if (((__uintptr_t)data & 3) == 0) {
   1495       1.1    nonaka 		while (datalen > 3) {
   1496      1.29      matt 			HWRITE4(hp, SDHC_DATA, htole32(*(uint32_t *)data));
   1497       1.1    nonaka 			data += 4;
   1498       1.1    nonaka 			datalen -= 4;
   1499       1.1    nonaka 		}
   1500       1.1    nonaka 		if (datalen > 1) {
   1501      1.29      matt 			HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
   1502       1.1    nonaka 			data += 2;
   1503       1.1    nonaka 			datalen -= 2;
   1504       1.1    nonaka 		}
   1505       1.1    nonaka 		if (datalen > 0) {
   1506       1.1    nonaka 			HWRITE1(hp, SDHC_DATA, *data);
   1507       1.1    nonaka 			data += 1;
   1508       1.1    nonaka 			datalen -= 1;
   1509       1.1    nonaka 		}
   1510       1.1    nonaka 	} else if (((__uintptr_t)data & 1) == 0) {
   1511       1.1    nonaka 		while (datalen > 1) {
   1512      1.29      matt 			HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
   1513       1.1    nonaka 			data += 2;
   1514       1.1    nonaka 			datalen -= 2;
   1515       1.1    nonaka 		}
   1516       1.1    nonaka 		if (datalen > 0) {
   1517       1.1    nonaka 			HWRITE1(hp, SDHC_DATA, *data);
   1518       1.1    nonaka 			data += 1;
   1519       1.1    nonaka 			datalen -= 1;
   1520       1.1    nonaka 		}
   1521       1.1    nonaka 	} else {
   1522       1.1    nonaka 		while (datalen > 0) {
   1523       1.1    nonaka 			HWRITE1(hp, SDHC_DATA, *data);
   1524       1.1    nonaka 			data += 1;
   1525       1.1    nonaka 			datalen -= 1;
   1526       1.1    nonaka 		}
   1527       1.1    nonaka 	}
   1528       1.1    nonaka }
   1529       1.1    nonaka 
   1530      1.11      matt static void
   1531      1.11      matt esdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1532      1.11      matt {
   1533      1.11      matt 	uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
   1534      1.12    nonaka 	uint32_t v;
   1535      1.12    nonaka 
   1536      1.23      matt 	const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_READ_SHIFT) & SDHC_WATERMARK_READ_MASK;
   1537      1.23      matt 	size_t count = 0;
   1538      1.23      matt 
   1539      1.11      matt 	while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1540      1.23      matt 		if (count == 0) {
   1541      1.23      matt 			/*
   1542      1.23      matt 			 * If we've drained "watermark" words, we need to wait
   1543      1.23      matt 			 * a little bit so the read FIFO can refill.
   1544      1.23      matt 			 */
   1545      1.23      matt 			sdmmc_delay(10);
   1546      1.23      matt 			count = watermark;
   1547      1.23      matt 		}
   1548      1.12    nonaka 		v = HREAD4(hp, SDHC_DATA);
   1549      1.11      matt 		v = le32toh(v);
   1550      1.11      matt 		*(uint32_t *)data = v;
   1551      1.11      matt 		data += 4;
   1552      1.11      matt 		datalen -= 4;
   1553      1.11      matt 		status = HREAD2(hp, SDHC_NINTR_STATUS);
   1554      1.23      matt 		count--;
   1555      1.11      matt 	}
   1556      1.11      matt 	if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1557      1.23      matt 		if (count == 0) {
   1558      1.23      matt 			sdmmc_delay(10);
   1559      1.23      matt 		}
   1560      1.12    nonaka 		v = HREAD4(hp, SDHC_DATA);
   1561      1.11      matt 		v = le32toh(v);
   1562      1.11      matt 		do {
   1563      1.11      matt 			*data++ = v;
   1564      1.11      matt 			v >>= 8;
   1565      1.11      matt 		} while (--datalen > 0);
   1566      1.11      matt 	}
   1567      1.11      matt }
   1568      1.11      matt 
   1569      1.11      matt static void
   1570      1.11      matt esdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1571      1.11      matt {
   1572      1.11      matt 	uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
   1573      1.12    nonaka 	uint32_t v;
   1574      1.12    nonaka 
   1575      1.23      matt 	const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_WRITE_SHIFT) & SDHC_WATERMARK_WRITE_MASK;
   1576      1.23      matt 	size_t count = watermark;
   1577      1.23      matt 
   1578      1.11      matt 	while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1579      1.23      matt 		if (count == 0) {
   1580      1.23      matt 			sdmmc_delay(10);
   1581      1.23      matt 			count = watermark;
   1582      1.23      matt 		}
   1583      1.12    nonaka 		v = *(uint32_t *)data;
   1584      1.11      matt 		v = htole32(v);
   1585      1.11      matt 		HWRITE4(hp, SDHC_DATA, v);
   1586      1.11      matt 		data += 4;
   1587      1.11      matt 		datalen -= 4;
   1588      1.11      matt 		status = HREAD2(hp, SDHC_NINTR_STATUS);
   1589      1.23      matt 		count--;
   1590      1.11      matt 	}
   1591      1.11      matt 	if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1592      1.23      matt 		if (count == 0) {
   1593      1.23      matt 			sdmmc_delay(10);
   1594      1.23      matt 		}
   1595      1.12    nonaka 		v = *(uint32_t *)data;
   1596      1.11      matt 		v = htole32(v);
   1597      1.11      matt 		HWRITE4(hp, SDHC_DATA, v);
   1598      1.11      matt 	}
   1599      1.11      matt }
   1600      1.11      matt 
   1601       1.1    nonaka /* Prepare for another command. */
   1602       1.1    nonaka static int
   1603       1.1    nonaka sdhc_soft_reset(struct sdhc_host *hp, int mask)
   1604       1.1    nonaka {
   1605       1.1    nonaka 	int timo;
   1606       1.1    nonaka 
   1607       1.1    nonaka 	DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask));
   1608       1.1    nonaka 
   1609      1.35  riastrad 	/* Request the reset.  */
   1610       1.1    nonaka 	HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
   1611      1.35  riastrad 
   1612      1.35  riastrad 	/*
   1613      1.35  riastrad 	 * If necessary, wait for the controller to set the bits to
   1614      1.35  riastrad 	 * acknowledge the reset.
   1615      1.35  riastrad 	 */
   1616      1.35  riastrad 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_WAIT_RESET) &&
   1617      1.35  riastrad 	    ISSET(mask, (SDHC_RESET_DAT | SDHC_RESET_CMD))) {
   1618      1.35  riastrad 		for (timo = 10000; timo > 0; timo--) {
   1619      1.35  riastrad 			if (ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
   1620      1.35  riastrad 				break;
   1621      1.35  riastrad 			/* Short delay because I worry we may miss it...  */
   1622      1.35  riastrad 			sdmmc_delay(1);
   1623      1.35  riastrad 		}
   1624      1.35  riastrad 		if (timo == 0)
   1625      1.35  riastrad 			return ETIMEDOUT;
   1626      1.35  riastrad 	}
   1627      1.35  riastrad 
   1628      1.35  riastrad 	/*
   1629      1.35  riastrad 	 * Wait for the controller to clear the bits to indicate that
   1630      1.35  riastrad 	 * the reset has completed.
   1631      1.35  riastrad 	 */
   1632       1.1    nonaka 	for (timo = 10; timo > 0; timo--) {
   1633       1.1    nonaka 		if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
   1634       1.1    nonaka 			break;
   1635       1.1    nonaka 		sdmmc_delay(10000);
   1636       1.1    nonaka 	}
   1637       1.1    nonaka 	if (timo == 0) {
   1638       1.1    nonaka 		DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp),
   1639       1.1    nonaka 		    HREAD1(hp, SDHC_SOFTWARE_RESET)));
   1640       1.1    nonaka 		return ETIMEDOUT;
   1641       1.1    nonaka 	}
   1642       1.1    nonaka 
   1643      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1644      1.11      matt 		HWRITE4(hp, SDHC_DMA_CTL, SDHC_DMA_SNOOP);
   1645      1.11      matt 	}
   1646      1.11      matt 
   1647       1.1    nonaka 	return 0;
   1648       1.1    nonaka }
   1649       1.1    nonaka 
   1650       1.1    nonaka static int
   1651       1.1    nonaka sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo)
   1652       1.1    nonaka {
   1653       1.1    nonaka 	int status;
   1654       1.1    nonaka 
   1655       1.1    nonaka 	mask |= SDHC_ERROR_INTERRUPT;
   1656       1.1    nonaka 
   1657       1.1    nonaka 	mutex_enter(&hp->intr_mtx);
   1658       1.1    nonaka 	status = hp->intr_status & mask;
   1659       1.1    nonaka 	while (status == 0) {
   1660       1.1    nonaka 		if (cv_timedwait(&hp->intr_cv, &hp->intr_mtx, timo)
   1661       1.1    nonaka 		    == EWOULDBLOCK) {
   1662       1.1    nonaka 			status |= SDHC_ERROR_INTERRUPT;
   1663       1.1    nonaka 			break;
   1664       1.1    nonaka 		}
   1665       1.1    nonaka 		status = hp->intr_status & mask;
   1666       1.1    nonaka 	}
   1667       1.1    nonaka 	hp->intr_status &= ~status;
   1668       1.1    nonaka 
   1669       1.1    nonaka 	DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status,
   1670       1.1    nonaka 	    hp->intr_error_status));
   1671  1.44.2.3    martin 
   1672       1.1    nonaka 	/* Command timeout has higher priority than command complete. */
   1673      1.11      matt 	if (ISSET(status, SDHC_ERROR_INTERRUPT) || hp->intr_error_status) {
   1674       1.1    nonaka 		hp->intr_error_status = 0;
   1675      1.11      matt 		hp->intr_status &= ~SDHC_ERROR_INTERRUPT;
   1676      1.11      matt 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1677      1.11      matt 		    (void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
   1678      1.11      matt 		}
   1679       1.1    nonaka 		status = 0;
   1680       1.1    nonaka 	}
   1681       1.1    nonaka 	mutex_exit(&hp->intr_mtx);
   1682       1.1    nonaka 
   1683       1.1    nonaka 	return status;
   1684       1.1    nonaka }
   1685       1.1    nonaka 
   1686       1.1    nonaka /*
   1687       1.1    nonaka  * Established by attachment driver at interrupt priority IPL_SDMMC.
   1688       1.1    nonaka  */
   1689       1.1    nonaka int
   1690       1.1    nonaka sdhc_intr(void *arg)
   1691       1.1    nonaka {
   1692       1.1    nonaka 	struct sdhc_softc *sc = (struct sdhc_softc *)arg;
   1693       1.1    nonaka 	struct sdhc_host *hp;
   1694       1.1    nonaka 	int done = 0;
   1695       1.1    nonaka 	uint16_t status;
   1696       1.1    nonaka 	uint16_t error;
   1697       1.1    nonaka 
   1698       1.1    nonaka 	/* We got an interrupt, but we don't know from which slot. */
   1699      1.11      matt 	for (size_t host = 0; host < sc->sc_nhosts; host++) {
   1700       1.1    nonaka 		hp = sc->sc_host[host];
   1701       1.1    nonaka 		if (hp == NULL)
   1702       1.1    nonaka 			continue;
   1703       1.1    nonaka 
   1704      1.11      matt 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   1705      1.11      matt 			/* Find out which interrupts are pending. */
   1706      1.11      matt 			uint32_t xstatus = HREAD4(hp, SDHC_NINTR_STATUS);
   1707      1.11      matt 			status = xstatus;
   1708      1.11      matt 			error = xstatus >> 16;
   1709      1.22      matt 			if (error)
   1710      1.22      matt 				xstatus |= SDHC_ERROR_INTERRUPT;
   1711      1.22      matt 			else if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   1712      1.11      matt 				continue; /* no interrupt for us */
   1713      1.11      matt 			/* Acknowledge the interrupts we are about to handle. */
   1714      1.11      matt 			HWRITE4(hp, SDHC_NINTR_STATUS, xstatus);
   1715      1.11      matt 		} else {
   1716      1.11      matt 			/* Find out which interrupts are pending. */
   1717      1.11      matt 			error = 0;
   1718      1.11      matt 			status = HREAD2(hp, SDHC_NINTR_STATUS);
   1719      1.11      matt 			if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   1720      1.11      matt 				continue; /* no interrupt for us */
   1721      1.11      matt 			/* Acknowledge the interrupts we are about to handle. */
   1722      1.11      matt 			HWRITE2(hp, SDHC_NINTR_STATUS, status);
   1723      1.11      matt 			if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
   1724      1.11      matt 				/* Acknowledge error interrupts. */
   1725      1.11      matt 				error = HREAD2(hp, SDHC_EINTR_STATUS);
   1726      1.11      matt 				HWRITE2(hp, SDHC_EINTR_STATUS, error);
   1727      1.11      matt 			}
   1728      1.11      matt 		}
   1729  1.44.2.3    martin 
   1730      1.11      matt 		DPRINTF(2,("%s: interrupt status=%x error=%x\n", HDEVNAME(hp),
   1731      1.11      matt 		    status, error));
   1732       1.1    nonaka 
   1733      1.29      matt 		mutex_enter(&hp->intr_mtx);
   1734      1.29      matt 
   1735       1.1    nonaka 		/* Claim this interrupt. */
   1736       1.1    nonaka 		done = 1;
   1737       1.1    nonaka 
   1738       1.1    nonaka 		/*
   1739       1.1    nonaka 		 * Service error interrupts.
   1740       1.1    nonaka 		 */
   1741      1.11      matt 		if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR|
   1742      1.11      matt 		    SDHC_DATA_TIMEOUT_ERROR)) {
   1743      1.11      matt 			hp->intr_error_status |= error;
   1744      1.11      matt 			hp->intr_status |= status;
   1745      1.11      matt 			cv_broadcast(&hp->intr_cv);
   1746       1.1    nonaka 		}
   1747       1.1    nonaka 
   1748       1.1    nonaka 		/*
   1749       1.1    nonaka 		 * Wake up the sdmmc event thread to scan for cards.
   1750       1.1    nonaka 		 */
   1751       1.9      matt 		if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)) {
   1752  1.44.2.3    martin 			if (hp->sdmmc != NULL) {
   1753  1.44.2.3    martin 				sdmmc_needs_discover(hp->sdmmc);
   1754  1.44.2.3    martin 			}
   1755      1.11      matt 			if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1756      1.11      matt 				HCLR4(hp, SDHC_NINTR_STATUS_EN,
   1757      1.11      matt 				    status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
   1758      1.11      matt 				HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
   1759      1.11      matt 				    status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
   1760      1.11      matt 			}
   1761       1.9      matt 		}
   1762       1.1    nonaka 
   1763       1.1    nonaka 		/*
   1764       1.1    nonaka 		 * Wake up the blocking process to service command
   1765       1.1    nonaka 		 * related interrupt(s).
   1766       1.1    nonaka 		 */
   1767      1.11      matt 		if (ISSET(status, SDHC_COMMAND_COMPLETE|
   1768      1.11      matt 		    SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY|
   1769       1.1    nonaka 		    SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) {
   1770       1.1    nonaka 			hp->intr_status |= status;
   1771      1.11      matt 			if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1772      1.11      matt 				HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
   1773      1.11      matt 				    status & (SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY));
   1774      1.11      matt 			}
   1775       1.1    nonaka 			cv_broadcast(&hp->intr_cv);
   1776       1.1    nonaka 		}
   1777       1.1    nonaka 
   1778       1.1    nonaka 		/*
   1779       1.1    nonaka 		 * Service SD card interrupts.
   1780       1.1    nonaka 		 */
   1781      1.11      matt 		if (!ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)
   1782      1.11      matt 		    && ISSET(status, SDHC_CARD_INTERRUPT)) {
   1783       1.1    nonaka 			DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp)));
   1784       1.1    nonaka 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1785       1.1    nonaka 			sdmmc_card_intr(hp->sdmmc);
   1786       1.1    nonaka 		}
   1787      1.29      matt 		mutex_exit(&hp->intr_mtx);
   1788       1.1    nonaka 	}
   1789       1.1    nonaka 
   1790       1.1    nonaka 	return done;
   1791       1.1    nonaka }
   1792       1.1    nonaka 
   1793       1.1    nonaka #ifdef SDHC_DEBUG
   1794       1.1    nonaka void
   1795       1.1    nonaka sdhc_dump_regs(struct sdhc_host *hp)
   1796       1.1    nonaka {
   1797       1.1    nonaka 
   1798       1.1    nonaka 	printf("0x%02x PRESENT_STATE:    %x\n", SDHC_PRESENT_STATE,
   1799       1.1    nonaka 	    HREAD4(hp, SDHC_PRESENT_STATE));
   1800      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
   1801      1.11      matt 		printf("0x%02x POWER_CTL:        %x\n", SDHC_POWER_CTL,
   1802      1.11      matt 		    HREAD1(hp, SDHC_POWER_CTL));
   1803       1.1    nonaka 	printf("0x%02x NINTR_STATUS:     %x\n", SDHC_NINTR_STATUS,
   1804       1.1    nonaka 	    HREAD2(hp, SDHC_NINTR_STATUS));
   1805       1.1    nonaka 	printf("0x%02x EINTR_STATUS:     %x\n", SDHC_EINTR_STATUS,
   1806       1.1    nonaka 	    HREAD2(hp, SDHC_EINTR_STATUS));
   1807       1.1    nonaka 	printf("0x%02x NINTR_STATUS_EN:  %x\n", SDHC_NINTR_STATUS_EN,
   1808       1.1    nonaka 	    HREAD2(hp, SDHC_NINTR_STATUS_EN));
   1809       1.1    nonaka 	printf("0x%02x EINTR_STATUS_EN:  %x\n", SDHC_EINTR_STATUS_EN,
   1810       1.1    nonaka 	    HREAD2(hp, SDHC_EINTR_STATUS_EN));
   1811       1.1    nonaka 	printf("0x%02x NINTR_SIGNAL_EN:  %x\n", SDHC_NINTR_SIGNAL_EN,
   1812       1.1    nonaka 	    HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
   1813       1.1    nonaka 	printf("0x%02x EINTR_SIGNAL_EN:  %x\n", SDHC_EINTR_SIGNAL_EN,
   1814       1.1    nonaka 	    HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
   1815       1.1    nonaka 	printf("0x%02x CAPABILITIES:     %x\n", SDHC_CAPABILITIES,
   1816       1.1    nonaka 	    HREAD4(hp, SDHC_CAPABILITIES));
   1817       1.1    nonaka 	printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
   1818       1.1    nonaka 	    HREAD4(hp, SDHC_MAX_CAPABILITIES));
   1819       1.1    nonaka }
   1820       1.1    nonaka #endif
   1821