Home | History | Annotate | Line # | Download | only in sdmmc
sdhc.c revision 1.51.2.3
      1  1.51.2.3     skrll /*	$NetBSD: sdhc.c,v 1.51.2.3 2015/09/22 12:06:00 skrll 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.51.2.3     skrll __KERNEL_RCSID(0, "$NetBSD: sdhc.c,v 1.51.2.3 2015/09/22 12:06:00 skrll 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/malloc.h>
     36       1.1    nonaka #include <sys/systm.h>
     37       1.1    nonaka #include <sys/mutex.h>
     38       1.1    nonaka #include <sys/condvar.h>
     39  1.51.2.3     skrll #include <sys/atomic.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.51.2.3     skrll #define SDHC_DMA_TIMEOUT	(hz*3)
     59  1.51.2.3     skrll #define SDHC_TUNING_TIMEOUT	hz
     60       1.1    nonaka 
     61       1.1    nonaka struct sdhc_host {
     62       1.1    nonaka 	struct sdhc_softc *sc;		/* host controller device */
     63       1.1    nonaka 
     64       1.1    nonaka 	bus_space_tag_t iot;		/* host register set tag */
     65       1.1    nonaka 	bus_space_handle_t ioh;		/* host register set handle */
     66      1.36  jakllsch 	bus_size_t ios;			/* host register space size */
     67       1.1    nonaka 	bus_dma_tag_t dmat;		/* host DMA tag */
     68       1.1    nonaka 
     69       1.1    nonaka 	device_t sdmmc;			/* generic SD/MMC device */
     70       1.1    nonaka 
     71       1.1    nonaka 	u_int clkbase;			/* base clock frequency in KHz */
     72       1.1    nonaka 	int maxblklen;			/* maximum block length */
     73       1.1    nonaka 	uint32_t ocr;			/* OCR value from capabilities */
     74       1.1    nonaka 
     75       1.1    nonaka 	uint8_t regs[14];		/* host controller state */
     76       1.1    nonaka 
     77       1.1    nonaka 	uint16_t intr_status;		/* soft interrupt status */
     78       1.1    nonaka 	uint16_t intr_error_status;	/* soft error status */
     79  1.51.2.3     skrll 	kmutex_t intr_lock;
     80  1.51.2.3     skrll 	kcondvar_t intr_cv;
     81  1.51.2.3     skrll 
     82  1.51.2.3     skrll 	callout_t tuning_timer;
     83  1.51.2.3     skrll 	int tuning_timing;
     84  1.51.2.3     skrll 	u_int tuning_timer_count;
     85  1.51.2.3     skrll 	u_int tuning_timer_pending;
     86       1.1    nonaka 
     87      1.12    nonaka 	int specver;			/* spec. version */
     88      1.12    nonaka 
     89       1.1    nonaka 	uint32_t flags;			/* flags for this host */
     90       1.1    nonaka #define SHF_USE_DMA		0x0001
     91       1.1    nonaka #define SHF_USE_4BIT_MODE	0x0002
     92      1.11      matt #define SHF_USE_8BIT_MODE	0x0004
     93  1.51.2.2     skrll #define SHF_MODE_DMAEN		0x0008 /* needs SDHC_DMA_ENABLE in mode */
     94  1.51.2.3     skrll #define SHF_USE_ADMA2_32	0x0010
     95  1.51.2.3     skrll #define SHF_USE_ADMA2_64	0x0020
     96  1.51.2.3     skrll #define SHF_USE_ADMA2_MASK	0x0030
     97  1.51.2.3     skrll 
     98  1.51.2.3     skrll 	bus_dmamap_t		adma_map;
     99  1.51.2.3     skrll 	bus_dma_segment_t	adma_segs[1];
    100  1.51.2.3     skrll 	void			*adma2;
    101       1.1    nonaka };
    102       1.1    nonaka 
    103       1.1    nonaka #define HDEVNAME(hp)	(device_xname((hp)->sc->sc_dev))
    104       1.1    nonaka 
    105      1.11      matt static uint8_t
    106      1.11      matt hread1(struct sdhc_host *hp, bus_size_t reg)
    107      1.11      matt {
    108      1.12    nonaka 
    109      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
    110      1.11      matt 		return bus_space_read_1(hp->iot, hp->ioh, reg);
    111      1.11      matt 	return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 3));
    112      1.11      matt }
    113      1.11      matt 
    114      1.11      matt static uint16_t
    115      1.11      matt hread2(struct sdhc_host *hp, bus_size_t reg)
    116      1.11      matt {
    117      1.12    nonaka 
    118      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
    119      1.11      matt 		return bus_space_read_2(hp->iot, hp->ioh, reg);
    120      1.11      matt 	return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 2));
    121      1.11      matt }
    122      1.11      matt 
    123      1.11      matt #define HREAD1(hp, reg)		hread1(hp, reg)
    124      1.11      matt #define HREAD2(hp, reg)		hread2(hp, reg)
    125      1.11      matt #define HREAD4(hp, reg)		\
    126       1.1    nonaka 	(bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
    127      1.11      matt 
    128      1.11      matt 
    129      1.11      matt static void
    130      1.11      matt hwrite1(struct sdhc_host *hp, bus_size_t o, uint8_t val)
    131      1.11      matt {
    132      1.12    nonaka 
    133      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    134      1.11      matt 		bus_space_write_1(hp->iot, hp->ioh, o, val);
    135      1.11      matt 	} else {
    136      1.11      matt 		const size_t shift = 8 * (o & 3);
    137      1.11      matt 		o &= -4;
    138      1.11      matt 		uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
    139      1.11      matt 		tmp = (val << shift) | (tmp & ~(0xff << shift));
    140      1.11      matt 		bus_space_write_4(hp->iot, hp->ioh, o, tmp);
    141      1.11      matt 	}
    142      1.11      matt }
    143      1.11      matt 
    144      1.11      matt static void
    145      1.11      matt hwrite2(struct sdhc_host *hp, bus_size_t o, uint16_t val)
    146      1.11      matt {
    147      1.12    nonaka 
    148      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    149      1.11      matt 		bus_space_write_2(hp->iot, hp->ioh, o, val);
    150      1.11      matt 	} else {
    151      1.11      matt 		const size_t shift = 8 * (o & 2);
    152      1.11      matt 		o &= -4;
    153      1.11      matt 		uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
    154      1.11      matt 		tmp = (val << shift) | (tmp & ~(0xffff << shift));
    155      1.11      matt 		bus_space_write_4(hp->iot, hp->ioh, o, tmp);
    156      1.11      matt 	}
    157      1.11      matt }
    158      1.11      matt 
    159      1.11      matt #define HWRITE1(hp, reg, val)		hwrite1(hp, reg, val)
    160      1.11      matt #define HWRITE2(hp, reg, val)		hwrite2(hp, reg, val)
    161       1.1    nonaka #define HWRITE4(hp, reg, val)						\
    162       1.1    nonaka 	bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val))
    163      1.11      matt 
    164       1.1    nonaka #define HCLR1(hp, reg, bits)						\
    165      1.11      matt 	do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits)); while (0)
    166       1.1    nonaka #define HCLR2(hp, reg, bits)						\
    167      1.11      matt 	do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits)); while (0)
    168      1.11      matt #define HCLR4(hp, reg, bits)						\
    169      1.11      matt 	do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) & ~(bits)); while (0)
    170       1.1    nonaka #define HSET1(hp, reg, bits)						\
    171      1.11      matt 	do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits)); while (0)
    172       1.1    nonaka #define HSET2(hp, reg, bits)						\
    173      1.11      matt 	do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits)); while (0)
    174      1.11      matt #define HSET4(hp, reg, bits)						\
    175      1.11      matt 	do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) | (bits)); while (0)
    176       1.1    nonaka 
    177       1.1    nonaka static int	sdhc_host_reset(sdmmc_chipset_handle_t);
    178       1.1    nonaka static int	sdhc_host_reset1(sdmmc_chipset_handle_t);
    179       1.1    nonaka static uint32_t	sdhc_host_ocr(sdmmc_chipset_handle_t);
    180       1.1    nonaka static int	sdhc_host_maxblklen(sdmmc_chipset_handle_t);
    181       1.1    nonaka static int	sdhc_card_detect(sdmmc_chipset_handle_t);
    182       1.1    nonaka static int	sdhc_write_protect(sdmmc_chipset_handle_t);
    183       1.1    nonaka static int	sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t);
    184  1.51.2.3     skrll static int	sdhc_bus_clock_ddr(sdmmc_chipset_handle_t, int, bool);
    185       1.1    nonaka static int	sdhc_bus_width(sdmmc_chipset_handle_t, int);
    186       1.8  kiyohara static int	sdhc_bus_rod(sdmmc_chipset_handle_t, int);
    187       1.1    nonaka static void	sdhc_card_enable_intr(sdmmc_chipset_handle_t, int);
    188       1.1    nonaka static void	sdhc_card_intr_ack(sdmmc_chipset_handle_t);
    189       1.1    nonaka static void	sdhc_exec_command(sdmmc_chipset_handle_t,
    190       1.1    nonaka 		    struct sdmmc_command *);
    191  1.51.2.3     skrll static int	sdhc_signal_voltage(sdmmc_chipset_handle_t, int);
    192  1.51.2.3     skrll static int	sdhc_execute_tuning1(struct sdhc_host *, int);
    193  1.51.2.3     skrll static int	sdhc_execute_tuning(sdmmc_chipset_handle_t, int);
    194  1.51.2.3     skrll static void	sdhc_tuning_timer(void *);
    195       1.1    nonaka static int	sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
    196       1.1    nonaka static int	sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t);
    197       1.1    nonaka static int	sdhc_soft_reset(struct sdhc_host *, int);
    198       1.1    nonaka static int	sdhc_wait_intr(struct sdhc_host *, int, int);
    199       1.1    nonaka static void	sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
    200       1.7    nonaka static int	sdhc_transfer_data_dma(struct sdhc_host *, struct sdmmc_command *);
    201       1.1    nonaka static int	sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *);
    202      1.11      matt static void	sdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
    203      1.11      matt static void	sdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
    204      1.11      matt static void	esdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
    205      1.11      matt static void	esdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
    206      1.11      matt 
    207       1.1    nonaka static struct sdmmc_chip_functions sdhc_functions = {
    208       1.1    nonaka 	/* host controller reset */
    209  1.51.2.3     skrll 	.host_reset = sdhc_host_reset,
    210       1.1    nonaka 
    211       1.1    nonaka 	/* host controller capabilities */
    212  1.51.2.3     skrll 	.host_ocr = sdhc_host_ocr,
    213  1.51.2.3     skrll 	.host_maxblklen = sdhc_host_maxblklen,
    214       1.1    nonaka 
    215       1.1    nonaka 	/* card detection */
    216  1.51.2.3     skrll 	.card_detect = sdhc_card_detect,
    217       1.1    nonaka 
    218       1.1    nonaka 	/* write protect */
    219  1.51.2.3     skrll 	.write_protect = sdhc_write_protect,
    220       1.1    nonaka 
    221  1.51.2.3     skrll 	/* bus power, clock frequency, width and ROD(OpenDrain/PushPull) */
    222  1.51.2.3     skrll 	.bus_power = sdhc_bus_power,
    223  1.51.2.3     skrll 	.bus_clock = NULL,	/* see sdhc_bus_clock_ddr */
    224  1.51.2.3     skrll 	.bus_width = sdhc_bus_width,
    225  1.51.2.3     skrll 	.bus_rod = sdhc_bus_rod,
    226       1.1    nonaka 
    227       1.1    nonaka 	/* command execution */
    228  1.51.2.3     skrll 	.exec_command = sdhc_exec_command,
    229       1.1    nonaka 
    230       1.1    nonaka 	/* card interrupt */
    231  1.51.2.3     skrll 	.card_enable_intr = sdhc_card_enable_intr,
    232  1.51.2.3     skrll 	.card_intr_ack = sdhc_card_intr_ack,
    233  1.51.2.3     skrll 
    234  1.51.2.3     skrll 	/* UHS functions */
    235  1.51.2.3     skrll 	.signal_voltage = sdhc_signal_voltage,
    236  1.51.2.3     skrll 	.bus_clock_ddr = sdhc_bus_clock_ddr,
    237  1.51.2.3     skrll 	.execute_tuning = sdhc_execute_tuning,
    238       1.1    nonaka };
    239       1.1    nonaka 
    240      1.17  jakllsch static int
    241      1.17  jakllsch sdhc_cfprint(void *aux, const char *pnp)
    242      1.17  jakllsch {
    243      1.31     joerg 	const struct sdmmcbus_attach_args * const saa = aux;
    244      1.17  jakllsch 	const struct sdhc_host * const hp = saa->saa_sch;
    245      1.47     skrll 
    246      1.17  jakllsch 	if (pnp) {
    247      1.17  jakllsch 		aprint_normal("sdmmc at %s", pnp);
    248      1.17  jakllsch 	}
    249      1.41  jakllsch 	for (size_t host = 0; host < hp->sc->sc_nhosts; host++) {
    250      1.41  jakllsch 		if (hp->sc->sc_host[host] == hp) {
    251      1.41  jakllsch 			aprint_normal(" slot %zu", host);
    252      1.41  jakllsch 		}
    253      1.41  jakllsch 	}
    254      1.17  jakllsch 
    255      1.17  jakllsch 	return UNCONF;
    256      1.17  jakllsch }
    257      1.17  jakllsch 
    258       1.1    nonaka /*
    259       1.1    nonaka  * Called by attachment driver.  For each SD card slot there is one SD
    260       1.1    nonaka  * host controller standard register set. (1.3)
    261       1.1    nonaka  */
    262       1.1    nonaka int
    263       1.1    nonaka sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
    264       1.1    nonaka     bus_space_handle_t ioh, bus_size_t iosize)
    265       1.1    nonaka {
    266       1.1    nonaka 	struct sdmmcbus_attach_args saa;
    267       1.1    nonaka 	struct sdhc_host *hp;
    268  1.51.2.3     skrll 	uint32_t caps, caps2;
    269       1.1    nonaka 	uint16_t sdhcver;
    270  1.51.2.3     skrll 	int error;
    271       1.1    nonaka 
    272      1.33  riastrad 	/* Allocate one more host structure. */
    273      1.33  riastrad 	hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO);
    274      1.33  riastrad 	if (hp == NULL) {
    275      1.33  riastrad 		aprint_error_dev(sc->sc_dev,
    276      1.33  riastrad 		    "couldn't alloc memory (sdhc host)\n");
    277      1.33  riastrad 		goto err1;
    278      1.33  riastrad 	}
    279      1.33  riastrad 	sc->sc_host[sc->sc_nhosts++] = hp;
    280      1.33  riastrad 
    281      1.33  riastrad 	/* Fill in the new host structure. */
    282      1.33  riastrad 	hp->sc = sc;
    283      1.33  riastrad 	hp->iot = iot;
    284      1.33  riastrad 	hp->ioh = ioh;
    285      1.36  jakllsch 	hp->ios = iosize;
    286      1.33  riastrad 	hp->dmat = sc->sc_dmat;
    287      1.33  riastrad 
    288  1.51.2.3     skrll 	mutex_init(&hp->intr_lock, MUTEX_DEFAULT, IPL_SDMMC);
    289      1.33  riastrad 	cv_init(&hp->intr_cv, "sdhcintr");
    290  1.51.2.3     skrll 	callout_init(&hp->tuning_timer, CALLOUT_MPSAFE);
    291  1.51.2.3     skrll 	callout_setfunc(&hp->tuning_timer, sdhc_tuning_timer, hp);
    292      1.33  riastrad 
    293  1.51.2.1     skrll 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    294  1.51.2.1     skrll 		sdhcver = HREAD4(hp, SDHC_ESDHC_HOST_CTL_VERSION);
    295  1.51.2.1     skrll 	} else {
    296  1.51.2.1     skrll 		sdhcver = HREAD2(hp, SDHC_HOST_CTL_VERSION);
    297  1.51.2.1     skrll 	}
    298  1.51.2.2     skrll 	aprint_normal_dev(sc->sc_dev, "SDHC ");
    299      1.33  riastrad 	hp->specver = SDHC_SPEC_VERSION(sdhcver);
    300       1.1    nonaka 	switch (SDHC_SPEC_VERSION(sdhcver)) {
    301      1.12    nonaka 	case SDHC_SPEC_VERS_100:
    302      1.12    nonaka 		aprint_normal("1.0");
    303      1.12    nonaka 		break;
    304      1.12    nonaka 
    305      1.12    nonaka 	case SDHC_SPEC_VERS_200:
    306      1.12    nonaka 		aprint_normal("2.0");
    307       1.1    nonaka 		break;
    308       1.1    nonaka 
    309      1.12    nonaka 	case SDHC_SPEC_VERS_300:
    310      1.12    nonaka 		aprint_normal("3.0");
    311       1.9      matt 		break;
    312       1.9      matt 
    313  1.51.2.2     skrll 	case SDHC_SPEC_VERS_400:
    314  1.51.2.2     skrll 		aprint_normal("4.0");
    315  1.51.2.2     skrll 		break;
    316  1.51.2.2     skrll 
    317       1.1    nonaka 	default:
    318      1.12    nonaka 		aprint_normal("unknown version(0x%x)",
    319      1.12    nonaka 		    SDHC_SPEC_VERSION(sdhcver));
    320       1.1    nonaka 		break;
    321       1.1    nonaka 	}
    322  1.51.2.2     skrll 	aprint_normal(", rev %u", SDHC_VENDOR_VERSION(sdhcver));
    323       1.1    nonaka 
    324       1.1    nonaka 	/*
    325       1.3  uebayasi 	 * Reset the host controller and enable interrupts.
    326       1.1    nonaka 	 */
    327       1.1    nonaka 	(void)sdhc_host_reset(hp);
    328       1.1    nonaka 
    329       1.1    nonaka 	/* Determine host capabilities. */
    330      1.24     skrll 	if (ISSET(sc->sc_flags, SDHC_FLAG_HOSTCAPS)) {
    331      1.24     skrll 		caps = sc->sc_caps;
    332  1.51.2.3     skrll 		caps2 = sc->sc_caps2;
    333      1.24     skrll 	} else {
    334  1.51.2.3     skrll 		caps = sc->sc_caps = HREAD4(hp, SDHC_CAPABILITIES);
    335  1.51.2.3     skrll 		if (hp->specver >= SDHC_SPEC_VERS_300) {
    336  1.51.2.3     skrll 			caps2 = sc->sc_caps2 = HREAD4(hp, SDHC_CAPABILITIES2);
    337  1.51.2.3     skrll 		} else {
    338  1.51.2.3     skrll 			caps2 = sc->sc_caps2 = 0;
    339  1.51.2.3     skrll 		}
    340  1.51.2.3     skrll 	}
    341  1.51.2.3     skrll 
    342  1.51.2.3     skrll 	const u_int retuning_mode = (caps2 >> SDHC_RETUNING_MODES_SHIFT) &
    343  1.51.2.3     skrll 	    SDHC_RETUNING_MODES_MASK;
    344  1.51.2.3     skrll 	if (retuning_mode == SDHC_RETUNING_MODE_1) {
    345  1.51.2.3     skrll 		hp->tuning_timer_count = (caps2 >> SDHC_TIMER_COUNT_SHIFT) &
    346  1.51.2.3     skrll 		    SDHC_TIMER_COUNT_MASK;
    347  1.51.2.3     skrll 		if (hp->tuning_timer_count == 0xf)
    348  1.51.2.3     skrll 			hp->tuning_timer_count = 0;
    349  1.51.2.3     skrll 		if (hp->tuning_timer_count)
    350  1.51.2.3     skrll 			hp->tuning_timer_count =
    351  1.51.2.3     skrll 			    1 << (hp->tuning_timer_count - 1);
    352      1.24     skrll 	}
    353       1.1    nonaka 
    354  1.51.2.2     skrll 	/*
    355  1.51.2.2     skrll 	 * Use DMA if the host system and the controller support it.
    356  1.51.2.2     skrll 	 * Suports integrated or external DMA egine, with or without
    357  1.51.2.2     skrll 	 * SDHC_DMA_ENABLE in the command.
    358  1.51.2.2     skrll 	 */
    359      1.28      matt 	if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA) ||
    360      1.27  jakllsch 	    (ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA &&
    361      1.28      matt 	     ISSET(caps, SDHC_DMA_SUPPORT)))) {
    362       1.1    nonaka 		SET(hp->flags, SHF_USE_DMA);
    363  1.51.2.2     skrll 
    364  1.51.2.3     skrll 		if (ISSET(sc->sc_flags, SDHC_FLAG_USE_ADMA2) &&
    365  1.51.2.3     skrll 		    ISSET(caps, SDHC_ADMA2_SUPP)) {
    366  1.51.2.3     skrll 			SET(hp->flags, SHF_MODE_DMAEN);
    367  1.51.2.3     skrll 			/*
    368  1.51.2.3     skrll 			 * 64-bit mode was present in the 2.00 spec, removed
    369  1.51.2.3     skrll 			 * from 3.00, and re-added in 4.00 with a different
    370  1.51.2.3     skrll 			 * descriptor layout. We only support 2.00 and 3.00
    371  1.51.2.3     skrll 			 * descriptors for now.
    372  1.51.2.3     skrll 			 */
    373  1.51.2.3     skrll 			if (hp->specver == SDHC_SPEC_VERS_200 &&
    374  1.51.2.3     skrll 			    ISSET(caps, SDHC_64BIT_SYS_BUS)) {
    375  1.51.2.3     skrll 				SET(hp->flags, SHF_USE_ADMA2_64);
    376  1.51.2.3     skrll 				aprint_normal(", 64-bit ADMA2");
    377  1.51.2.3     skrll 			} else {
    378  1.51.2.3     skrll 				SET(hp->flags, SHF_USE_ADMA2_32);
    379  1.51.2.3     skrll 				aprint_normal(", 32-bit ADMA2");
    380  1.51.2.3     skrll 			}
    381  1.51.2.3     skrll 		} else {
    382  1.51.2.3     skrll 			if (!ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA) ||
    383  1.51.2.3     skrll 			    ISSET(sc->sc_flags, SDHC_FLAG_EXTDMA_DMAEN))
    384  1.51.2.3     skrll 				SET(hp->flags, SHF_MODE_DMAEN);
    385  1.51.2.3     skrll 			if (sc->sc_vendor_transfer_data_dma) {
    386  1.51.2.3     skrll 				aprint_normal(", platform DMA");
    387  1.51.2.3     skrll 			} else {
    388  1.51.2.3     skrll 				aprint_normal(", SDMA");
    389  1.51.2.3     skrll 			}
    390  1.51.2.3     skrll 		}
    391  1.51.2.2     skrll 	} else {
    392  1.51.2.2     skrll 		aprint_normal(", PIO");
    393       1.1    nonaka 	}
    394       1.1    nonaka 
    395       1.1    nonaka 	/*
    396       1.1    nonaka 	 * Determine the base clock frequency. (2.2.24)
    397       1.1    nonaka 	 */
    398  1.51.2.2     skrll 	if (hp->specver >= SDHC_SPEC_VERS_300) {
    399      1.30      matt 		hp->clkbase = SDHC_BASE_V3_FREQ_KHZ(caps);
    400      1.30      matt 	} else {
    401      1.30      matt 		hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
    402      1.30      matt 	}
    403  1.51.2.2     skrll 	if (hp->clkbase == 0 ||
    404  1.51.2.2     skrll 	    ISSET(sc->sc_flags, SDHC_FLAG_NO_CLKBASE)) {
    405       1.9      matt 		if (sc->sc_clkbase == 0) {
    406       1.9      matt 			/* The attachment driver must tell us. */
    407      1.12    nonaka 			aprint_error_dev(sc->sc_dev,
    408      1.12    nonaka 			    "unknown base clock frequency\n");
    409       1.9      matt 			goto err;
    410       1.9      matt 		}
    411       1.9      matt 		hp->clkbase = sc->sc_clkbase;
    412       1.9      matt 	}
    413       1.9      matt 	if (hp->clkbase < 10000 || hp->clkbase > 10000 * 256) {
    414       1.1    nonaka 		/* SDHC 1.0 supports only 10-63 MHz. */
    415       1.1    nonaka 		aprint_error_dev(sc->sc_dev,
    416       1.1    nonaka 		    "base clock frequency out of range: %u MHz\n",
    417       1.1    nonaka 		    hp->clkbase / 1000);
    418       1.1    nonaka 		goto err;
    419       1.1    nonaka 	}
    420  1.51.2.2     skrll 	aprint_normal(", %u kHz", hp->clkbase);
    421       1.1    nonaka 
    422       1.1    nonaka 	/*
    423       1.1    nonaka 	 * XXX Set the data timeout counter value according to
    424       1.1    nonaka 	 * capabilities. (2.2.15)
    425       1.1    nonaka 	 */
    426       1.1    nonaka 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    427      1.29      matt #if 1
    428      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    429      1.11      matt 		HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
    430      1.11      matt #endif
    431       1.1    nonaka 
    432  1.51.2.2     skrll 	if (ISSET(caps, SDHC_EMBEDDED_SLOT))
    433  1.51.2.2     skrll 		aprint_normal(", embedded slot");
    434  1.51.2.2     skrll 
    435       1.1    nonaka 	/*
    436       1.1    nonaka 	 * Determine SD bus voltage levels supported by the controller.
    437       1.1    nonaka 	 */
    438  1.51.2.2     skrll 	aprint_normal(",");
    439  1.51.2.3     skrll 	if (ISSET(caps, SDHC_HIGH_SPEED_SUPP)) {
    440  1.51.2.3     skrll 		SET(hp->ocr, MMC_OCR_HCS);
    441  1.51.2.3     skrll 		aprint_normal(" HS");
    442  1.51.2.3     skrll 	}
    443  1.51.2.3     skrll 	if (ISSET(caps2, SDHC_SDR50_SUPP)) {
    444  1.51.2.3     skrll 		SET(hp->ocr, MMC_OCR_S18A);
    445  1.51.2.3     skrll 		aprint_normal(" SDR50");
    446  1.51.2.3     skrll 	}
    447  1.51.2.3     skrll 	if (ISSET(caps2, SDHC_DDR50_SUPP)) {
    448  1.51.2.3     skrll 		SET(hp->ocr, MMC_OCR_S18A);
    449  1.51.2.3     skrll 		aprint_normal(" DDR50");
    450  1.51.2.3     skrll 	}
    451  1.51.2.3     skrll 	if (ISSET(caps2, SDHC_SDR104_SUPP)) {
    452  1.51.2.3     skrll 		SET(hp->ocr, MMC_OCR_S18A);
    453  1.51.2.3     skrll 		aprint_normal(" SDR104 HS200");
    454  1.51.2.3     skrll 	}
    455  1.51.2.3     skrll 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V)) {
    456       1.1    nonaka 		SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V);
    457  1.51.2.2     skrll 		aprint_normal(" 1.8V");
    458      1.11      matt 	}
    459      1.11      matt 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V)) {
    460       1.1    nonaka 		SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
    461  1.51.2.2     skrll 		aprint_normal(" 3.0V");
    462      1.11      matt 	}
    463      1.11      matt 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V)) {
    464       1.1    nonaka 		SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
    465  1.51.2.2     skrll 		aprint_normal(" 3.3V");
    466      1.11      matt 	}
    467  1.51.2.3     skrll 	if (hp->specver >= SDHC_SPEC_VERS_300) {
    468  1.51.2.3     skrll 		aprint_normal(", re-tuning mode %d", retuning_mode + 1);
    469  1.51.2.3     skrll 		if (hp->tuning_timer_count)
    470  1.51.2.3     skrll 			aprint_normal(" (%us timer)", hp->tuning_timer_count);
    471  1.51.2.3     skrll 	}
    472       1.1    nonaka 
    473       1.1    nonaka 	/*
    474       1.1    nonaka 	 * Determine the maximum block length supported by the host
    475       1.1    nonaka 	 * controller. (2.2.24)
    476       1.1    nonaka 	 */
    477       1.1    nonaka 	switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
    478       1.1    nonaka 	case SDHC_MAX_BLK_LEN_512:
    479       1.1    nonaka 		hp->maxblklen = 512;
    480       1.1    nonaka 		break;
    481       1.1    nonaka 
    482       1.1    nonaka 	case SDHC_MAX_BLK_LEN_1024:
    483       1.1    nonaka 		hp->maxblklen = 1024;
    484       1.1    nonaka 		break;
    485       1.1    nonaka 
    486       1.1    nonaka 	case SDHC_MAX_BLK_LEN_2048:
    487       1.1    nonaka 		hp->maxblklen = 2048;
    488       1.1    nonaka 		break;
    489       1.1    nonaka 
    490       1.9      matt 	case SDHC_MAX_BLK_LEN_4096:
    491       1.9      matt 		hp->maxblklen = 4096;
    492       1.9      matt 		break;
    493       1.9      matt 
    494       1.1    nonaka 	default:
    495       1.1    nonaka 		aprint_error_dev(sc->sc_dev, "max block length unknown\n");
    496       1.1    nonaka 		goto err;
    497       1.1    nonaka 	}
    498  1.51.2.2     skrll 	aprint_normal(", %u byte blocks", hp->maxblklen);
    499  1.51.2.2     skrll 	aprint_normal("\n");
    500       1.1    nonaka 
    501  1.51.2.3     skrll 	if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
    502  1.51.2.3     skrll 		int rseg;
    503  1.51.2.3     skrll 
    504  1.51.2.3     skrll 		/* Allocate ADMA2 descriptor memory */
    505  1.51.2.3     skrll 		error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
    506  1.51.2.3     skrll 		    PAGE_SIZE, hp->adma_segs, 1, &rseg, BUS_DMA_WAITOK);
    507  1.51.2.3     skrll 		if (error) {
    508  1.51.2.3     skrll 			aprint_error_dev(sc->sc_dev,
    509  1.51.2.3     skrll 			    "ADMA2 dmamem_alloc failed (%d)\n", error);
    510  1.51.2.3     skrll 			goto adma_done;
    511  1.51.2.3     skrll 		}
    512  1.51.2.3     skrll 		error = bus_dmamem_map(sc->sc_dmat, hp->adma_segs, rseg,
    513  1.51.2.3     skrll 		    PAGE_SIZE, (void **)&hp->adma2, BUS_DMA_WAITOK);
    514  1.51.2.3     skrll 		if (error) {
    515  1.51.2.3     skrll 			aprint_error_dev(sc->sc_dev,
    516  1.51.2.3     skrll 			    "ADMA2 dmamem_map failed (%d)\n", error);
    517  1.51.2.3     skrll 			goto adma_done;
    518  1.51.2.3     skrll 		}
    519  1.51.2.3     skrll 		error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
    520  1.51.2.3     skrll 		    0, BUS_DMA_WAITOK, &hp->adma_map);
    521  1.51.2.3     skrll 		if (error) {
    522  1.51.2.3     skrll 			aprint_error_dev(sc->sc_dev,
    523  1.51.2.3     skrll 			    "ADMA2 dmamap_create failed (%d)\n", error);
    524  1.51.2.3     skrll 			goto adma_done;
    525  1.51.2.3     skrll 		}
    526  1.51.2.3     skrll 		error = bus_dmamap_load(sc->sc_dmat, hp->adma_map,
    527  1.51.2.3     skrll 		    hp->adma2, PAGE_SIZE, NULL,
    528  1.51.2.3     skrll 		    BUS_DMA_WAITOK|BUS_DMA_WRITE);
    529  1.51.2.3     skrll 		if (error) {
    530  1.51.2.3     skrll 			aprint_error_dev(sc->sc_dev,
    531  1.51.2.3     skrll 			    "ADMA2 dmamap_load failed (%d)\n", error);
    532  1.51.2.3     skrll 			goto adma_done;
    533  1.51.2.3     skrll 		}
    534  1.51.2.3     skrll 
    535  1.51.2.3     skrll 		memset(hp->adma2, 0, PAGE_SIZE);
    536  1.51.2.3     skrll 
    537  1.51.2.3     skrll adma_done:
    538  1.51.2.3     skrll 		if (error)
    539  1.51.2.3     skrll 			CLR(hp->flags, SHF_USE_ADMA2_MASK);
    540  1.51.2.3     skrll 	}
    541  1.51.2.3     skrll 
    542       1.1    nonaka 	/*
    543       1.1    nonaka 	 * Attach the generic SD/MMC bus driver.  (The bus driver must
    544       1.1    nonaka 	 * not invoke any chipset functions before it is attached.)
    545       1.1    nonaka 	 */
    546       1.1    nonaka 	memset(&saa, 0, sizeof(saa));
    547       1.1    nonaka 	saa.saa_busname = "sdmmc";
    548       1.1    nonaka 	saa.saa_sct = &sdhc_functions;
    549       1.1    nonaka 	saa.saa_sch = hp;
    550       1.1    nonaka 	saa.saa_dmat = hp->dmat;
    551       1.1    nonaka 	saa.saa_clkmax = hp->clkbase;
    552      1.11      matt 	if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_CGM))
    553      1.38  jakllsch 		saa.saa_clkmin = hp->clkbase / 256 / 2046;
    554      1.11      matt 	else if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_DVS))
    555      1.38  jakllsch 		saa.saa_clkmin = hp->clkbase / 256 / 16;
    556      1.38  jakllsch 	else if (hp->sc->sc_clkmsk != 0)
    557      1.38  jakllsch 		saa.saa_clkmin = hp->clkbase / (hp->sc->sc_clkmsk >>
    558      1.38  jakllsch 		    (ffs(hp->sc->sc_clkmsk) - 1));
    559  1.51.2.2     skrll 	else if (hp->specver >= SDHC_SPEC_VERS_300)
    560      1.38  jakllsch 		saa.saa_clkmin = hp->clkbase / 0x3ff;
    561      1.38  jakllsch 	else
    562      1.38  jakllsch 		saa.saa_clkmin = hp->clkbase / 256;
    563       1.1    nonaka 	saa.saa_caps = SMC_CAPS_4BIT_MODE|SMC_CAPS_AUTO_STOP;
    564      1.11      matt 	if (ISSET(sc->sc_flags, SDHC_FLAG_8BIT_MODE))
    565      1.11      matt 		saa.saa_caps |= SMC_CAPS_8BIT_MODE;
    566      1.11      matt 	if (ISSET(caps, SDHC_HIGH_SPEED_SUPP))
    567      1.11      matt 		saa.saa_caps |= SMC_CAPS_SD_HIGHSPEED;
    568  1.51.2.3     skrll 	if (ISSET(caps2, SDHC_SDR104_SUPP))
    569  1.51.2.3     skrll 		saa.saa_caps |= SMC_CAPS_UHS_SDR104 |
    570  1.51.2.3     skrll 				SMC_CAPS_UHS_SDR50 |
    571  1.51.2.3     skrll 				SMC_CAPS_MMC_HS200;
    572  1.51.2.3     skrll 	if (ISSET(caps2, SDHC_SDR50_SUPP))
    573  1.51.2.3     skrll 		saa.saa_caps |= SMC_CAPS_UHS_SDR50;
    574  1.51.2.3     skrll 	if (ISSET(caps2, SDHC_DDR50_SUPP))
    575  1.51.2.3     skrll 		saa.saa_caps |= SMC_CAPS_UHS_DDR50;
    576      1.26      matt 	if (ISSET(hp->flags, SHF_USE_DMA)) {
    577  1.51.2.1     skrll 		saa.saa_caps |= SMC_CAPS_DMA;
    578  1.51.2.1     skrll 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    579  1.51.2.1     skrll 			saa.saa_caps |= SMC_CAPS_MULTI_SEG_DMA;
    580      1.26      matt 	}
    581      1.32  kiyohara 	if (ISSET(sc->sc_flags, SDHC_FLAG_SINGLE_ONLY))
    582      1.32  kiyohara 		saa.saa_caps |= SMC_CAPS_SINGLE_ONLY;
    583  1.51.2.3     skrll 	if (ISSET(sc->sc_flags, SDHC_FLAG_POLL_CARD_DET))
    584  1.51.2.3     skrll 		saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
    585      1.17  jakllsch 	hp->sdmmc = config_found(sc->sc_dev, &saa, sdhc_cfprint);
    586       1.1    nonaka 
    587       1.1    nonaka 	return 0;
    588       1.1    nonaka 
    589       1.1    nonaka err:
    590  1.51.2.3     skrll 	callout_destroy(&hp->tuning_timer);
    591       1.1    nonaka 	cv_destroy(&hp->intr_cv);
    592  1.51.2.3     skrll 	mutex_destroy(&hp->intr_lock);
    593       1.1    nonaka 	free(hp, M_DEVBUF);
    594       1.1    nonaka 	sc->sc_host[--sc->sc_nhosts] = NULL;
    595       1.1    nonaka err1:
    596       1.1    nonaka 	return 1;
    597       1.1    nonaka }
    598       1.1    nonaka 
    599       1.7    nonaka int
    600      1.36  jakllsch sdhc_detach(struct sdhc_softc *sc, int flags)
    601       1.7    nonaka {
    602      1.36  jakllsch 	struct sdhc_host *hp;
    603       1.7    nonaka 	int rv = 0;
    604       1.7    nonaka 
    605      1.36  jakllsch 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    606      1.36  jakllsch 		hp = sc->sc_host[n];
    607      1.36  jakllsch 		if (hp == NULL)
    608      1.36  jakllsch 			continue;
    609      1.36  jakllsch 		if (hp->sdmmc != NULL) {
    610      1.36  jakllsch 			rv = config_detach(hp->sdmmc, flags);
    611      1.36  jakllsch 			if (rv)
    612      1.36  jakllsch 				break;
    613      1.36  jakllsch 			hp->sdmmc = NULL;
    614      1.36  jakllsch 		}
    615      1.36  jakllsch 		/* disable interrupts */
    616      1.36  jakllsch 		if ((flags & DETACH_FORCE) == 0) {
    617  1.51.2.3     skrll 			mutex_enter(&hp->intr_lock);
    618      1.36  jakllsch 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    619      1.36  jakllsch 				HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
    620      1.36  jakllsch 			} else {
    621      1.36  jakllsch 				HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
    622      1.36  jakllsch 			}
    623      1.36  jakllsch 			sdhc_soft_reset(hp, SDHC_RESET_ALL);
    624  1.51.2.3     skrll 			mutex_exit(&hp->intr_lock);
    625      1.36  jakllsch 		}
    626  1.51.2.3     skrll 		callout_halt(&hp->tuning_timer, NULL);
    627  1.51.2.3     skrll 		callout_destroy(&hp->tuning_timer);
    628      1.36  jakllsch 		cv_destroy(&hp->intr_cv);
    629  1.51.2.3     skrll 		mutex_destroy(&hp->intr_lock);
    630      1.36  jakllsch 		if (hp->ios > 0) {
    631      1.36  jakllsch 			bus_space_unmap(hp->iot, hp->ioh, hp->ios);
    632      1.36  jakllsch 			hp->ios = 0;
    633      1.36  jakllsch 		}
    634  1.51.2.3     skrll 		if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
    635  1.51.2.3     skrll 			bus_dmamap_unload(sc->sc_dmat, hp->adma_map);
    636  1.51.2.3     skrll 			bus_dmamap_destroy(sc->sc_dmat, hp->adma_map);
    637  1.51.2.3     skrll 			bus_dmamem_unmap(sc->sc_dmat, hp->adma2, PAGE_SIZE);
    638  1.51.2.3     skrll 			bus_dmamem_free(sc->sc_dmat, hp->adma_segs, 1);
    639  1.51.2.3     skrll 		}
    640      1.36  jakllsch 		free(hp, M_DEVBUF);
    641      1.36  jakllsch 		sc->sc_host[n] = NULL;
    642      1.36  jakllsch 	}
    643       1.7    nonaka 
    644       1.7    nonaka 	return rv;
    645       1.7    nonaka }
    646       1.7    nonaka 
    647       1.1    nonaka bool
    648       1.6    dyoung sdhc_suspend(device_t dev, const pmf_qual_t *qual)
    649       1.1    nonaka {
    650       1.1    nonaka 	struct sdhc_softc *sc = device_private(dev);
    651       1.1    nonaka 	struct sdhc_host *hp;
    652      1.12    nonaka 	size_t i;
    653       1.1    nonaka 
    654       1.1    nonaka 	/* XXX poll for command completion or suspend command
    655       1.1    nonaka 	 * in progress */
    656       1.1    nonaka 
    657       1.1    nonaka 	/* Save the host controller state. */
    658      1.11      matt 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    659       1.1    nonaka 		hp = sc->sc_host[n];
    660      1.11      matt 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    661      1.12    nonaka 			for (i = 0; i < sizeof hp->regs; i += 4) {
    662      1.11      matt 				uint32_t v = HREAD4(hp, i);
    663      1.12    nonaka 				hp->regs[i + 0] = (v >> 0);
    664      1.12    nonaka 				hp->regs[i + 1] = (v >> 8);
    665      1.13    bouyer 				if (i + 3 < sizeof hp->regs) {
    666      1.13    bouyer 					hp->regs[i + 2] = (v >> 16);
    667      1.13    bouyer 					hp->regs[i + 3] = (v >> 24);
    668      1.13    bouyer 				}
    669      1.11      matt 			}
    670      1.11      matt 		} else {
    671      1.12    nonaka 			for (i = 0; i < sizeof hp->regs; i++) {
    672      1.11      matt 				hp->regs[i] = HREAD1(hp, i);
    673      1.11      matt 			}
    674      1.11      matt 		}
    675       1.1    nonaka 	}
    676       1.1    nonaka 	return true;
    677       1.1    nonaka }
    678       1.1    nonaka 
    679       1.1    nonaka bool
    680       1.6    dyoung sdhc_resume(device_t dev, const pmf_qual_t *qual)
    681       1.1    nonaka {
    682       1.1    nonaka 	struct sdhc_softc *sc = device_private(dev);
    683       1.1    nonaka 	struct sdhc_host *hp;
    684      1.12    nonaka 	size_t i;
    685       1.1    nonaka 
    686       1.1    nonaka 	/* Restore the host controller state. */
    687      1.11      matt 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    688       1.1    nonaka 		hp = sc->sc_host[n];
    689       1.1    nonaka 		(void)sdhc_host_reset(hp);
    690      1.11      matt 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    691      1.12    nonaka 			for (i = 0; i < sizeof hp->regs; i += 4) {
    692      1.13    bouyer 				if (i + 3 < sizeof hp->regs) {
    693      1.13    bouyer 					HWRITE4(hp, i,
    694      1.13    bouyer 					    (hp->regs[i + 0] << 0)
    695      1.13    bouyer 					    | (hp->regs[i + 1] << 8)
    696      1.13    bouyer 					    | (hp->regs[i + 2] << 16)
    697      1.13    bouyer 					    | (hp->regs[i + 3] << 24));
    698      1.13    bouyer 				} else {
    699      1.13    bouyer 					HWRITE4(hp, i,
    700      1.13    bouyer 					    (hp->regs[i + 0] << 0)
    701      1.13    bouyer 					    | (hp->regs[i + 1] << 8));
    702      1.13    bouyer 				}
    703      1.11      matt 			}
    704      1.11      matt 		} else {
    705      1.12    nonaka 			for (i = 0; i < sizeof hp->regs; i++) {
    706      1.11      matt 				HWRITE1(hp, i, hp->regs[i]);
    707      1.11      matt 			}
    708      1.11      matt 		}
    709       1.1    nonaka 	}
    710       1.1    nonaka 	return true;
    711       1.1    nonaka }
    712       1.1    nonaka 
    713       1.1    nonaka bool
    714       1.1    nonaka sdhc_shutdown(device_t dev, int flags)
    715       1.1    nonaka {
    716       1.1    nonaka 	struct sdhc_softc *sc = device_private(dev);
    717       1.1    nonaka 	struct sdhc_host *hp;
    718       1.1    nonaka 
    719       1.1    nonaka 	/* XXX chip locks up if we don't disable it before reboot. */
    720      1.11      matt 	for (size_t i = 0; i < sc->sc_nhosts; i++) {
    721       1.1    nonaka 		hp = sc->sc_host[i];
    722       1.1    nonaka 		(void)sdhc_host_reset(hp);
    723       1.1    nonaka 	}
    724       1.1    nonaka 	return true;
    725       1.1    nonaka }
    726       1.1    nonaka 
    727       1.1    nonaka /*
    728       1.1    nonaka  * Reset the host controller.  Called during initialization, when
    729       1.1    nonaka  * cards are removed, upon resume, and during error recovery.
    730       1.1    nonaka  */
    731       1.1    nonaka static int
    732       1.1    nonaka sdhc_host_reset1(sdmmc_chipset_handle_t sch)
    733       1.1    nonaka {
    734       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    735      1.11      matt 	uint32_t sdhcimask;
    736       1.1    nonaka 	int error;
    737       1.1    nonaka 
    738  1.51.2.3     skrll 	KASSERT(mutex_owned(&hp->intr_lock));
    739       1.1    nonaka 
    740       1.1    nonaka 	/* Disable all interrupts. */
    741      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    742      1.11      matt 		HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
    743      1.11      matt 	} else {
    744      1.11      matt 		HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
    745      1.11      matt 	}
    746       1.1    nonaka 
    747       1.1    nonaka 	/*
    748       1.1    nonaka 	 * Reset the entire host controller and wait up to 100ms for
    749       1.1    nonaka 	 * the controller to clear the reset bit.
    750       1.1    nonaka 	 */
    751       1.1    nonaka 	error = sdhc_soft_reset(hp, SDHC_RESET_ALL);
    752       1.1    nonaka 	if (error)
    753       1.1    nonaka 		goto out;
    754       1.1    nonaka 
    755       1.1    nonaka 	/* Set data timeout counter value to max for now. */
    756       1.1    nonaka 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    757      1.29      matt #if 1
    758      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    759      1.11      matt 		HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
    760      1.11      matt #endif
    761       1.1    nonaka 
    762       1.1    nonaka 	/* Enable interrupts. */
    763       1.1    nonaka 	sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
    764       1.1    nonaka 	    SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
    765       1.1    nonaka 	    SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
    766       1.1    nonaka 	    SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
    767      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    768      1.11      matt 		sdhcimask |= SDHC_EINTR_STATUS_MASK << 16;
    769      1.11      matt 		HWRITE4(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
    770      1.11      matt 		sdhcimask ^=
    771      1.11      matt 		    (SDHC_EINTR_STATUS_MASK ^ SDHC_EINTR_SIGNAL_MASK) << 16;
    772      1.11      matt 		sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
    773      1.11      matt 		HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
    774      1.11      matt 	} else {
    775      1.11      matt 		HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
    776      1.11      matt 		HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
    777      1.11      matt 		sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
    778      1.11      matt 		HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
    779      1.11      matt 		HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
    780      1.11      matt 	}
    781       1.1    nonaka 
    782       1.1    nonaka out:
    783       1.1    nonaka 	return error;
    784       1.1    nonaka }
    785       1.1    nonaka 
    786       1.1    nonaka static int
    787       1.1    nonaka sdhc_host_reset(sdmmc_chipset_handle_t sch)
    788       1.1    nonaka {
    789       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    790       1.1    nonaka 	int error;
    791       1.1    nonaka 
    792  1.51.2.3     skrll 	mutex_enter(&hp->intr_lock);
    793       1.1    nonaka 	error = sdhc_host_reset1(sch);
    794  1.51.2.3     skrll 	mutex_exit(&hp->intr_lock);
    795       1.1    nonaka 
    796       1.1    nonaka 	return error;
    797       1.1    nonaka }
    798       1.1    nonaka 
    799       1.1    nonaka static uint32_t
    800       1.1    nonaka sdhc_host_ocr(sdmmc_chipset_handle_t sch)
    801       1.1    nonaka {
    802       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    803       1.1    nonaka 
    804       1.1    nonaka 	return hp->ocr;
    805       1.1    nonaka }
    806       1.1    nonaka 
    807       1.1    nonaka static int
    808       1.1    nonaka sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
    809       1.1    nonaka {
    810       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    811       1.1    nonaka 
    812       1.1    nonaka 	return hp->maxblklen;
    813       1.1    nonaka }
    814       1.1    nonaka 
    815       1.1    nonaka /*
    816       1.1    nonaka  * Return non-zero if the card is currently inserted.
    817       1.1    nonaka  */
    818       1.1    nonaka static int
    819       1.1    nonaka sdhc_card_detect(sdmmc_chipset_handle_t sch)
    820       1.1    nonaka {
    821       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    822       1.1    nonaka 	int r;
    823       1.1    nonaka 
    824      1.32  kiyohara 	if (hp->sc->sc_vendor_card_detect)
    825      1.32  kiyohara 		return (*hp->sc->sc_vendor_card_detect)(hp->sc);
    826      1.32  kiyohara 
    827       1.1    nonaka 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED);
    828       1.1    nonaka 
    829      1.11      matt 	return r ? 1 : 0;
    830       1.1    nonaka }
    831       1.1    nonaka 
    832       1.1    nonaka /*
    833       1.1    nonaka  * Return non-zero if the card is currently write-protected.
    834       1.1    nonaka  */
    835       1.1    nonaka static int
    836       1.1    nonaka sdhc_write_protect(sdmmc_chipset_handle_t sch)
    837       1.1    nonaka {
    838       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    839       1.1    nonaka 	int r;
    840       1.1    nonaka 
    841      1.32  kiyohara 	if (hp->sc->sc_vendor_write_protect)
    842      1.32  kiyohara 		return (*hp->sc->sc_vendor_write_protect)(hp->sc);
    843      1.32  kiyohara 
    844       1.1    nonaka 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH);
    845       1.1    nonaka 
    846      1.12    nonaka 	return r ? 0 : 1;
    847       1.1    nonaka }
    848       1.1    nonaka 
    849       1.1    nonaka /*
    850       1.1    nonaka  * Set or change SD bus voltage and enable or disable SD bus power.
    851       1.1    nonaka  * Return zero on success.
    852       1.1    nonaka  */
    853       1.1    nonaka static int
    854       1.1    nonaka sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    855       1.1    nonaka {
    856       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    857       1.1    nonaka 	uint8_t vdd;
    858       1.1    nonaka 	int error = 0;
    859      1.32  kiyohara 	const uint32_t pcmask =
    860      1.32  kiyohara 	    ~(SDHC_BUS_POWER | (SDHC_VOLTAGE_MASK << SDHC_VOLTAGE_SHIFT));
    861       1.1    nonaka 
    862  1.51.2.3     skrll 	mutex_enter(&hp->intr_lock);
    863       1.1    nonaka 
    864       1.1    nonaka 	/*
    865       1.1    nonaka 	 * Disable bus power before voltage change.
    866       1.1    nonaka 	 */
    867      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)
    868      1.11      matt 	    && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_PWR0))
    869       1.1    nonaka 		HWRITE1(hp, SDHC_POWER_CTL, 0);
    870       1.1    nonaka 
    871       1.1    nonaka 	/* If power is disabled, reset the host and return now. */
    872       1.1    nonaka 	if (ocr == 0) {
    873       1.1    nonaka 		(void)sdhc_host_reset1(hp);
    874  1.51.2.3     skrll 		callout_halt(&hp->tuning_timer, &hp->intr_lock);
    875       1.1    nonaka 		goto out;
    876       1.1    nonaka 	}
    877       1.1    nonaka 
    878       1.1    nonaka 	/*
    879       1.1    nonaka 	 * Select the lowest voltage according to capabilities.
    880       1.1    nonaka 	 */
    881       1.1    nonaka 	ocr &= hp->ocr;
    882      1.11      matt 	if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V)) {
    883       1.1    nonaka 		vdd = SDHC_VOLTAGE_1_8V;
    884      1.11      matt 	} else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V)) {
    885       1.1    nonaka 		vdd = SDHC_VOLTAGE_3_0V;
    886      1.11      matt 	} else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) {
    887       1.1    nonaka 		vdd = SDHC_VOLTAGE_3_3V;
    888      1.11      matt 	} else {
    889       1.1    nonaka 		/* Unsupported voltage level requested. */
    890       1.1    nonaka 		error = EINVAL;
    891       1.1    nonaka 		goto out;
    892       1.1    nonaka 	}
    893       1.1    nonaka 
    894      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    895      1.11      matt 		/*
    896      1.11      matt 		 * Enable bus power.  Wait at least 1 ms (or 74 clocks) plus
    897      1.11      matt 		 * voltage ramp until power rises.
    898      1.11      matt 		 */
    899  1.51.2.2     skrll 
    900  1.51.2.2     skrll 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_SINGLE_POWER_WRITE)) {
    901  1.51.2.2     skrll 			HWRITE1(hp, SDHC_POWER_CTL,
    902  1.51.2.2     skrll 			    (vdd << SDHC_VOLTAGE_SHIFT) | SDHC_BUS_POWER);
    903  1.51.2.2     skrll 		} else {
    904  1.51.2.2     skrll 			HWRITE1(hp, SDHC_POWER_CTL,
    905  1.51.2.2     skrll 			    HREAD1(hp, SDHC_POWER_CTL) & pcmask);
    906  1.51.2.2     skrll 			sdmmc_delay(1);
    907  1.51.2.2     skrll 			HWRITE1(hp, SDHC_POWER_CTL,
    908  1.51.2.2     skrll 			    (vdd << SDHC_VOLTAGE_SHIFT));
    909  1.51.2.2     skrll 			sdmmc_delay(1);
    910  1.51.2.2     skrll 			HSET1(hp, SDHC_POWER_CTL, SDHC_BUS_POWER);
    911  1.51.2.2     skrll 			sdmmc_delay(10000);
    912  1.51.2.2     skrll 		}
    913       1.1    nonaka 
    914      1.11      matt 		/*
    915      1.11      matt 		 * The host system may not power the bus due to battery low,
    916      1.11      matt 		 * etc.  In that case, the host controller should clear the
    917      1.11      matt 		 * bus power bit.
    918      1.11      matt 		 */
    919      1.11      matt 		if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
    920      1.11      matt 			error = ENXIO;
    921      1.11      matt 			goto out;
    922      1.11      matt 		}
    923       1.1    nonaka 	}
    924       1.1    nonaka 
    925       1.1    nonaka out:
    926  1.51.2.3     skrll 	mutex_exit(&hp->intr_lock);
    927       1.1    nonaka 
    928       1.1    nonaka 	return error;
    929       1.1    nonaka }
    930       1.1    nonaka 
    931       1.1    nonaka /*
    932       1.1    nonaka  * Return the smallest possible base clock frequency divisor value
    933       1.1    nonaka  * for the CLOCK_CTL register to produce `freq' (KHz).
    934       1.1    nonaka  */
    935      1.11      matt static bool
    936      1.11      matt sdhc_clock_divisor(struct sdhc_host *hp, u_int freq, u_int *divp)
    937       1.1    nonaka {
    938      1.11      matt 	u_int div;
    939       1.1    nonaka 
    940      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_CGM)) {
    941      1.11      matt 		for (div = hp->clkbase / freq; div <= 0x3ff; div++) {
    942      1.11      matt 			if ((hp->clkbase / div) <= freq) {
    943      1.11      matt 				*divp = SDHC_SDCLK_CGM
    944      1.11      matt 				    | ((div & 0x300) << SDHC_SDCLK_XDIV_SHIFT)
    945      1.11      matt 				    | ((div & 0x0ff) << SDHC_SDCLK_DIV_SHIFT);
    946      1.18  jakllsch 				//freq = hp->clkbase / div;
    947      1.11      matt 				return true;
    948      1.11      matt 			}
    949      1.11      matt 		}
    950      1.11      matt 		/* No divisor found. */
    951      1.11      matt 		return false;
    952      1.11      matt 	}
    953      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_DVS)) {
    954      1.11      matt 		u_int dvs = (hp->clkbase + freq - 1) / freq;
    955      1.11      matt 		u_int roundup = dvs & 1;
    956      1.11      matt 		for (dvs >>= 1, div = 1; div <= 256; div <<= 1, dvs >>= 1) {
    957      1.11      matt 			if (dvs + roundup <= 16) {
    958      1.11      matt 				dvs += roundup - 1;
    959      1.11      matt 				*divp = (div << SDHC_SDCLK_DIV_SHIFT)
    960      1.11      matt 				    |   (dvs << SDHC_SDCLK_DVS_SHIFT);
    961      1.11      matt 				DPRINTF(2,
    962      1.11      matt 				    ("%s: divisor for freq %u is %u * %u\n",
    963      1.11      matt 				    HDEVNAME(hp), freq, div * 2, dvs + 1));
    964      1.18  jakllsch 				//freq = hp->clkbase / (div * 2) * (dvs + 1);
    965      1.11      matt 				return true;
    966       1.9      matt 			}
    967      1.11      matt 			/*
    968      1.11      matt 			 * If we drop bits, we need to round up the divisor.
    969      1.11      matt 			 */
    970      1.11      matt 			roundup |= dvs & 1;
    971       1.9      matt 		}
    972      1.18  jakllsch 		/* No divisor found. */
    973      1.18  jakllsch 		return false;
    974      1.38  jakllsch 	}
    975      1.38  jakllsch 	if (hp->sc->sc_clkmsk != 0) {
    976      1.38  jakllsch 		div = howmany(hp->clkbase, freq);
    977      1.38  jakllsch 		if (div > (hp->sc->sc_clkmsk >> (ffs(hp->sc->sc_clkmsk) - 1)))
    978      1.38  jakllsch 			return false;
    979      1.38  jakllsch 		*divp = div << (ffs(hp->sc->sc_clkmsk) - 1);
    980      1.38  jakllsch 		//freq = hp->clkbase / div;
    981      1.38  jakllsch 		return true;
    982      1.38  jakllsch 	}
    983  1.51.2.2     skrll 	if (hp->specver >= SDHC_SPEC_VERS_300) {
    984      1.38  jakllsch 		div = howmany(hp->clkbase, freq);
    985      1.50   mlelstv 		div = div > 1 ? howmany(div, 2) : 0;
    986      1.38  jakllsch 		if (div > 0x3ff)
    987      1.38  jakllsch 			return false;
    988      1.38  jakllsch 		*divp = (((div >> 8) & SDHC_SDCLK_XDIV_MASK)
    989      1.38  jakllsch 			 << SDHC_SDCLK_XDIV_SHIFT) |
    990      1.38  jakllsch 			(((div >> 0) & SDHC_SDCLK_DIV_MASK)
    991      1.38  jakllsch 			 << SDHC_SDCLK_DIV_SHIFT);
    992  1.51.2.3     skrll 		//freq = hp->clkbase / (div ? div * 2 : 1);
    993      1.38  jakllsch 		return true;
    994       1.9      matt 	} else {
    995      1.38  jakllsch 		for (div = 1; div <= 256; div *= 2) {
    996      1.38  jakllsch 			if ((hp->clkbase / div) <= freq) {
    997      1.38  jakllsch 				*divp = (div / 2) << SDHC_SDCLK_DIV_SHIFT;
    998      1.38  jakllsch 				//freq = hp->clkbase / div;
    999      1.38  jakllsch 				return true;
   1000      1.38  jakllsch 			}
   1001      1.38  jakllsch 		}
   1002      1.38  jakllsch 		/* No divisor found. */
   1003      1.38  jakllsch 		return false;
   1004       1.9      matt 	}
   1005       1.1    nonaka 	/* No divisor found. */
   1006      1.11      matt 	return false;
   1007       1.1    nonaka }
   1008       1.1    nonaka 
   1009       1.1    nonaka /*
   1010       1.1    nonaka  * Set or change SDCLK frequency or disable the SD clock.
   1011       1.1    nonaka  * Return zero on success.
   1012       1.1    nonaka  */
   1013       1.1    nonaka static int
   1014  1.51.2.3     skrll sdhc_bus_clock_ddr(sdmmc_chipset_handle_t sch, int freq, bool ddr)
   1015       1.1    nonaka {
   1016       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1017      1.11      matt 	u_int div;
   1018      1.11      matt 	u_int timo;
   1019      1.32  kiyohara 	int16_t reg;
   1020       1.1    nonaka 	int error = 0;
   1021  1.51.2.3     skrll 	bool present __diagused;
   1022       1.1    nonaka 
   1023  1.51.2.3     skrll 	mutex_enter(&hp->intr_lock);
   1024  1.51.2.3     skrll 
   1025  1.51.2.3     skrll #ifdef DIAGNOSTIC
   1026      1.12    nonaka 	present = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK);
   1027       1.1    nonaka 
   1028       1.1    nonaka 	/* Must not stop the clock if commands are in progress. */
   1029      1.12    nonaka 	if (present && sdhc_card_detect(hp)) {
   1030      1.26      matt 		aprint_normal_dev(hp->sc->sc_dev,
   1031      1.26      matt 		    "%s: command in progress\n", __func__);
   1032      1.12    nonaka 	}
   1033       1.1    nonaka #endif
   1034       1.1    nonaka 
   1035      1.34      matt 	if (hp->sc->sc_vendor_bus_clock) {
   1036      1.34      matt 		error = (*hp->sc->sc_vendor_bus_clock)(hp->sc, freq);
   1037      1.34      matt 		if (error != 0)
   1038      1.34      matt 			goto out;
   1039      1.34      matt 	}
   1040      1.34      matt 
   1041       1.1    nonaka 	/*
   1042       1.1    nonaka 	 * Stop SD clock before changing the frequency.
   1043       1.1    nonaka 	 */
   1044      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1045      1.11      matt 		HCLR4(hp, SDHC_CLOCK_CTL, 0xfff8);
   1046      1.11      matt 		if (freq == SDMMC_SDCLK_OFF) {
   1047      1.11      matt 			HSET4(hp, SDHC_CLOCK_CTL, 0x80f0);
   1048      1.11      matt 			goto out;
   1049      1.11      matt 		}
   1050      1.11      matt 	} else {
   1051      1.32  kiyohara 		HCLR2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
   1052      1.11      matt 		if (freq == SDMMC_SDCLK_OFF)
   1053      1.11      matt 			goto out;
   1054      1.11      matt 	}
   1055       1.1    nonaka 
   1056  1.51.2.3     skrll 	if (hp->specver >= SDHC_SPEC_VERS_300) {
   1057  1.51.2.3     skrll 		HCLR2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_MASK);
   1058  1.51.2.3     skrll 		if (freq > 100000) {
   1059  1.51.2.3     skrll 			HSET2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_SDR104);
   1060  1.51.2.3     skrll 		} else if (freq > 50000) {
   1061  1.51.2.3     skrll 			HSET2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_SDR50);
   1062  1.51.2.3     skrll 		} else if (freq > 25000) {
   1063  1.51.2.3     skrll 			if (ddr) {
   1064  1.51.2.3     skrll 				HSET2(hp, SDHC_HOST_CTL2,
   1065  1.51.2.3     skrll 				    SDHC_UHS_MODE_SELECT_DDR50);
   1066  1.51.2.3     skrll 			} else {
   1067  1.51.2.3     skrll 				HSET2(hp, SDHC_HOST_CTL2,
   1068  1.51.2.3     skrll 				    SDHC_UHS_MODE_SELECT_SDR25);
   1069  1.51.2.3     skrll 			}
   1070  1.51.2.3     skrll 		} else if (freq > 400) {
   1071  1.51.2.3     skrll 			HSET2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_SDR12);
   1072  1.51.2.3     skrll 		}
   1073  1.51.2.3     skrll 	}
   1074  1.51.2.3     skrll 
   1075  1.51.2.3     skrll 	/*
   1076  1.51.2.3     skrll 	 * Slow down Ricoh 5U823 controller that isn't reliable
   1077  1.51.2.3     skrll 	 * at 100MHz bus clock.
   1078  1.51.2.3     skrll 	 */
   1079  1.51.2.3     skrll 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_SLOW_SDR50)) {
   1080  1.51.2.3     skrll 		if (freq == 100000)
   1081  1.51.2.3     skrll 			--freq;
   1082  1.51.2.3     skrll 	}
   1083  1.51.2.3     skrll 
   1084       1.1    nonaka 	/*
   1085       1.1    nonaka 	 * Set the minimum base clock frequency divisor.
   1086       1.1    nonaka 	 */
   1087      1.11      matt 	if (!sdhc_clock_divisor(hp, freq, &div)) {
   1088       1.1    nonaka 		/* Invalid base clock frequency or `freq' value. */
   1089  1.51.2.3     skrll 		aprint_error_dev(hp->sc->sc_dev,
   1090  1.51.2.3     skrll 			"Invalid bus clock %d kHz\n", freq);
   1091       1.1    nonaka 		error = EINVAL;
   1092       1.1    nonaka 		goto out;
   1093       1.1    nonaka 	}
   1094      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1095      1.11      matt 		HWRITE4(hp, SDHC_CLOCK_CTL,
   1096      1.11      matt 		    div | (SDHC_TIMEOUT_MAX << 16));
   1097      1.11      matt 	} else {
   1098      1.32  kiyohara 		reg = HREAD2(hp, SDHC_CLOCK_CTL);
   1099      1.32  kiyohara 		reg &= (SDHC_INTCLK_STABLE | SDHC_INTCLK_ENABLE);
   1100      1.32  kiyohara 		HWRITE2(hp, SDHC_CLOCK_CTL, reg | div);
   1101      1.11      matt 	}
   1102       1.1    nonaka 
   1103       1.1    nonaka 	/*
   1104       1.1    nonaka 	 * Start internal clock.  Wait 10ms for stabilization.
   1105       1.1    nonaka 	 */
   1106      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1107      1.11      matt 		sdmmc_delay(10000);
   1108      1.12    nonaka 		HSET4(hp, SDHC_CLOCK_CTL,
   1109      1.12    nonaka 		    8 | SDHC_INTCLK_ENABLE | SDHC_INTCLK_STABLE);
   1110      1.11      matt 	} else {
   1111      1.11      matt 		HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
   1112      1.11      matt 		for (timo = 1000; timo > 0; timo--) {
   1113      1.12    nonaka 			if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL),
   1114      1.12    nonaka 			    SDHC_INTCLK_STABLE))
   1115      1.11      matt 				break;
   1116      1.11      matt 			sdmmc_delay(10);
   1117      1.11      matt 		}
   1118      1.11      matt 		if (timo == 0) {
   1119      1.11      matt 			error = ETIMEDOUT;
   1120  1.51.2.3     skrll 			DPRINTF(1,("%s: timeout\n", __func__));
   1121      1.11      matt 			goto out;
   1122      1.11      matt 		}
   1123       1.1    nonaka 	}
   1124       1.1    nonaka 
   1125      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1126      1.11      matt 		HSET1(hp, SDHC_SOFTWARE_RESET, SDHC_INIT_ACTIVE);
   1127      1.11      matt 		/*
   1128      1.11      matt 		 * Sending 80 clocks at 400kHz takes 200us.
   1129      1.11      matt 		 * So delay for that time + slop and then
   1130      1.11      matt 		 * check a few times for completion.
   1131      1.11      matt 		 */
   1132      1.11      matt 		sdmmc_delay(210);
   1133      1.11      matt 		for (timo = 10; timo > 0; timo--) {
   1134      1.11      matt 			if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET),
   1135      1.11      matt 			    SDHC_INIT_ACTIVE))
   1136      1.11      matt 				break;
   1137      1.11      matt 			sdmmc_delay(10);
   1138      1.11      matt 		}
   1139      1.11      matt 		DPRINTF(2,("%s: %u init spins\n", __func__, 10 - timo));
   1140      1.12    nonaka 
   1141      1.11      matt 		/*
   1142      1.11      matt 		 * Enable SD clock.
   1143      1.11      matt 		 */
   1144      1.11      matt 		HSET4(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
   1145      1.11      matt 	} else {
   1146      1.11      matt 		/*
   1147      1.11      matt 		 * Enable SD clock.
   1148      1.11      matt 		 */
   1149      1.11      matt 		HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
   1150       1.1    nonaka 
   1151      1.43  jmcneill 		if (freq > 25000 &&
   1152      1.43  jmcneill 		    !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_HS_BIT))
   1153      1.11      matt 			HSET1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
   1154      1.11      matt 		else
   1155      1.11      matt 			HCLR1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
   1156      1.11      matt 	}
   1157       1.8  kiyohara 
   1158       1.1    nonaka out:
   1159  1.51.2.3     skrll 	mutex_exit(&hp->intr_lock);
   1160       1.1    nonaka 
   1161       1.1    nonaka 	return error;
   1162       1.1    nonaka }
   1163       1.1    nonaka 
   1164       1.1    nonaka static int
   1165       1.1    nonaka sdhc_bus_width(sdmmc_chipset_handle_t sch, int width)
   1166       1.1    nonaka {
   1167       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1168       1.1    nonaka 	int reg;
   1169       1.1    nonaka 
   1170       1.1    nonaka 	switch (width) {
   1171       1.1    nonaka 	case 1:
   1172       1.1    nonaka 	case 4:
   1173       1.1    nonaka 		break;
   1174       1.1    nonaka 
   1175      1.11      matt 	case 8:
   1176      1.11      matt 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_8BIT_MODE))
   1177      1.11      matt 			break;
   1178      1.11      matt 		/* FALLTHROUGH */
   1179       1.1    nonaka 	default:
   1180       1.1    nonaka 		DPRINTF(0,("%s: unsupported bus width (%d)\n",
   1181       1.1    nonaka 		    HDEVNAME(hp), width));
   1182       1.1    nonaka 		return 1;
   1183       1.1    nonaka 	}
   1184       1.1    nonaka 
   1185  1.51.2.3     skrll 	mutex_enter(&hp->intr_lock);
   1186  1.51.2.3     skrll 
   1187       1.5  uebayasi 	reg = HREAD1(hp, SDHC_HOST_CTL);
   1188      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1189      1.12    nonaka 		reg &= ~(SDHC_4BIT_MODE|SDHC_ESDHC_8BIT_MODE);
   1190      1.11      matt 		if (width == 4)
   1191      1.11      matt 			reg |= SDHC_4BIT_MODE;
   1192      1.11      matt 		else if (width == 8)
   1193      1.12    nonaka 			reg |= SDHC_ESDHC_8BIT_MODE;
   1194      1.11      matt 	} else {
   1195      1.11      matt 		reg &= ~SDHC_4BIT_MODE;
   1196  1.51.2.2     skrll 		if (hp->specver >= SDHC_SPEC_VERS_300) {
   1197  1.51.2.2     skrll 			reg &= ~SDHC_8BIT_MODE;
   1198  1.51.2.2     skrll 		}
   1199  1.51.2.2     skrll 		if (width == 4) {
   1200      1.11      matt 			reg |= SDHC_4BIT_MODE;
   1201  1.51.2.2     skrll 		} else if (width == 8 && hp->specver >= SDHC_SPEC_VERS_300) {
   1202  1.51.2.2     skrll 			reg |= SDHC_8BIT_MODE;
   1203  1.51.2.2     skrll 		}
   1204      1.11      matt 	}
   1205       1.5  uebayasi 	HWRITE1(hp, SDHC_HOST_CTL, reg);
   1206  1.51.2.3     skrll 
   1207  1.51.2.3     skrll 	mutex_exit(&hp->intr_lock);
   1208       1.1    nonaka 
   1209       1.1    nonaka 	return 0;
   1210       1.1    nonaka }
   1211       1.1    nonaka 
   1212       1.8  kiyohara static int
   1213       1.8  kiyohara sdhc_bus_rod(sdmmc_chipset_handle_t sch, int on)
   1214       1.8  kiyohara {
   1215      1.32  kiyohara 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1216      1.32  kiyohara 
   1217      1.32  kiyohara 	if (hp->sc->sc_vendor_rod)
   1218      1.32  kiyohara 		return (*hp->sc->sc_vendor_rod)(hp->sc, on);
   1219       1.8  kiyohara 
   1220       1.8  kiyohara 	return 0;
   1221       1.8  kiyohara }
   1222       1.8  kiyohara 
   1223       1.1    nonaka static void
   1224       1.1    nonaka sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
   1225       1.1    nonaka {
   1226       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1227       1.1    nonaka 
   1228      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1229  1.51.2.3     skrll 		mutex_enter(&hp->intr_lock);
   1230      1.11      matt 		if (enable) {
   1231      1.11      matt 			HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1232      1.11      matt 			HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
   1233      1.11      matt 		} else {
   1234      1.11      matt 			HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
   1235      1.11      matt 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1236      1.11      matt 		}
   1237  1.51.2.3     skrll 		mutex_exit(&hp->intr_lock);
   1238       1.1    nonaka 	}
   1239       1.1    nonaka }
   1240       1.1    nonaka 
   1241      1.47     skrll static void
   1242       1.1    nonaka sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
   1243       1.1    nonaka {
   1244       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1245       1.1    nonaka 
   1246      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1247  1.51.2.3     skrll 		mutex_enter(&hp->intr_lock);
   1248      1.11      matt 		HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1249  1.51.2.3     skrll 		mutex_exit(&hp->intr_lock);
   1250  1.51.2.3     skrll 	}
   1251  1.51.2.3     skrll }
   1252  1.51.2.3     skrll 
   1253  1.51.2.3     skrll static int
   1254  1.51.2.3     skrll sdhc_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
   1255  1.51.2.3     skrll {
   1256  1.51.2.3     skrll 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1257  1.51.2.3     skrll 
   1258  1.51.2.3     skrll 	mutex_enter(&hp->intr_lock);
   1259  1.51.2.3     skrll 	switch (signal_voltage) {
   1260  1.51.2.3     skrll 	case SDMMC_SIGNAL_VOLTAGE_180:
   1261  1.51.2.3     skrll 		HSET2(hp, SDHC_HOST_CTL2, SDHC_1_8V_SIGNAL_EN);
   1262  1.51.2.3     skrll 		break;
   1263  1.51.2.3     skrll 	case SDMMC_SIGNAL_VOLTAGE_330:
   1264  1.51.2.3     skrll 		HCLR2(hp, SDHC_HOST_CTL2, SDHC_1_8V_SIGNAL_EN);
   1265  1.51.2.3     skrll 		break;
   1266  1.51.2.3     skrll 	default:
   1267  1.51.2.3     skrll 		return EINVAL;
   1268      1.11      matt 	}
   1269  1.51.2.3     skrll 	mutex_exit(&hp->intr_lock);
   1270  1.51.2.3     skrll 
   1271  1.51.2.3     skrll 	return 0;
   1272  1.51.2.3     skrll }
   1273  1.51.2.3     skrll 
   1274  1.51.2.3     skrll /*
   1275  1.51.2.3     skrll  * Sampling clock tuning procedure (UHS)
   1276  1.51.2.3     skrll  */
   1277  1.51.2.3     skrll static int
   1278  1.51.2.3     skrll sdhc_execute_tuning1(struct sdhc_host *hp, int timing)
   1279  1.51.2.3     skrll {
   1280  1.51.2.3     skrll 	struct sdmmc_command cmd;
   1281  1.51.2.3     skrll 	uint8_t hostctl;
   1282  1.51.2.3     skrll 	int opcode, error, retry = 40;
   1283  1.51.2.3     skrll 
   1284  1.51.2.3     skrll 	KASSERT(mutex_owned(&hp->intr_lock));
   1285  1.51.2.3     skrll 
   1286  1.51.2.3     skrll 	hp->tuning_timing = timing;
   1287  1.51.2.3     skrll 
   1288  1.51.2.3     skrll 	switch (timing) {
   1289  1.51.2.3     skrll 	case SDMMC_TIMING_MMC_HS200:
   1290  1.51.2.3     skrll 		opcode = MMC_SEND_TUNING_BLOCK_HS200;
   1291  1.51.2.3     skrll 		break;
   1292  1.51.2.3     skrll 	case SDMMC_TIMING_UHS_SDR50:
   1293  1.51.2.3     skrll 		if (!ISSET(hp->sc->sc_caps2, SDHC_TUNING_SDR50))
   1294  1.51.2.3     skrll 			return 0;
   1295  1.51.2.3     skrll 		/* FALLTHROUGH */
   1296  1.51.2.3     skrll 	case SDMMC_TIMING_UHS_SDR104:
   1297  1.51.2.3     skrll 		opcode = MMC_SEND_TUNING_BLOCK;
   1298  1.51.2.3     skrll 		break;
   1299  1.51.2.3     skrll 	default:
   1300  1.51.2.3     skrll 		return EINVAL;
   1301  1.51.2.3     skrll 	}
   1302  1.51.2.3     skrll 
   1303  1.51.2.3     skrll 	hostctl = HREAD1(hp, SDHC_HOST_CTL);
   1304  1.51.2.3     skrll 
   1305  1.51.2.3     skrll 	/* enable buffer read ready interrupt */
   1306  1.51.2.3     skrll 	HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_BUFFER_READ_READY);
   1307  1.51.2.3     skrll 	HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_BUFFER_READ_READY);
   1308  1.51.2.3     skrll 
   1309  1.51.2.3     skrll 	/* disable DMA */
   1310  1.51.2.3     skrll 	HCLR1(hp, SDHC_HOST_CTL, SDHC_DMA_SELECT);
   1311  1.51.2.3     skrll 
   1312  1.51.2.3     skrll 	/* reset tuning circuit */
   1313  1.51.2.3     skrll 	HCLR2(hp, SDHC_HOST_CTL2, SDHC_SAMPLING_CLOCK_SEL);
   1314  1.51.2.3     skrll 
   1315  1.51.2.3     skrll 	/* start of tuning */
   1316  1.51.2.3     skrll 	HWRITE2(hp, SDHC_HOST_CTL2, SDHC_EXECUTE_TUNING);
   1317  1.51.2.3     skrll 
   1318  1.51.2.3     skrll 	do {
   1319  1.51.2.3     skrll 		memset(&cmd, 0, sizeof(cmd));
   1320  1.51.2.3     skrll 		cmd.c_opcode = opcode;
   1321  1.51.2.3     skrll 		cmd.c_arg = 0;
   1322  1.51.2.3     skrll 		cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1;
   1323  1.51.2.3     skrll 		if (ISSET(hostctl, SDHC_8BIT_MODE)) {
   1324  1.51.2.3     skrll 			cmd.c_blklen = cmd.c_datalen = 128;
   1325  1.51.2.3     skrll 		} else {
   1326  1.51.2.3     skrll 			cmd.c_blklen = cmd.c_datalen = 64;
   1327  1.51.2.3     skrll 		}
   1328  1.51.2.3     skrll 
   1329  1.51.2.3     skrll 		error = sdhc_start_command(hp, &cmd);
   1330  1.51.2.3     skrll 		if (error)
   1331  1.51.2.3     skrll 			break;
   1332  1.51.2.3     skrll 
   1333  1.51.2.3     skrll 		if (!sdhc_wait_intr(hp, SDHC_BUFFER_READ_READY,
   1334  1.51.2.3     skrll 		    SDHC_TUNING_TIMEOUT)) {
   1335  1.51.2.3     skrll 			break;
   1336  1.51.2.3     skrll 		}
   1337  1.51.2.3     skrll 
   1338  1.51.2.3     skrll 		delay(1000);
   1339  1.51.2.3     skrll 	} while (HREAD2(hp, SDHC_HOST_CTL2) & SDHC_EXECUTE_TUNING && --retry);
   1340  1.51.2.3     skrll 
   1341  1.51.2.3     skrll 	/* disable buffer read ready interrupt */
   1342  1.51.2.3     skrll 	HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_BUFFER_READ_READY);
   1343  1.51.2.3     skrll 	HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_BUFFER_READ_READY);
   1344  1.51.2.3     skrll 
   1345  1.51.2.3     skrll 	if (HREAD2(hp, SDHC_HOST_CTL2) & SDHC_EXECUTE_TUNING) {
   1346  1.51.2.3     skrll 		HCLR2(hp, SDHC_HOST_CTL2,
   1347  1.51.2.3     skrll 		    SDHC_SAMPLING_CLOCK_SEL|SDHC_EXECUTE_TUNING);
   1348  1.51.2.3     skrll 		sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
   1349  1.51.2.3     skrll 		aprint_error_dev(hp->sc->sc_dev,
   1350  1.51.2.3     skrll 		    "tuning did not complete, using fixed sampling clock\n");
   1351  1.51.2.3     skrll 		return EIO;		/* tuning did not complete */
   1352  1.51.2.3     skrll 	}
   1353  1.51.2.3     skrll 
   1354  1.51.2.3     skrll 	if ((HREAD2(hp, SDHC_HOST_CTL2) & SDHC_SAMPLING_CLOCK_SEL) == 0) {
   1355  1.51.2.3     skrll 		HCLR2(hp, SDHC_HOST_CTL2,
   1356  1.51.2.3     skrll 		    SDHC_SAMPLING_CLOCK_SEL|SDHC_EXECUTE_TUNING);
   1357  1.51.2.3     skrll 		sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
   1358  1.51.2.3     skrll 		aprint_error_dev(hp->sc->sc_dev,
   1359  1.51.2.3     skrll 		    "tuning failed, using fixed sampling clock\n");
   1360  1.51.2.3     skrll 		return EIO;		/* tuning failed */
   1361  1.51.2.3     skrll 	}
   1362  1.51.2.3     skrll 
   1363  1.51.2.3     skrll 	if (hp->tuning_timer_count) {
   1364  1.51.2.3     skrll 		callout_schedule(&hp->tuning_timer,
   1365  1.51.2.3     skrll 		    hz * hp->tuning_timer_count);
   1366  1.51.2.3     skrll 	}
   1367  1.51.2.3     skrll 
   1368  1.51.2.3     skrll 	return 0;		/* tuning completed */
   1369  1.51.2.3     skrll }
   1370  1.51.2.3     skrll 
   1371  1.51.2.3     skrll static int
   1372  1.51.2.3     skrll sdhc_execute_tuning(sdmmc_chipset_handle_t sch, int timing)
   1373  1.51.2.3     skrll {
   1374  1.51.2.3     skrll 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1375  1.51.2.3     skrll 	int error;
   1376  1.51.2.3     skrll 
   1377  1.51.2.3     skrll 	mutex_enter(&hp->intr_lock);
   1378  1.51.2.3     skrll 	error = sdhc_execute_tuning1(hp, timing);
   1379  1.51.2.3     skrll 	mutex_exit(&hp->intr_lock);
   1380  1.51.2.3     skrll 	return error;
   1381  1.51.2.3     skrll }
   1382  1.51.2.3     skrll 
   1383  1.51.2.3     skrll static void
   1384  1.51.2.3     skrll sdhc_tuning_timer(void *arg)
   1385  1.51.2.3     skrll {
   1386  1.51.2.3     skrll 	struct sdhc_host *hp = arg;
   1387  1.51.2.3     skrll 
   1388  1.51.2.3     skrll 	atomic_swap_uint(&hp->tuning_timer_pending, 1);
   1389       1.1    nonaka }
   1390       1.1    nonaka 
   1391       1.1    nonaka static int
   1392       1.1    nonaka sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value)
   1393       1.1    nonaka {
   1394       1.1    nonaka 	uint32_t state;
   1395       1.1    nonaka 	int timeout;
   1396       1.1    nonaka 
   1397  1.51.2.3     skrll 	for (timeout = 10000; timeout > 0; timeout--) {
   1398       1.1    nonaka 		if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value)
   1399       1.1    nonaka 			return 0;
   1400  1.51.2.3     skrll 		sdmmc_delay(10);
   1401       1.1    nonaka 	}
   1402  1.51.2.3     skrll 	aprint_error_dev(hp->sc->sc_dev, "timeout waiting for mask %#x value %#x (state=%#x)\n",
   1403  1.51.2.3     skrll 	    mask, value, state);
   1404       1.1    nonaka 	return ETIMEDOUT;
   1405       1.1    nonaka }
   1406       1.1    nonaka 
   1407       1.1    nonaka static void
   1408       1.1    nonaka sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
   1409       1.1    nonaka {
   1410       1.1    nonaka 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1411       1.1    nonaka 	int error;
   1412       1.1    nonaka 
   1413  1.51.2.3     skrll 	mutex_enter(&hp->intr_lock);
   1414  1.51.2.3     skrll 
   1415  1.51.2.3     skrll 	if (atomic_cas_uint(&hp->tuning_timer_pending, 1, 0) == 1) {
   1416  1.51.2.3     skrll 		(void)sdhc_execute_tuning1(hp, hp->tuning_timing);
   1417  1.51.2.3     skrll 	}
   1418  1.51.2.3     skrll 
   1419      1.26      matt 	if (cmd->c_data && ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1420      1.11      matt 		const uint16_t ready = SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY;
   1421      1.11      matt 		if (ISSET(hp->flags, SHF_USE_DMA)) {
   1422      1.11      matt 			HCLR2(hp, SDHC_NINTR_SIGNAL_EN, ready);
   1423      1.11      matt 			HCLR2(hp, SDHC_NINTR_STATUS_EN, ready);
   1424      1.11      matt 		} else {
   1425      1.11      matt 			HSET2(hp, SDHC_NINTR_SIGNAL_EN, ready);
   1426      1.11      matt 			HSET2(hp, SDHC_NINTR_STATUS_EN, ready);
   1427      1.47     skrll 		}
   1428  1.51.2.3     skrll 	}
   1429  1.51.2.3     skrll 
   1430  1.51.2.3     skrll 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_TIMEOUT)) {
   1431  1.51.2.3     skrll 		const uint16_t eintr = SDHC_CMD_TIMEOUT_ERROR;
   1432  1.51.2.3     skrll 		if (cmd->c_data != NULL) {
   1433  1.51.2.3     skrll 			HCLR2(hp, SDHC_EINTR_SIGNAL_EN, eintr);
   1434  1.51.2.3     skrll 			HCLR2(hp, SDHC_EINTR_STATUS_EN, eintr);
   1435  1.51.2.3     skrll 		} else {
   1436  1.51.2.3     skrll 			HSET2(hp, SDHC_EINTR_SIGNAL_EN, eintr);
   1437  1.51.2.3     skrll 			HSET2(hp, SDHC_EINTR_STATUS_EN, eintr);
   1438  1.51.2.3     skrll 		}
   1439      1.11      matt 	}
   1440      1.11      matt 
   1441       1.1    nonaka 	/*
   1442       1.1    nonaka 	 * Start the MMC command, or mark `cmd' as failed and return.
   1443       1.1    nonaka 	 */
   1444       1.1    nonaka 	error = sdhc_start_command(hp, cmd);
   1445       1.1    nonaka 	if (error) {
   1446       1.1    nonaka 		cmd->c_error = error;
   1447       1.1    nonaka 		goto out;
   1448       1.1    nonaka 	}
   1449       1.1    nonaka 
   1450       1.1    nonaka 	/*
   1451       1.1    nonaka 	 * Wait until the command phase is done, or until the command
   1452       1.1    nonaka 	 * is marked done for any other reason.
   1453       1.1    nonaka 	 */
   1454       1.1    nonaka 	if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT)) {
   1455  1.51.2.3     skrll 		DPRINTF(1,("%s: timeout for command\n", __func__));
   1456       1.1    nonaka 		cmd->c_error = ETIMEDOUT;
   1457       1.1    nonaka 		goto out;
   1458       1.1    nonaka 	}
   1459       1.1    nonaka 
   1460       1.1    nonaka 	/*
   1461       1.1    nonaka 	 * The host controller removes bits [0:7] from the response
   1462       1.1    nonaka 	 * data (CRC) and we pass the data up unchanged to the bus
   1463       1.1    nonaka 	 * driver (without padding).
   1464       1.1    nonaka 	 */
   1465       1.1    nonaka 	if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
   1466      1.23      matt 		cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE + 0);
   1467      1.23      matt 		if (ISSET(cmd->c_flags, SCF_RSP_136)) {
   1468      1.23      matt 			cmd->c_resp[1] = HREAD4(hp, SDHC_RESPONSE + 4);
   1469      1.23      matt 			cmd->c_resp[2] = HREAD4(hp, SDHC_RESPONSE + 8);
   1470      1.23      matt 			cmd->c_resp[3] = HREAD4(hp, SDHC_RESPONSE + 12);
   1471      1.32  kiyohara 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_RSP136_CRC)) {
   1472      1.32  kiyohara 				cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
   1473      1.32  kiyohara 				    (cmd->c_resp[1] << 24);
   1474      1.32  kiyohara 				cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
   1475      1.32  kiyohara 				    (cmd->c_resp[2] << 24);
   1476      1.32  kiyohara 				cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
   1477      1.32  kiyohara 				    (cmd->c_resp[3] << 24);
   1478      1.32  kiyohara 				cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
   1479      1.32  kiyohara 			}
   1480       1.1    nonaka 		}
   1481       1.1    nonaka 	}
   1482      1.25      matt 	DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0]));
   1483       1.1    nonaka 
   1484       1.1    nonaka 	/*
   1485       1.1    nonaka 	 * If the command has data to transfer in any direction,
   1486       1.1    nonaka 	 * execute the transfer now.
   1487       1.1    nonaka 	 */
   1488       1.1    nonaka 	if (cmd->c_error == 0 && cmd->c_data != NULL)
   1489       1.1    nonaka 		sdhc_transfer_data(hp, cmd);
   1490      1.42  jakllsch 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY)) {
   1491      1.42  jakllsch 		if (!sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE, hz * 10)) {
   1492  1.51.2.3     skrll 			DPRINTF(1,("%s: sdhc_exec_command: RSP_BSY\n",
   1493  1.51.2.3     skrll 			    HDEVNAME(hp)));
   1494      1.42  jakllsch 			cmd->c_error = ETIMEDOUT;
   1495      1.42  jakllsch 			goto out;
   1496      1.42  jakllsch 		}
   1497      1.42  jakllsch 	}
   1498       1.1    nonaka 
   1499       1.1    nonaka out:
   1500      1.14      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)
   1501      1.14      matt 	    && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_LED_ON)) {
   1502      1.11      matt 		/* Turn off the LED. */
   1503      1.11      matt 		HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
   1504      1.11      matt 	}
   1505       1.1    nonaka 	SET(cmd->c_flags, SCF_ITSDONE);
   1506       1.1    nonaka 
   1507  1.51.2.3     skrll 	mutex_exit(&hp->intr_lock);
   1508  1.51.2.3     skrll 
   1509       1.1    nonaka 	DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp),
   1510       1.1    nonaka 	    cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort",
   1511       1.1    nonaka 	    cmd->c_flags, cmd->c_error));
   1512       1.1    nonaka }
   1513       1.1    nonaka 
   1514       1.1    nonaka static int
   1515       1.1    nonaka sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1516       1.1    nonaka {
   1517      1.11      matt 	struct sdhc_softc * const sc = hp->sc;
   1518       1.1    nonaka 	uint16_t blksize = 0;
   1519       1.1    nonaka 	uint16_t blkcount = 0;
   1520       1.1    nonaka 	uint16_t mode;
   1521       1.1    nonaka 	uint16_t command;
   1522  1.51.2.3     skrll 	uint32_t pmask;
   1523       1.1    nonaka 	int error;
   1524       1.1    nonaka 
   1525  1.51.2.3     skrll 	KASSERT(mutex_owned(&hp->intr_lock));
   1526  1.51.2.3     skrll 
   1527      1.11      matt 	DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x, status=%#x\n",
   1528       1.7    nonaka 	    HDEVNAME(hp), cmd->c_opcode, cmd->c_arg, cmd->c_data,
   1529      1.11      matt 	    cmd->c_datalen, cmd->c_flags, HREAD4(hp, SDHC_NINTR_STATUS)));
   1530       1.1    nonaka 
   1531       1.1    nonaka 	/*
   1532       1.1    nonaka 	 * The maximum block length for commands should be the minimum
   1533       1.1    nonaka 	 * of the host buffer size and the card buffer size. (1.7.2)
   1534       1.1    nonaka 	 */
   1535       1.1    nonaka 
   1536       1.1    nonaka 	/* Fragment the data into proper blocks. */
   1537       1.1    nonaka 	if (cmd->c_datalen > 0) {
   1538       1.1    nonaka 		blksize = MIN(cmd->c_datalen, cmd->c_blklen);
   1539       1.1    nonaka 		blkcount = cmd->c_datalen / blksize;
   1540       1.1    nonaka 		if (cmd->c_datalen % blksize > 0) {
   1541       1.1    nonaka 			/* XXX: Split this command. (1.7.4) */
   1542      1.11      matt 			aprint_error_dev(sc->sc_dev,
   1543       1.1    nonaka 			    "data not a multiple of %u bytes\n", blksize);
   1544       1.1    nonaka 			return EINVAL;
   1545       1.1    nonaka 		}
   1546       1.1    nonaka 	}
   1547       1.1    nonaka 
   1548       1.1    nonaka 	/* Check limit imposed by 9-bit block count. (1.7.2) */
   1549       1.1    nonaka 	if (blkcount > SDHC_BLOCK_COUNT_MAX) {
   1550      1.11      matt 		aprint_error_dev(sc->sc_dev, "too much data\n");
   1551       1.1    nonaka 		return EINVAL;
   1552       1.1    nonaka 	}
   1553       1.1    nonaka 
   1554       1.1    nonaka 	/* Prepare transfer mode register value. (2.2.5) */
   1555      1.15  jakllsch 	mode = SDHC_BLOCK_COUNT_ENABLE;
   1556       1.1    nonaka 	if (ISSET(cmd->c_flags, SCF_CMD_READ))
   1557       1.1    nonaka 		mode |= SDHC_READ_MODE;
   1558      1.15  jakllsch 	if (blkcount > 1) {
   1559      1.15  jakllsch 		mode |= SDHC_MULTI_BLOCK_MODE;
   1560      1.15  jakllsch 		/* XXX only for memory commands? */
   1561      1.15  jakllsch 		mode |= SDHC_AUTO_CMD12_ENABLE;
   1562       1.1    nonaka 	}
   1563      1.45  jakllsch 	if (cmd->c_dmamap != NULL && cmd->c_datalen > 0 &&
   1564  1.51.2.2     skrll 	    ISSET(hp->flags,  SHF_MODE_DMAEN)) {
   1565      1.19  jakllsch 		mode |= SDHC_DMA_ENABLE;
   1566       1.7    nonaka 	}
   1567       1.1    nonaka 
   1568       1.1    nonaka 	/*
   1569       1.1    nonaka 	 * Prepare command register value. (2.2.6)
   1570       1.1    nonaka 	 */
   1571      1.12    nonaka 	command = (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT;
   1572       1.1    nonaka 
   1573       1.1    nonaka 	if (ISSET(cmd->c_flags, SCF_RSP_CRC))
   1574       1.1    nonaka 		command |= SDHC_CRC_CHECK_ENABLE;
   1575       1.1    nonaka 	if (ISSET(cmd->c_flags, SCF_RSP_IDX))
   1576       1.1    nonaka 		command |= SDHC_INDEX_CHECK_ENABLE;
   1577  1.51.2.3     skrll 	if (cmd->c_datalen > 0)
   1578       1.1    nonaka 		command |= SDHC_DATA_PRESENT_SELECT;
   1579       1.1    nonaka 
   1580       1.1    nonaka 	if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
   1581       1.1    nonaka 		command |= SDHC_NO_RESPONSE;
   1582       1.1    nonaka 	else if (ISSET(cmd->c_flags, SCF_RSP_136))
   1583       1.1    nonaka 		command |= SDHC_RESP_LEN_136;
   1584       1.1    nonaka 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
   1585       1.1    nonaka 		command |= SDHC_RESP_LEN_48_CHK_BUSY;
   1586       1.1    nonaka 	else
   1587       1.1    nonaka 		command |= SDHC_RESP_LEN_48;
   1588       1.1    nonaka 
   1589  1.51.2.3     skrll 	/* Wait until command and optionally data inhibit bits are clear. (1.5) */
   1590  1.51.2.3     skrll 	pmask = SDHC_CMD_INHIBIT_CMD;
   1591  1.51.2.3     skrll 	if (cmd->c_flags & SCF_CMD_ADTC)
   1592  1.51.2.3     skrll 		pmask |= SDHC_CMD_INHIBIT_DAT;
   1593  1.51.2.3     skrll 	error = sdhc_wait_state(hp, pmask, 0);
   1594  1.51.2.3     skrll 	if (error) {
   1595  1.51.2.3     skrll 		(void) sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
   1596  1.51.2.3     skrll 		device_printf(sc->sc_dev, "command or data phase inhibited\n");
   1597       1.1    nonaka 		return error;
   1598  1.51.2.3     skrll 	}
   1599       1.1    nonaka 
   1600       1.1    nonaka 	DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n",
   1601       1.1    nonaka 	    HDEVNAME(hp), blksize, blkcount, mode, command));
   1602       1.1    nonaka 
   1603      1.44   hkenken 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1604      1.44   hkenken 		blksize |= (MAX(0, PAGE_SHIFT - 12) & SDHC_DMA_BOUNDARY_MASK) <<
   1605      1.44   hkenken 		    SDHC_DMA_BOUNDARY_SHIFT;	/* PAGE_SIZE DMA boundary */
   1606      1.44   hkenken 	}
   1607      1.19  jakllsch 
   1608      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1609      1.11      matt 		/* Alert the user not to remove the card. */
   1610      1.11      matt 		HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
   1611      1.11      matt 	}
   1612       1.1    nonaka 
   1613       1.7    nonaka 	/* Set DMA start address. */
   1614  1.51.2.3     skrll 	if (ISSET(hp->flags, SHF_USE_ADMA2_MASK) && cmd->c_data != NULL) {
   1615  1.51.2.3     skrll 		for (int seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
   1616  1.51.2.3     skrll 			bus_addr_t paddr =
   1617  1.51.2.3     skrll 			    cmd->c_dmamap->dm_segs[seg].ds_addr;
   1618  1.51.2.3     skrll 			uint16_t len =
   1619  1.51.2.3     skrll 			    cmd->c_dmamap->dm_segs[seg].ds_len == 65536 ?
   1620  1.51.2.3     skrll 			    0 : cmd->c_dmamap->dm_segs[seg].ds_len;
   1621  1.51.2.3     skrll 			uint16_t attr =
   1622  1.51.2.3     skrll 			    SDHC_ADMA2_VALID | SDHC_ADMA2_ACT_TRANS;
   1623  1.51.2.3     skrll 			if (seg == cmd->c_dmamap->dm_nsegs - 1) {
   1624  1.51.2.3     skrll 				attr |= SDHC_ADMA2_END;
   1625  1.51.2.3     skrll 			}
   1626  1.51.2.3     skrll 			if (ISSET(hp->flags, SHF_USE_ADMA2_32)) {
   1627  1.51.2.3     skrll 				struct sdhc_adma2_descriptor32 *desc =
   1628  1.51.2.3     skrll 				    hp->adma2;
   1629  1.51.2.3     skrll 				desc[seg].attribute = htole16(attr);
   1630  1.51.2.3     skrll 				desc[seg].length = htole16(len);
   1631  1.51.2.3     skrll 				desc[seg].address = htole32(paddr);
   1632  1.51.2.3     skrll 			} else {
   1633  1.51.2.3     skrll 				struct sdhc_adma2_descriptor64 *desc =
   1634  1.51.2.3     skrll 				    hp->adma2;
   1635  1.51.2.3     skrll 				desc[seg].attribute = htole16(attr);
   1636  1.51.2.3     skrll 				desc[seg].length = htole16(len);
   1637  1.51.2.3     skrll 				desc[seg].address = htole32(paddr & 0xffffffff);
   1638  1.51.2.3     skrll 				desc[seg].address_hi = htole32(
   1639  1.51.2.3     skrll 				    (uint64_t)paddr >> 32);
   1640  1.51.2.3     skrll 			}
   1641  1.51.2.3     skrll 		}
   1642  1.51.2.3     skrll 		if (ISSET(hp->flags, SHF_USE_ADMA2_32)) {
   1643  1.51.2.3     skrll 			struct sdhc_adma2_descriptor32 *desc = hp->adma2;
   1644  1.51.2.3     skrll 			desc[cmd->c_dmamap->dm_nsegs].attribute = htole16(0);
   1645  1.51.2.3     skrll 		} else {
   1646  1.51.2.3     skrll 			struct sdhc_adma2_descriptor64 *desc = hp->adma2;
   1647  1.51.2.3     skrll 			desc[cmd->c_dmamap->dm_nsegs].attribute = htole16(0);
   1648  1.51.2.3     skrll 		}
   1649  1.51.2.3     skrll 		bus_dmamap_sync(sc->sc_dmat, hp->adma_map, 0, PAGE_SIZE,
   1650  1.51.2.3     skrll 		    BUS_DMASYNC_PREWRITE);
   1651  1.51.2.3     skrll 		HCLR1(hp, SDHC_HOST_CTL, SDHC_DMA_SELECT);
   1652  1.51.2.3     skrll 		HSET1(hp, SDHC_HOST_CTL, SDHC_DMA_SELECT_ADMA2);
   1653  1.51.2.3     skrll 
   1654  1.51.2.3     skrll 		const bus_addr_t desc_addr = hp->adma_map->dm_segs[0].ds_addr;
   1655  1.51.2.3     skrll 
   1656  1.51.2.3     skrll 		HWRITE4(hp, SDHC_ADMA_SYSTEM_ADDR, desc_addr & 0xffffffff);
   1657  1.51.2.3     skrll 		if (ISSET(hp->flags, SHF_USE_ADMA2_64)) {
   1658  1.51.2.3     skrll 			HWRITE4(hp, SDHC_ADMA_SYSTEM_ADDR + 4,
   1659  1.51.2.3     skrll 			    (uint64_t)desc_addr >> 32);
   1660  1.51.2.3     skrll 		}
   1661  1.51.2.3     skrll 	} else if (ISSET(mode, SDHC_DMA_ENABLE) &&
   1662  1.51.2.3     skrll 	    !ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA)) {
   1663       1.7    nonaka 		HWRITE4(hp, SDHC_DMA_ADDR, cmd->c_dmamap->dm_segs[0].ds_addr);
   1664  1.51.2.3     skrll 	}
   1665       1.7    nonaka 
   1666       1.1    nonaka 	/*
   1667       1.1    nonaka 	 * Start a CPU data transfer.  Writing to the high order byte
   1668       1.1    nonaka 	 * of the SDHC_COMMAND register triggers the SD command. (1.5)
   1669       1.1    nonaka 	 */
   1670      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   1671      1.11      matt 		HWRITE4(hp, SDHC_BLOCK_SIZE, blksize | (blkcount << 16));
   1672      1.11      matt 		HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
   1673      1.11      matt 		HWRITE4(hp, SDHC_TRANSFER_MODE, mode | (command << 16));
   1674      1.11      matt 	} else {
   1675      1.11      matt 		HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
   1676      1.15  jakllsch 		HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
   1677      1.11      matt 		HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
   1678      1.15  jakllsch 		HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
   1679      1.11      matt 		HWRITE2(hp, SDHC_COMMAND, command);
   1680      1.11      matt 	}
   1681       1.1    nonaka 
   1682       1.1    nonaka 	return 0;
   1683       1.1    nonaka }
   1684       1.1    nonaka 
   1685       1.1    nonaka static void
   1686       1.1    nonaka sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1687       1.1    nonaka {
   1688      1.51  jmcneill 	struct sdhc_softc *sc = hp->sc;
   1689       1.1    nonaka 	int error;
   1690       1.1    nonaka 
   1691  1.51.2.3     skrll 	KASSERT(mutex_owned(&hp->intr_lock));
   1692  1.51.2.3     skrll 
   1693       1.1    nonaka 	DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp),
   1694       1.1    nonaka 	    MMC_R1(cmd->c_resp), cmd->c_datalen));
   1695       1.1    nonaka 
   1696       1.1    nonaka #ifdef SDHC_DEBUG
   1697       1.1    nonaka 	/* XXX I forgot why I wanted to know when this happens :-( */
   1698       1.1    nonaka 	if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
   1699       1.1    nonaka 	    ISSET(MMC_R1(cmd->c_resp), 0xcb00)) {
   1700       1.1    nonaka 		aprint_error_dev(hp->sc->sc_dev,
   1701       1.1    nonaka 		    "CMD52/53 error response flags %#x\n",
   1702       1.1    nonaka 		    MMC_R1(cmd->c_resp) & 0xff00);
   1703       1.1    nonaka 	}
   1704       1.1    nonaka #endif
   1705       1.1    nonaka 
   1706      1.47     skrll 	if (cmd->c_dmamap != NULL) {
   1707      1.47     skrll 		if (hp->sc->sc_vendor_transfer_data_dma != NULL) {
   1708      1.51  jmcneill 			error = hp->sc->sc_vendor_transfer_data_dma(sc, cmd);
   1709      1.47     skrll 			if (error == 0 && !sdhc_wait_intr(hp,
   1710  1.51.2.3     skrll 			    SDHC_TRANSFER_COMPLETE, SDHC_DMA_TIMEOUT)) {
   1711  1.51.2.3     skrll 				DPRINTF(1,("%s: timeout\n", __func__));
   1712      1.47     skrll 				error = ETIMEDOUT;
   1713      1.47     skrll 			}
   1714      1.47     skrll 		} else {
   1715      1.47     skrll 			error = sdhc_transfer_data_dma(hp, cmd);
   1716      1.47     skrll 		}
   1717      1.47     skrll 	} else
   1718       1.7    nonaka 		error = sdhc_transfer_data_pio(hp, cmd);
   1719       1.1    nonaka 	if (error)
   1720       1.1    nonaka 		cmd->c_error = error;
   1721       1.1    nonaka 	SET(cmd->c_flags, SCF_ITSDONE);
   1722       1.1    nonaka 
   1723       1.1    nonaka 	DPRINTF(1,("%s: data transfer done (error=%d)\n",
   1724       1.1    nonaka 	    HDEVNAME(hp), cmd->c_error));
   1725       1.1    nonaka }
   1726       1.1    nonaka 
   1727       1.1    nonaka static int
   1728       1.7    nonaka sdhc_transfer_data_dma(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1729       1.7    nonaka {
   1730      1.19  jakllsch 	bus_dma_segment_t *dm_segs = cmd->c_dmamap->dm_segs;
   1731      1.19  jakllsch 	bus_addr_t posaddr;
   1732      1.19  jakllsch 	bus_addr_t segaddr;
   1733      1.19  jakllsch 	bus_size_t seglen;
   1734      1.19  jakllsch 	u_int seg = 0;
   1735       1.7    nonaka 	int error = 0;
   1736      1.19  jakllsch 	int status;
   1737       1.7    nonaka 
   1738  1.51.2.3     skrll 	KASSERT(mutex_owned(&hp->intr_lock));
   1739      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_DMA_INTERRUPT);
   1740      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_DMA_INTERRUPT);
   1741      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
   1742      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
   1743      1.11      matt 
   1744       1.7    nonaka 	for (;;) {
   1745      1.19  jakllsch 		status = sdhc_wait_intr(hp,
   1746       1.7    nonaka 		    SDHC_DMA_INTERRUPT|SDHC_TRANSFER_COMPLETE,
   1747      1.19  jakllsch 		    SDHC_DMA_TIMEOUT);
   1748      1.19  jakllsch 
   1749      1.19  jakllsch 		if (status & SDHC_TRANSFER_COMPLETE) {
   1750      1.19  jakllsch 			break;
   1751      1.19  jakllsch 		}
   1752      1.19  jakllsch 		if (!status) {
   1753  1.51.2.3     skrll 			DPRINTF(1,("%s: timeout\n", __func__));
   1754       1.7    nonaka 			error = ETIMEDOUT;
   1755       1.7    nonaka 			break;
   1756       1.7    nonaka 		}
   1757  1.51.2.3     skrll 
   1758  1.51.2.3     skrll 		if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
   1759  1.51.2.3     skrll 			continue;
   1760  1.51.2.3     skrll 		}
   1761  1.51.2.3     skrll 
   1762      1.19  jakllsch 		if ((status & SDHC_DMA_INTERRUPT) == 0) {
   1763      1.19  jakllsch 			continue;
   1764      1.19  jakllsch 		}
   1765      1.19  jakllsch 
   1766      1.19  jakllsch 		/* DMA Interrupt (boundary crossing) */
   1767       1.7    nonaka 
   1768      1.19  jakllsch 		segaddr = dm_segs[seg].ds_addr;
   1769      1.19  jakllsch 		seglen = dm_segs[seg].ds_len;
   1770      1.19  jakllsch 		posaddr = HREAD4(hp, SDHC_DMA_ADDR);
   1771       1.7    nonaka 
   1772      1.19  jakllsch 		if ((seg == (cmd->c_dmamap->dm_nsegs-1)) && (posaddr == (segaddr + seglen))) {
   1773      1.37  jakllsch 			continue;
   1774      1.19  jakllsch 		}
   1775      1.19  jakllsch 		if ((posaddr >= segaddr) && (posaddr < (segaddr + seglen)))
   1776      1.19  jakllsch 			HWRITE4(hp, SDHC_DMA_ADDR, posaddr);
   1777      1.19  jakllsch 		else if ((posaddr >= segaddr) && (posaddr == (segaddr + seglen)) && (seg + 1) < cmd->c_dmamap->dm_nsegs)
   1778      1.19  jakllsch 			HWRITE4(hp, SDHC_DMA_ADDR, dm_segs[++seg].ds_addr);
   1779      1.19  jakllsch 		KASSERT(seg < cmd->c_dmamap->dm_nsegs);
   1780       1.7    nonaka 	}
   1781       1.7    nonaka 
   1782  1.51.2.3     skrll 	if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
   1783  1.51.2.3     skrll 		bus_dmamap_sync(hp->sc->sc_dmat, hp->adma_map, 0,
   1784  1.51.2.3     skrll 		    PAGE_SIZE, BUS_DMASYNC_POSTWRITE);
   1785  1.51.2.3     skrll 	}
   1786  1.51.2.3     skrll 
   1787       1.7    nonaka 	return error;
   1788       1.7    nonaka }
   1789       1.7    nonaka 
   1790       1.7    nonaka static int
   1791       1.1    nonaka sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1792       1.1    nonaka {
   1793       1.1    nonaka 	uint8_t *data = cmd->c_data;
   1794      1.12    nonaka 	void (*pio_func)(struct sdhc_host *, uint8_t *, u_int);
   1795      1.11      matt 	u_int len, datalen;
   1796      1.11      matt 	u_int imask;
   1797      1.11      matt 	u_int pmask;
   1798       1.1    nonaka 	int error = 0;
   1799       1.1    nonaka 
   1800  1.51.2.3     skrll 	KASSERT(mutex_owned(&hp->intr_lock));
   1801  1.51.2.3     skrll 
   1802      1.11      matt 	if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
   1803      1.11      matt 		imask = SDHC_BUFFER_READ_READY;
   1804      1.11      matt 		pmask = SDHC_BUFFER_READ_ENABLE;
   1805      1.11      matt 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1806      1.11      matt 			pio_func = esdhc_read_data_pio;
   1807      1.11      matt 		} else {
   1808      1.11      matt 			pio_func = sdhc_read_data_pio;
   1809      1.11      matt 		}
   1810      1.11      matt 	} else {
   1811      1.11      matt 		imask = SDHC_BUFFER_WRITE_READY;
   1812      1.11      matt 		pmask = SDHC_BUFFER_WRITE_ENABLE;
   1813      1.11      matt 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1814      1.11      matt 			pio_func = esdhc_write_data_pio;
   1815      1.11      matt 		} else {
   1816      1.11      matt 			pio_func = sdhc_write_data_pio;
   1817      1.11      matt 		}
   1818      1.11      matt 	}
   1819       1.1    nonaka 	datalen = cmd->c_datalen;
   1820       1.1    nonaka 
   1821  1.51.2.3     skrll 	KASSERT(mutex_owned(&hp->intr_lock));
   1822      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & imask);
   1823      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
   1824      1.11      matt 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
   1825      1.11      matt 
   1826       1.1    nonaka 	while (datalen > 0) {
   1827      1.11      matt 		if (!ISSET(HREAD4(hp, SDHC_PRESENT_STATE), imask)) {
   1828      1.11      matt 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   1829      1.11      matt 				HSET4(hp, SDHC_NINTR_SIGNAL_EN, imask);
   1830      1.11      matt 			} else {
   1831      1.11      matt 				HSET2(hp, SDHC_NINTR_SIGNAL_EN, imask);
   1832      1.11      matt 			}
   1833      1.11      matt 			if (!sdhc_wait_intr(hp, imask, SDHC_BUFFER_TIMEOUT)) {
   1834  1.51.2.3     skrll 				DPRINTF(1,("%s: timeout\n", __func__));
   1835      1.11      matt 				error = ETIMEDOUT;
   1836      1.11      matt 				break;
   1837      1.11      matt 			}
   1838      1.11      matt 
   1839      1.11      matt 			error = sdhc_wait_state(hp, pmask, pmask);
   1840      1.11      matt 			if (error)
   1841      1.11      matt 				break;
   1842       1.1    nonaka 		}
   1843       1.1    nonaka 
   1844       1.1    nonaka 		len = MIN(datalen, cmd->c_blklen);
   1845      1.11      matt 		(*pio_func)(hp, data, len);
   1846      1.11      matt 		DPRINTF(2,("%s: pio data transfer %u @ %p\n",
   1847      1.11      matt 		    HDEVNAME(hp), len, data));
   1848       1.1    nonaka 
   1849       1.1    nonaka 		data += len;
   1850       1.1    nonaka 		datalen -= len;
   1851       1.1    nonaka 	}
   1852       1.1    nonaka 
   1853       1.1    nonaka 	if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
   1854  1.51.2.3     skrll 	    SDHC_TRANSFER_TIMEOUT)) {
   1855  1.51.2.3     skrll 		DPRINTF(1,("%s: timeout for transfer\n", __func__));
   1856       1.1    nonaka 		error = ETIMEDOUT;
   1857  1.51.2.3     skrll 	}
   1858       1.1    nonaka 
   1859       1.1    nonaka 	return error;
   1860       1.1    nonaka }
   1861       1.1    nonaka 
   1862       1.1    nonaka static void
   1863      1.11      matt sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1864       1.1    nonaka {
   1865       1.1    nonaka 
   1866       1.1    nonaka 	if (((__uintptr_t)data & 3) == 0) {
   1867       1.1    nonaka 		while (datalen > 3) {
   1868      1.29      matt 			*(uint32_t *)data = le32toh(HREAD4(hp, SDHC_DATA));
   1869       1.1    nonaka 			data += 4;
   1870       1.1    nonaka 			datalen -= 4;
   1871       1.1    nonaka 		}
   1872       1.1    nonaka 		if (datalen > 1) {
   1873      1.29      matt 			*(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
   1874       1.1    nonaka 			data += 2;
   1875       1.1    nonaka 			datalen -= 2;
   1876       1.1    nonaka 		}
   1877       1.1    nonaka 		if (datalen > 0) {
   1878       1.1    nonaka 			*data = HREAD1(hp, SDHC_DATA);
   1879       1.1    nonaka 			data += 1;
   1880       1.1    nonaka 			datalen -= 1;
   1881       1.1    nonaka 		}
   1882       1.1    nonaka 	} else if (((__uintptr_t)data & 1) == 0) {
   1883       1.1    nonaka 		while (datalen > 1) {
   1884      1.29      matt 			*(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
   1885       1.1    nonaka 			data += 2;
   1886       1.1    nonaka 			datalen -= 2;
   1887       1.1    nonaka 		}
   1888       1.1    nonaka 		if (datalen > 0) {
   1889       1.1    nonaka 			*data = HREAD1(hp, SDHC_DATA);
   1890       1.1    nonaka 			data += 1;
   1891       1.1    nonaka 			datalen -= 1;
   1892       1.1    nonaka 		}
   1893       1.1    nonaka 	} else {
   1894       1.1    nonaka 		while (datalen > 0) {
   1895       1.1    nonaka 			*data = HREAD1(hp, SDHC_DATA);
   1896       1.1    nonaka 			data += 1;
   1897       1.1    nonaka 			datalen -= 1;
   1898       1.1    nonaka 		}
   1899       1.1    nonaka 	}
   1900       1.1    nonaka }
   1901       1.1    nonaka 
   1902       1.1    nonaka static void
   1903      1.11      matt sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1904       1.1    nonaka {
   1905       1.1    nonaka 
   1906       1.1    nonaka 	if (((__uintptr_t)data & 3) == 0) {
   1907       1.1    nonaka 		while (datalen > 3) {
   1908      1.29      matt 			HWRITE4(hp, SDHC_DATA, htole32(*(uint32_t *)data));
   1909       1.1    nonaka 			data += 4;
   1910       1.1    nonaka 			datalen -= 4;
   1911       1.1    nonaka 		}
   1912       1.1    nonaka 		if (datalen > 1) {
   1913      1.29      matt 			HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
   1914       1.1    nonaka 			data += 2;
   1915       1.1    nonaka 			datalen -= 2;
   1916       1.1    nonaka 		}
   1917       1.1    nonaka 		if (datalen > 0) {
   1918       1.1    nonaka 			HWRITE1(hp, SDHC_DATA, *data);
   1919       1.1    nonaka 			data += 1;
   1920       1.1    nonaka 			datalen -= 1;
   1921       1.1    nonaka 		}
   1922       1.1    nonaka 	} else if (((__uintptr_t)data & 1) == 0) {
   1923       1.1    nonaka 		while (datalen > 1) {
   1924      1.29      matt 			HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
   1925       1.1    nonaka 			data += 2;
   1926       1.1    nonaka 			datalen -= 2;
   1927       1.1    nonaka 		}
   1928       1.1    nonaka 		if (datalen > 0) {
   1929       1.1    nonaka 			HWRITE1(hp, SDHC_DATA, *data);
   1930       1.1    nonaka 			data += 1;
   1931       1.1    nonaka 			datalen -= 1;
   1932       1.1    nonaka 		}
   1933       1.1    nonaka 	} else {
   1934       1.1    nonaka 		while (datalen > 0) {
   1935       1.1    nonaka 			HWRITE1(hp, SDHC_DATA, *data);
   1936       1.1    nonaka 			data += 1;
   1937       1.1    nonaka 			datalen -= 1;
   1938       1.1    nonaka 		}
   1939       1.1    nonaka 	}
   1940       1.1    nonaka }
   1941       1.1    nonaka 
   1942      1.11      matt static void
   1943      1.11      matt esdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1944      1.11      matt {
   1945      1.11      matt 	uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
   1946      1.12    nonaka 	uint32_t v;
   1947      1.12    nonaka 
   1948      1.23      matt 	const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_READ_SHIFT) & SDHC_WATERMARK_READ_MASK;
   1949      1.23      matt 	size_t count = 0;
   1950      1.23      matt 
   1951      1.11      matt 	while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1952      1.23      matt 		if (count == 0) {
   1953      1.23      matt 			/*
   1954      1.23      matt 			 * If we've drained "watermark" words, we need to wait
   1955      1.23      matt 			 * a little bit so the read FIFO can refill.
   1956      1.23      matt 			 */
   1957      1.23      matt 			sdmmc_delay(10);
   1958      1.23      matt 			count = watermark;
   1959      1.23      matt 		}
   1960      1.12    nonaka 		v = HREAD4(hp, SDHC_DATA);
   1961      1.11      matt 		v = le32toh(v);
   1962      1.11      matt 		*(uint32_t *)data = v;
   1963      1.11      matt 		data += 4;
   1964      1.11      matt 		datalen -= 4;
   1965      1.11      matt 		status = HREAD2(hp, SDHC_NINTR_STATUS);
   1966      1.23      matt 		count--;
   1967      1.11      matt 	}
   1968      1.11      matt 	if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1969      1.23      matt 		if (count == 0) {
   1970      1.23      matt 			sdmmc_delay(10);
   1971      1.23      matt 		}
   1972      1.12    nonaka 		v = HREAD4(hp, SDHC_DATA);
   1973      1.11      matt 		v = le32toh(v);
   1974      1.11      matt 		do {
   1975      1.11      matt 			*data++ = v;
   1976      1.11      matt 			v >>= 8;
   1977      1.11      matt 		} while (--datalen > 0);
   1978      1.11      matt 	}
   1979      1.11      matt }
   1980      1.11      matt 
   1981      1.11      matt static void
   1982      1.11      matt esdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1983      1.11      matt {
   1984      1.11      matt 	uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
   1985      1.12    nonaka 	uint32_t v;
   1986      1.12    nonaka 
   1987      1.23      matt 	const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_WRITE_SHIFT) & SDHC_WATERMARK_WRITE_MASK;
   1988      1.23      matt 	size_t count = watermark;
   1989      1.23      matt 
   1990      1.11      matt 	while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1991      1.23      matt 		if (count == 0) {
   1992      1.23      matt 			sdmmc_delay(10);
   1993      1.23      matt 			count = watermark;
   1994      1.23      matt 		}
   1995      1.12    nonaka 		v = *(uint32_t *)data;
   1996      1.11      matt 		v = htole32(v);
   1997      1.11      matt 		HWRITE4(hp, SDHC_DATA, v);
   1998      1.11      matt 		data += 4;
   1999      1.11      matt 		datalen -= 4;
   2000      1.11      matt 		status = HREAD2(hp, SDHC_NINTR_STATUS);
   2001      1.23      matt 		count--;
   2002      1.11      matt 	}
   2003      1.11      matt 	if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   2004      1.23      matt 		if (count == 0) {
   2005      1.23      matt 			sdmmc_delay(10);
   2006      1.23      matt 		}
   2007      1.12    nonaka 		v = *(uint32_t *)data;
   2008      1.11      matt 		v = htole32(v);
   2009      1.11      matt 		HWRITE4(hp, SDHC_DATA, v);
   2010      1.11      matt 	}
   2011      1.11      matt }
   2012      1.11      matt 
   2013       1.1    nonaka /* Prepare for another command. */
   2014       1.1    nonaka static int
   2015       1.1    nonaka sdhc_soft_reset(struct sdhc_host *hp, int mask)
   2016       1.1    nonaka {
   2017       1.1    nonaka 	int timo;
   2018       1.1    nonaka 
   2019  1.51.2.3     skrll 	KASSERT(mutex_owned(&hp->intr_lock));
   2020  1.51.2.3     skrll 
   2021       1.1    nonaka 	DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask));
   2022       1.1    nonaka 
   2023      1.35  riastrad 	/* Request the reset.  */
   2024       1.1    nonaka 	HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
   2025      1.35  riastrad 
   2026      1.35  riastrad 	/*
   2027      1.35  riastrad 	 * If necessary, wait for the controller to set the bits to
   2028      1.35  riastrad 	 * acknowledge the reset.
   2029      1.35  riastrad 	 */
   2030      1.35  riastrad 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_WAIT_RESET) &&
   2031      1.35  riastrad 	    ISSET(mask, (SDHC_RESET_DAT | SDHC_RESET_CMD))) {
   2032      1.35  riastrad 		for (timo = 10000; timo > 0; timo--) {
   2033      1.35  riastrad 			if (ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
   2034      1.35  riastrad 				break;
   2035      1.35  riastrad 			/* Short delay because I worry we may miss it...  */
   2036      1.35  riastrad 			sdmmc_delay(1);
   2037      1.35  riastrad 		}
   2038      1.35  riastrad 		if (timo == 0)
   2039  1.51.2.3     skrll 			DPRINTF(1,("%s: timeout for reset on\n", __func__));
   2040      1.35  riastrad 			return ETIMEDOUT;
   2041      1.35  riastrad 	}
   2042      1.35  riastrad 
   2043      1.35  riastrad 	/*
   2044      1.35  riastrad 	 * Wait for the controller to clear the bits to indicate that
   2045      1.35  riastrad 	 * the reset has completed.
   2046      1.35  riastrad 	 */
   2047       1.1    nonaka 	for (timo = 10; timo > 0; timo--) {
   2048       1.1    nonaka 		if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
   2049       1.1    nonaka 			break;
   2050       1.1    nonaka 		sdmmc_delay(10000);
   2051       1.1    nonaka 	}
   2052       1.1    nonaka 	if (timo == 0) {
   2053       1.1    nonaka 		DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp),
   2054       1.1    nonaka 		    HREAD1(hp, SDHC_SOFTWARE_RESET)));
   2055       1.1    nonaka 		return ETIMEDOUT;
   2056       1.1    nonaka 	}
   2057       1.1    nonaka 
   2058      1.11      matt 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   2059  1.51.2.1     skrll 		HSET4(hp, SDHC_DMA_CTL, SDHC_DMA_SNOOP);
   2060      1.11      matt 	}
   2061      1.11      matt 
   2062       1.1    nonaka 	return 0;
   2063       1.1    nonaka }
   2064       1.1    nonaka 
   2065       1.1    nonaka static int
   2066       1.1    nonaka sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo)
   2067       1.1    nonaka {
   2068  1.51.2.3     skrll 	int status, error, nointr;
   2069  1.51.2.3     skrll 
   2070  1.51.2.3     skrll 	KASSERT(mutex_owned(&hp->intr_lock));
   2071       1.1    nonaka 
   2072       1.1    nonaka 	mask |= SDHC_ERROR_INTERRUPT;
   2073       1.1    nonaka 
   2074  1.51.2.3     skrll 	nointr = 0;
   2075       1.1    nonaka 	status = hp->intr_status & mask;
   2076       1.1    nonaka 	while (status == 0) {
   2077  1.51.2.3     skrll 		if (cv_timedwait(&hp->intr_cv, &hp->intr_lock, timo)
   2078       1.1    nonaka 		    == EWOULDBLOCK) {
   2079  1.51.2.3     skrll 			nointr = 1;
   2080       1.1    nonaka 			break;
   2081       1.1    nonaka 		}
   2082       1.1    nonaka 		status = hp->intr_status & mask;
   2083       1.1    nonaka 	}
   2084  1.51.2.3     skrll 	error = hp->intr_error_status;
   2085       1.1    nonaka 
   2086       1.1    nonaka 	DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status,
   2087  1.51.2.3     skrll 	    error));
   2088  1.51.2.3     skrll 
   2089  1.51.2.3     skrll 	hp->intr_status &= ~status;
   2090  1.51.2.3     skrll 	hp->intr_error_status &= ~error;
   2091      1.47     skrll 
   2092  1.51.2.3     skrll 	if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
   2093  1.51.2.3     skrll 		if (ISSET(error, SDHC_DMA_ERROR))
   2094  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"dma error\n");
   2095  1.51.2.3     skrll 		if (ISSET(error, SDHC_ADMA_ERROR))
   2096  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"adma error\n");
   2097  1.51.2.3     skrll 		if (ISSET(error, SDHC_AUTO_CMD12_ERROR))
   2098  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"auto_cmd12 error\n");
   2099  1.51.2.3     skrll 		if (ISSET(error, SDHC_CURRENT_LIMIT_ERROR))
   2100  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"current limit error\n");
   2101  1.51.2.3     skrll 		if (ISSET(error, SDHC_DATA_END_BIT_ERROR))
   2102  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"data end bit error\n");
   2103  1.51.2.3     skrll 		if (ISSET(error, SDHC_DATA_CRC_ERROR))
   2104  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"data crc error\n");
   2105  1.51.2.3     skrll 		if (ISSET(error, SDHC_DATA_TIMEOUT_ERROR))
   2106  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"data timeout error\n");
   2107  1.51.2.3     skrll 		if (ISSET(error, SDHC_CMD_INDEX_ERROR))
   2108  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"cmd index error\n");
   2109  1.51.2.3     skrll 		if (ISSET(error, SDHC_CMD_END_BIT_ERROR))
   2110  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"cmd end bit error\n");
   2111  1.51.2.3     skrll 		if (ISSET(error, SDHC_CMD_CRC_ERROR))
   2112  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"cmd crc error\n");
   2113  1.51.2.3     skrll 		if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR))
   2114  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"cmd timeout error\n");
   2115  1.51.2.3     skrll 		if ((error & ~SDHC_EINTR_STATUS_MASK) != 0)
   2116  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"vendor error %#x\n",
   2117  1.51.2.3     skrll 				(error & ~SDHC_EINTR_STATUS_MASK));
   2118  1.51.2.3     skrll 		if (error == 0)
   2119  1.51.2.3     skrll 			device_printf(hp->sc->sc_dev,"no error\n");
   2120  1.51.2.3     skrll 
   2121  1.51.2.3     skrll 		/* Command timeout has higher priority than command complete. */
   2122  1.51.2.3     skrll 		if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR))
   2123  1.51.2.3     skrll 			CLR(status, SDHC_COMMAND_COMPLETE);
   2124  1.51.2.3     skrll 
   2125  1.51.2.3     skrll 		/* Transfer complete has higher priority than data timeout. */
   2126  1.51.2.3     skrll 		if (ISSET(status, SDHC_TRANSFER_COMPLETE))
   2127  1.51.2.3     skrll 			CLR(error, SDHC_DATA_TIMEOUT_ERROR);
   2128  1.51.2.3     skrll 	}
   2129  1.51.2.3     skrll 
   2130  1.51.2.3     skrll 	if (nointr ||
   2131  1.51.2.3     skrll 	    (ISSET(status, SDHC_ERROR_INTERRUPT) && error)) {
   2132  1.51.2.3     skrll 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
   2133  1.51.2.3     skrll 			(void)sdhc_soft_reset(hp, SDHC_RESET_CMD|SDHC_RESET_DAT);
   2134       1.1    nonaka 		hp->intr_error_status = 0;
   2135       1.1    nonaka 		status = 0;
   2136       1.1    nonaka 	}
   2137       1.1    nonaka 
   2138       1.1    nonaka 	return status;
   2139       1.1    nonaka }
   2140       1.1    nonaka 
   2141       1.1    nonaka /*
   2142       1.1    nonaka  * Established by attachment driver at interrupt priority IPL_SDMMC.
   2143       1.1    nonaka  */
   2144       1.1    nonaka int
   2145       1.1    nonaka sdhc_intr(void *arg)
   2146       1.1    nonaka {
   2147       1.1    nonaka 	struct sdhc_softc *sc = (struct sdhc_softc *)arg;
   2148       1.1    nonaka 	struct sdhc_host *hp;
   2149       1.1    nonaka 	int done = 0;
   2150       1.1    nonaka 	uint16_t status;
   2151       1.1    nonaka 	uint16_t error;
   2152       1.1    nonaka 
   2153       1.1    nonaka 	/* We got an interrupt, but we don't know from which slot. */
   2154      1.11      matt 	for (size_t host = 0; host < sc->sc_nhosts; host++) {
   2155       1.1    nonaka 		hp = sc->sc_host[host];
   2156       1.1    nonaka 		if (hp == NULL)
   2157       1.1    nonaka 			continue;
   2158       1.1    nonaka 
   2159  1.51.2.3     skrll 		mutex_enter(&hp->intr_lock);
   2160  1.51.2.3     skrll 
   2161      1.11      matt 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   2162      1.11      matt 			/* Find out which interrupts are pending. */
   2163      1.11      matt 			uint32_t xstatus = HREAD4(hp, SDHC_NINTR_STATUS);
   2164      1.11      matt 			status = xstatus;
   2165      1.11      matt 			error = xstatus >> 16;
   2166  1.51.2.3     skrll 			if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   2167  1.51.2.3     skrll 				if ((error & SDHC_NINTR_STATUS_MASK) != 0)
   2168  1.51.2.3     skrll 					SET(status, SDHC_ERROR_INTERRUPT);
   2169  1.51.2.3     skrll 			}
   2170      1.22      matt 			if (error)
   2171      1.22      matt 				xstatus |= SDHC_ERROR_INTERRUPT;
   2172      1.22      matt 			else if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   2173  1.51.2.3     skrll 				goto next_port; /* no interrupt for us */
   2174      1.11      matt 			/* Acknowledge the interrupts we are about to handle. */
   2175      1.11      matt 			HWRITE4(hp, SDHC_NINTR_STATUS, xstatus);
   2176      1.11      matt 		} else {
   2177      1.11      matt 			/* Find out which interrupts are pending. */
   2178      1.11      matt 			error = 0;
   2179      1.11      matt 			status = HREAD2(hp, SDHC_NINTR_STATUS);
   2180      1.11      matt 			if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   2181  1.51.2.3     skrll 				goto next_port; /* no interrupt for us */
   2182      1.11      matt 			/* Acknowledge the interrupts we are about to handle. */
   2183      1.11      matt 			HWRITE2(hp, SDHC_NINTR_STATUS, status);
   2184      1.11      matt 			if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
   2185      1.11      matt 				/* Acknowledge error interrupts. */
   2186      1.11      matt 				error = HREAD2(hp, SDHC_EINTR_STATUS);
   2187      1.11      matt 				HWRITE2(hp, SDHC_EINTR_STATUS, error);
   2188      1.11      matt 			}
   2189      1.11      matt 		}
   2190      1.47     skrll 
   2191      1.11      matt 		DPRINTF(2,("%s: interrupt status=%x error=%x\n", HDEVNAME(hp),
   2192      1.11      matt 		    status, error));
   2193       1.1    nonaka 
   2194       1.1    nonaka 		/* Claim this interrupt. */
   2195       1.1    nonaka 		done = 1;
   2196       1.1    nonaka 
   2197  1.51.2.3     skrll 		if (ISSET(status, SDHC_ERROR_INTERRUPT) &&
   2198  1.51.2.3     skrll 		    ISSET(error, SDHC_ADMA_ERROR)) {
   2199  1.51.2.3     skrll 			uint8_t adma_err = HREAD1(hp, SDHC_ADMA_ERROR_STATUS);
   2200  1.51.2.3     skrll 			printf("%s: ADMA error, status %02x\n", HDEVNAME(hp),
   2201  1.51.2.3     skrll 			    adma_err);
   2202       1.1    nonaka 		}
   2203       1.1    nonaka 
   2204       1.1    nonaka 		/*
   2205       1.1    nonaka 		 * Wake up the sdmmc event thread to scan for cards.
   2206       1.1    nonaka 		 */
   2207       1.9      matt 		if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)) {
   2208      1.46  jakllsch 			if (hp->sdmmc != NULL) {
   2209      1.46  jakllsch 				sdmmc_needs_discover(hp->sdmmc);
   2210      1.46  jakllsch 			}
   2211      1.11      matt 			if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   2212      1.11      matt 				HCLR4(hp, SDHC_NINTR_STATUS_EN,
   2213      1.11      matt 				    status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
   2214      1.11      matt 				HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
   2215      1.11      matt 				    status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
   2216      1.11      matt 			}
   2217       1.9      matt 		}
   2218       1.1    nonaka 
   2219       1.1    nonaka 		/*
   2220  1.51.2.3     skrll 		 * Schedule re-tuning process (UHS).
   2221  1.51.2.3     skrll 		 */
   2222  1.51.2.3     skrll 		if (ISSET(status, SDHC_RETUNING_EVENT)) {
   2223  1.51.2.3     skrll 			atomic_swap_uint(&hp->tuning_timer_pending, 1);
   2224  1.51.2.3     skrll 		}
   2225  1.51.2.3     skrll 
   2226  1.51.2.3     skrll 		/*
   2227       1.1    nonaka 		 * Wake up the blocking process to service command
   2228       1.1    nonaka 		 * related interrupt(s).
   2229       1.1    nonaka 		 */
   2230  1.51.2.3     skrll 		if (ISSET(status, SDHC_COMMAND_COMPLETE|SDHC_ERROR_INTERRUPT|
   2231      1.11      matt 		    SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY|
   2232       1.1    nonaka 		    SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) {
   2233  1.51.2.3     skrll 			hp->intr_error_status |= error;
   2234       1.1    nonaka 			hp->intr_status |= status;
   2235      1.11      matt 			if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   2236      1.11      matt 				HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
   2237      1.11      matt 				    status & (SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY));
   2238      1.11      matt 			}
   2239       1.1    nonaka 			cv_broadcast(&hp->intr_cv);
   2240       1.1    nonaka 		}
   2241       1.1    nonaka 
   2242       1.1    nonaka 		/*
   2243       1.1    nonaka 		 * Service SD card interrupts.
   2244       1.1    nonaka 		 */
   2245      1.11      matt 		if (!ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)
   2246      1.11      matt 		    && ISSET(status, SDHC_CARD_INTERRUPT)) {
   2247       1.1    nonaka 			DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp)));
   2248       1.1    nonaka 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   2249       1.1    nonaka 			sdmmc_card_intr(hp->sdmmc);
   2250       1.1    nonaka 		}
   2251  1.51.2.3     skrll next_port:
   2252  1.51.2.3     skrll 		mutex_exit(&hp->intr_lock);
   2253       1.1    nonaka 	}
   2254       1.1    nonaka 
   2255       1.1    nonaka 	return done;
   2256       1.1    nonaka }
   2257       1.1    nonaka 
   2258  1.51.2.3     skrll kmutex_t *
   2259  1.51.2.3     skrll sdhc_host_lock(struct sdhc_host *hp)
   2260  1.51.2.3     skrll {
   2261  1.51.2.3     skrll 	return &hp->intr_lock;
   2262  1.51.2.3     skrll }
   2263  1.51.2.3     skrll 
   2264       1.1    nonaka #ifdef SDHC_DEBUG
   2265       1.1    nonaka void
   2266       1.1    nonaka sdhc_dump_regs(struct sdhc_host *hp)
   2267       1.1    nonaka {
   2268       1.1    nonaka 
   2269       1.1    nonaka 	printf("0x%02x PRESENT_STATE:    %x\n", SDHC_PRESENT_STATE,
   2270       1.1    nonaka 	    HREAD4(hp, SDHC_PRESENT_STATE));
   2271      1.11      matt 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
   2272      1.11      matt 		printf("0x%02x POWER_CTL:        %x\n", SDHC_POWER_CTL,
   2273      1.11      matt 		    HREAD1(hp, SDHC_POWER_CTL));
   2274       1.1    nonaka 	printf("0x%02x NINTR_STATUS:     %x\n", SDHC_NINTR_STATUS,
   2275       1.1    nonaka 	    HREAD2(hp, SDHC_NINTR_STATUS));
   2276       1.1    nonaka 	printf("0x%02x EINTR_STATUS:     %x\n", SDHC_EINTR_STATUS,
   2277       1.1    nonaka 	    HREAD2(hp, SDHC_EINTR_STATUS));
   2278       1.1    nonaka 	printf("0x%02x NINTR_STATUS_EN:  %x\n", SDHC_NINTR_STATUS_EN,
   2279       1.1    nonaka 	    HREAD2(hp, SDHC_NINTR_STATUS_EN));
   2280       1.1    nonaka 	printf("0x%02x EINTR_STATUS_EN:  %x\n", SDHC_EINTR_STATUS_EN,
   2281       1.1    nonaka 	    HREAD2(hp, SDHC_EINTR_STATUS_EN));
   2282       1.1    nonaka 	printf("0x%02x NINTR_SIGNAL_EN:  %x\n", SDHC_NINTR_SIGNAL_EN,
   2283       1.1    nonaka 	    HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
   2284       1.1    nonaka 	printf("0x%02x EINTR_SIGNAL_EN:  %x\n", SDHC_EINTR_SIGNAL_EN,
   2285       1.1    nonaka 	    HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
   2286       1.1    nonaka 	printf("0x%02x CAPABILITIES:     %x\n", SDHC_CAPABILITIES,
   2287       1.1    nonaka 	    HREAD4(hp, SDHC_CAPABILITIES));
   2288       1.1    nonaka 	printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
   2289       1.1    nonaka 	    HREAD4(hp, SDHC_MAX_CAPABILITIES));
   2290       1.1    nonaka }
   2291       1.1    nonaka #endif
   2292