Home | History | Annotate | Line # | Download | only in sdmmc
sdmmc_io.c revision 1.16
      1  1.16  jmcneill /*	$NetBSD: sdmmc_io.c,v 1.16 2019/09/02 11:09:42 jmcneill Exp $	*/
      2   1.1    nonaka /*	$OpenBSD: sdmmc_io.c,v 1.10 2007/09/17 01:33:33 krw 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 /* Routines for SD I/O cards. */
     21   1.1    nonaka 
     22   1.1    nonaka #include <sys/cdefs.h>
     23  1.16  jmcneill __KERNEL_RCSID(0, "$NetBSD: sdmmc_io.c,v 1.16 2019/09/02 11:09:42 jmcneill Exp $");
     24   1.7      matt 
     25   1.7      matt #ifdef _KERNEL_OPT
     26   1.7      matt #include "opt_sdmmc.h"
     27   1.7      matt #endif
     28   1.1    nonaka 
     29   1.1    nonaka #include <sys/param.h>
     30   1.1    nonaka #include <sys/kernel.h>
     31   1.1    nonaka #include <sys/malloc.h>
     32   1.1    nonaka #include <sys/proc.h>
     33   1.1    nonaka #include <sys/systm.h>
     34   1.1    nonaka 
     35   1.1    nonaka #include <dev/sdmmc/sdmmc_ioreg.h>
     36   1.1    nonaka #include <dev/sdmmc/sdmmcchip.h>
     37   1.1    nonaka #include <dev/sdmmc/sdmmcreg.h>
     38   1.1    nonaka #include <dev/sdmmc/sdmmcvar.h>
     39   1.1    nonaka 
     40   1.1    nonaka #ifdef SDMMC_DEBUG
     41   1.1    nonaka #define DPRINTF(s)	do { printf s; } while (0)
     42   1.1    nonaka #else
     43   1.1    nonaka #define DPRINTF(s)	do {} while (0)
     44   1.1    nonaka #endif
     45   1.1    nonaka 
     46   1.1    nonaka struct sdmmc_intr_handler {
     47   1.1    nonaka 	struct sdmmc_softc *ih_softc;
     48   1.1    nonaka 	char *ih_name;
     49   1.1    nonaka 	int (*ih_fun)(void *);
     50   1.1    nonaka 	void *ih_arg;
     51   1.1    nonaka 	TAILQ_ENTRY(sdmmc_intr_handler) entry;
     52   1.1    nonaka };
     53   1.1    nonaka 
     54   1.1    nonaka static int	sdmmc_io_rw_direct(struct sdmmc_softc *,
     55   1.1    nonaka 		    struct sdmmc_function *, int, u_char *, int);
     56   1.1    nonaka static int	sdmmc_io_rw_extended(struct sdmmc_softc *,
     57   1.1    nonaka 		    struct sdmmc_function *, int, u_char *, int, int);
     58   1.1    nonaka #if 0
     59   1.1    nonaka static int	sdmmc_io_xchg(struct sdmmc_softc *, struct sdmmc_function *,
     60   1.1    nonaka 		    int, u_char *);
     61   1.1    nonaka #endif
     62   1.1    nonaka static void	sdmmc_io_reset(struct sdmmc_softc *);
     63   1.1    nonaka static int	sdmmc_io_send_op_cond(struct sdmmc_softc *, uint32_t,
     64   1.1    nonaka 		    uint32_t *);
     65   1.1    nonaka 
     66   1.1    nonaka /*
     67   1.1    nonaka  * Initialize SD I/O card functions (before memory cards).  The host
     68   1.1    nonaka  * system and controller must support card interrupts in order to use
     69   1.1    nonaka  * I/O functions.
     70   1.1    nonaka  */
     71   1.1    nonaka int
     72   1.1    nonaka sdmmc_io_enable(struct sdmmc_softc *sc)
     73   1.1    nonaka {
     74   1.1    nonaka 	uint32_t host_ocr;
     75   1.1    nonaka 	uint32_t card_ocr;
     76   1.1    nonaka 	int error;
     77   1.1    nonaka 
     78   1.1    nonaka 	SDMMC_LOCK(sc);
     79   1.1    nonaka 
     80   1.1    nonaka 	/* Set host mode to SD "combo" card. */
     81   1.1    nonaka 	SET(sc->sc_flags, SMF_SD_MODE|SMF_IO_MODE|SMF_MEM_MODE);
     82   1.1    nonaka 
     83   1.1    nonaka 	/* Reset I/O functions. */
     84   1.1    nonaka 	sdmmc_io_reset(sc);
     85   1.1    nonaka 
     86   1.1    nonaka 	/*
     87   1.1    nonaka 	 * Read the I/O OCR value, determine the number of I/O
     88   1.1    nonaka 	 * functions and whether memory is also present (a "combo
     89   1.1    nonaka 	 * card") by issuing CMD5.  SD memory-only and MMC cards
     90   1.1    nonaka 	 * do not respond to CMD5.
     91   1.1    nonaka 	 */
     92   1.1    nonaka 	error = sdmmc_io_send_op_cond(sc, 0, &card_ocr);
     93   1.1    nonaka 	if (error) {
     94   1.1    nonaka 		/* No SDIO card; switch to SD memory-only mode. */
     95   1.1    nonaka 		CLR(sc->sc_flags, SMF_IO_MODE);
     96   1.1    nonaka 		error = 0;
     97   1.1    nonaka 		goto out;
     98   1.1    nonaka 	}
     99   1.1    nonaka 
    100   1.1    nonaka 	/* Parse the additional bits in the I/O OCR value. */
    101   1.1    nonaka 	if (!ISSET(card_ocr, SD_IO_OCR_MEM_PRESENT)) {
    102   1.1    nonaka 		/* SDIO card without memory (not a "combo card"). */
    103   1.1    nonaka 		DPRINTF(("%s: no memory present\n", SDMMCDEVNAME(sc)));
    104   1.1    nonaka 		CLR(sc->sc_flags, SMF_MEM_MODE);
    105   1.1    nonaka 	}
    106   1.1    nonaka 	sc->sc_function_count = SD_IO_OCR_NUM_FUNCTIONS(card_ocr);
    107   1.1    nonaka 	if (sc->sc_function_count == 0) {
    108   1.1    nonaka 		/* Useless SDIO card without any I/O functions. */
    109   1.1    nonaka 		DPRINTF(("%s: no I/O functions\n", SDMMCDEVNAME(sc)));
    110   1.1    nonaka 		CLR(sc->sc_flags, SMF_IO_MODE);
    111   1.1    nonaka 		error = 0;
    112   1.1    nonaka 		goto out;
    113   1.1    nonaka 	}
    114   1.1    nonaka 	card_ocr &= SD_IO_OCR_MASK;
    115   1.1    nonaka 
    116   1.1    nonaka 	/* Set the lowest voltage supported by the card and host. */
    117   1.1    nonaka 	host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
    118   1.1    nonaka 	error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
    119   1.1    nonaka 	if (error) {
    120   1.1    nonaka 		aprint_error_dev(sc->sc_dev,
    121   1.1    nonaka 		    "couldn't supply voltage requested by card\n");
    122   1.1    nonaka 		goto out;
    123   1.1    nonaka 	}
    124   1.1    nonaka 
    125   1.1    nonaka 	/* Send the new OCR value until all cards are ready. */
    126   1.1    nonaka 	error = sdmmc_io_send_op_cond(sc, host_ocr, NULL);
    127   1.1    nonaka 	if (error) {
    128   1.1    nonaka 		aprint_error_dev(sc->sc_dev, "couldn't send I/O OCR\n");
    129   1.1    nonaka 		goto out;
    130   1.1    nonaka 	}
    131   1.1    nonaka 
    132   1.1    nonaka out:
    133   1.1    nonaka 	SDMMC_UNLOCK(sc);
    134   1.1    nonaka 
    135   1.1    nonaka 	return error;
    136   1.1    nonaka }
    137   1.1    nonaka 
    138   1.1    nonaka /*
    139   1.1    nonaka  * Allocate sdmmc_function structures for SD card I/O function
    140   1.1    nonaka  * (including function 0).
    141   1.1    nonaka  */
    142   1.1    nonaka void
    143   1.1    nonaka sdmmc_io_scan(struct sdmmc_softc *sc)
    144   1.1    nonaka {
    145   1.1    nonaka 	struct sdmmc_function *sf0, *sf;
    146   1.1    nonaka 	int error;
    147   1.1    nonaka 	int i;
    148   1.1    nonaka 
    149   1.1    nonaka 	SDMMC_LOCK(sc);
    150   1.1    nonaka 
    151   1.1    nonaka 	sf0 = sdmmc_function_alloc(sc);
    152   1.1    nonaka 	sf0->number = 0;
    153   1.1    nonaka 	error = sdmmc_set_relative_addr(sc, sf0);
    154   1.1    nonaka 	if (error) {
    155   1.1    nonaka 		aprint_error_dev(sc->sc_dev, "couldn't set I/O RCA\n");
    156   1.1    nonaka 		SET(sf0->flags, SFF_ERROR);
    157   1.1    nonaka 		goto out;
    158   1.1    nonaka 	}
    159   1.1    nonaka 	sc->sc_fn0 = sf0;
    160   1.1    nonaka 	SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf0, sf_list);
    161   1.1    nonaka 
    162   1.3  kiyohara 	/* Go to Data Transfer Mode, if possible. */
    163   1.3  kiyohara 	sdmmc_chip_bus_rod(sc->sc_sct, sc->sc_sch, 0);
    164   1.3  kiyohara 
    165   1.1    nonaka 	/* Verify that the RCA has been set by selecting the card. */
    166   1.1    nonaka 	error = sdmmc_select_card(sc, sf0);
    167   1.1    nonaka 	if (error) {
    168   1.1    nonaka 		aprint_error_dev(sc->sc_dev, "couldn't select I/O RCA %d\n",
    169   1.1    nonaka 		    sf0->rca);
    170   1.1    nonaka 		SET(sf0->flags, SFF_ERROR);
    171   1.1    nonaka 		goto out;
    172   1.1    nonaka 	}
    173   1.1    nonaka 
    174   1.1    nonaka 	for (i = 1; i <= sc->sc_function_count; i++) {
    175   1.1    nonaka 		sf = sdmmc_function_alloc(sc);
    176   1.1    nonaka 		sf->number = i;
    177   1.1    nonaka 		sf->rca = sf0->rca;
    178   1.1    nonaka 		SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
    179   1.1    nonaka 	}
    180   1.1    nonaka 
    181   1.1    nonaka out:
    182   1.1    nonaka 	SDMMC_UNLOCK(sc);
    183   1.1    nonaka }
    184   1.1    nonaka 
    185   1.1    nonaka /*
    186   1.1    nonaka  * Initialize SDIO card functions.
    187   1.1    nonaka  */
    188   1.1    nonaka int
    189   1.1    nonaka sdmmc_io_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    190   1.1    nonaka {
    191   1.5  kiyohara 	struct sdmmc_function *sf0 = sc->sc_fn0;
    192   1.1    nonaka 	int error = 0;
    193   1.5  kiyohara 	uint8_t reg;
    194   1.1    nonaka 
    195   1.1    nonaka 	SDMMC_LOCK(sc);
    196   1.1    nonaka 
    197  1.15   mlelstv 	sf->blklen = sdmmc_chip_host_maxblklen(sc->sc_sct, sc->sc_sch);
    198  1.15   mlelstv 
    199   1.1    nonaka 	if (sf->number == 0) {
    200   1.5  kiyohara 		reg = sdmmc_io_read_1(sf, SD_IO_CCCR_CAPABILITY);
    201   1.5  kiyohara 		if (!(reg & CCCR_CAPS_LSC) || (reg & CCCR_CAPS_4BLS)) {
    202   1.5  kiyohara 			sdmmc_io_write_1(sf, SD_IO_CCCR_BUS_WIDTH,
    203   1.5  kiyohara 			    CCCR_BUS_WIDTH_4);
    204   1.5  kiyohara 			sf->width = 4;
    205  1.13  jmcneill 			error = sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch,
    206  1.13  jmcneill 			    sf->width);
    207  1.13  jmcneill 			if (error)
    208  1.13  jmcneill 				aprint_error_dev(sc->sc_dev,
    209  1.13  jmcneill 				    "can't change bus width\n");
    210   1.5  kiyohara 		}
    211   1.1    nonaka 
    212   1.1    nonaka 		error = sdmmc_read_cis(sf, &sf->cis);
    213   1.1    nonaka 		if (error) {
    214   1.1    nonaka 			aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
    215   1.1    nonaka 			SET(sf->flags, SFF_ERROR);
    216   1.1    nonaka 			goto out;
    217   1.1    nonaka 		}
    218   1.1    nonaka 
    219   1.1    nonaka 		sdmmc_check_cis_quirks(sf);
    220   1.1    nonaka 
    221   1.1    nonaka #ifdef SDMMC_DEBUG
    222   1.1    nonaka 		if (sdmmcdebug)
    223   1.1    nonaka 			sdmmc_print_cis(sf);
    224   1.1    nonaka #endif
    225   1.3  kiyohara 
    226   1.5  kiyohara 		reg = sdmmc_io_read_1(sf, SD_IO_CCCR_HIGH_SPEED);
    227   1.5  kiyohara 		if (reg & CCCR_HIGH_SPEED_SHS) {
    228   1.5  kiyohara 			reg |= CCCR_HIGH_SPEED_EHS;
    229   1.5  kiyohara 			sdmmc_io_write_1(sf, SD_IO_CCCR_HIGH_SPEED, reg);
    230   1.5  kiyohara 			sf->csd.tran_speed = 50000;	/* 50MHz */
    231   1.5  kiyohara 
    232   1.5  kiyohara 			/* Wait 400KHz x 8 clock */
    233   1.5  kiyohara 			delay(1);
    234   1.5  kiyohara 		}
    235   1.3  kiyohara 		if (sc->sc_busclk > sf->csd.tran_speed)
    236   1.3  kiyohara 			sc->sc_busclk = sf->csd.tran_speed;
    237   1.3  kiyohara 		error =
    238  1.10  jmcneill 		    sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, sc->sc_busclk,
    239  1.10  jmcneill 			false);
    240   1.3  kiyohara 		if (error)
    241   1.3  kiyohara 			aprint_error_dev(sc->sc_dev,
    242   1.3  kiyohara 			    "can't change bus clock\n");
    243   1.5  kiyohara 	} else {
    244   1.5  kiyohara 		reg = sdmmc_io_read_1(sf0, SD_IO_FBR(sf->number) + 0x000);
    245   1.5  kiyohara 		sf->interface = FBR_STD_FUNC_IF_CODE(reg);
    246   1.5  kiyohara 		if (sf->interface == 0x0f)
    247   1.5  kiyohara 			sf->interface =
    248   1.5  kiyohara 			    sdmmc_io_read_1(sf0, SD_IO_FBR(sf->number) + 0x001);
    249   1.5  kiyohara 		error = sdmmc_read_cis(sf, &sf->cis);
    250   1.5  kiyohara 		if (error) {
    251   1.5  kiyohara 			aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
    252   1.5  kiyohara 			SET(sf->flags, SFF_ERROR);
    253   1.5  kiyohara 			goto out;
    254   1.5  kiyohara 		}
    255   1.5  kiyohara 
    256   1.5  kiyohara 		sdmmc_check_cis_quirks(sf);
    257   1.5  kiyohara 
    258   1.5  kiyohara #ifdef SDMMC_DEBUG
    259   1.5  kiyohara 		if (sdmmcdebug)
    260   1.5  kiyohara 			sdmmc_print_cis(sf);
    261   1.5  kiyohara #endif
    262   1.1    nonaka 	}
    263   1.1    nonaka 
    264   1.1    nonaka out:
    265   1.1    nonaka 	SDMMC_UNLOCK(sc);
    266   1.1    nonaka 
    267   1.1    nonaka 	return error;
    268   1.1    nonaka }
    269   1.1    nonaka 
    270   1.1    nonaka /*
    271   1.1    nonaka  * Indicate whether the function is ready to operate.
    272   1.1    nonaka  */
    273   1.1    nonaka static int
    274   1.1    nonaka sdmmc_io_function_ready(struct sdmmc_function *sf)
    275   1.1    nonaka {
    276   1.1    nonaka 	struct sdmmc_softc *sc = sf->sc;
    277   1.1    nonaka 	struct sdmmc_function *sf0 = sc->sc_fn0;
    278   1.1    nonaka 	uint8_t reg;
    279   1.1    nonaka 
    280   1.1    nonaka 	if (sf->number == 0)
    281   1.1    nonaka 		return 1;	/* FN0 is always ready */
    282   1.1    nonaka 
    283   1.1    nonaka 	SDMMC_LOCK(sc);
    284   1.1    nonaka 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_IOREADY);
    285   1.1    nonaka 	SDMMC_UNLOCK(sc);
    286   1.1    nonaka 	return (reg & (1 << sf->number)) != 0;
    287   1.1    nonaka }
    288   1.1    nonaka 
    289   1.1    nonaka int
    290   1.1    nonaka sdmmc_io_function_enable(struct sdmmc_function *sf)
    291   1.1    nonaka {
    292   1.1    nonaka 	struct sdmmc_softc *sc = sf->sc;
    293   1.1    nonaka 	struct sdmmc_function *sf0 = sc->sc_fn0;
    294   1.1    nonaka 	uint8_t reg;
    295   1.1    nonaka 	int retry;
    296   1.1    nonaka 
    297   1.1    nonaka 	if (sf->number == 0)
    298   1.1    nonaka 		return 0;	/* FN0 is always enabled */
    299   1.1    nonaka 
    300   1.1    nonaka 	SDMMC_LOCK(sc);
    301   1.1    nonaka 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
    302   1.1    nonaka 	SET(reg, (1U << sf->number));
    303   1.1    nonaka 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
    304   1.1    nonaka 	SDMMC_UNLOCK(sc);
    305   1.4  kiyohara 
    306   1.1    nonaka 	retry = 5;
    307   1.1    nonaka 	while (!sdmmc_io_function_ready(sf) && retry-- > 0)
    308   1.2     pooka 		kpause("pause", false, hz, NULL);
    309   1.1    nonaka 	return (retry >= 0) ? 0 : ETIMEDOUT;
    310   1.1    nonaka }
    311   1.1    nonaka 
    312   1.1    nonaka /*
    313   1.1    nonaka  * Disable the I/O function.  Return zero if the function was
    314   1.1    nonaka  * disabled successfully.
    315   1.1    nonaka  */
    316   1.1    nonaka void
    317   1.1    nonaka sdmmc_io_function_disable(struct sdmmc_function *sf)
    318   1.1    nonaka {
    319   1.1    nonaka 	struct sdmmc_softc *sc = sf->sc;
    320   1.1    nonaka 	struct sdmmc_function *sf0 = sc->sc_fn0;
    321   1.1    nonaka 	uint8_t reg;
    322   1.1    nonaka 
    323   1.1    nonaka 	if (sf->number == 0)
    324   1.1    nonaka 		return;		/* FN0 is always enabled */
    325   1.1    nonaka 
    326   1.1    nonaka 	SDMMC_LOCK(sc);
    327   1.1    nonaka 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
    328   1.1    nonaka 	CLR(reg, (1U << sf->number));
    329   1.1    nonaka 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
    330   1.1    nonaka 	SDMMC_UNLOCK(sc);
    331   1.1    nonaka }
    332   1.1    nonaka 
    333   1.1    nonaka static int
    334   1.1    nonaka sdmmc_io_rw_direct(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    335   1.1    nonaka     int reg, u_char *datap, int arg)
    336   1.1    nonaka {
    337   1.1    nonaka 	struct sdmmc_command cmd;
    338   1.1    nonaka 	int error;
    339   1.1    nonaka 
    340   1.1    nonaka 	/* Don't lock */
    341   1.1    nonaka 
    342   1.1    nonaka 	/* Make sure the card is selected. */
    343   1.1    nonaka 	error = sdmmc_select_card(sc, sf);
    344   1.1    nonaka 	if (error)
    345   1.1    nonaka 		return error;
    346   1.1    nonaka 
    347   1.1    nonaka 	arg |= ((sf == NULL ? 0 : sf->number) & SD_ARG_CMD52_FUNC_MASK) <<
    348   1.1    nonaka 	    SD_ARG_CMD52_FUNC_SHIFT;
    349   1.1    nonaka 	arg |= (reg & SD_ARG_CMD52_REG_MASK) <<
    350   1.1    nonaka 	    SD_ARG_CMD52_REG_SHIFT;
    351   1.1    nonaka 	arg |= (*datap & SD_ARG_CMD52_DATA_MASK) <<
    352   1.1    nonaka 	    SD_ARG_CMD52_DATA_SHIFT;
    353   1.1    nonaka 
    354   1.1    nonaka 	memset(&cmd, 0, sizeof cmd);
    355   1.1    nonaka 	cmd.c_opcode = SD_IO_RW_DIRECT;
    356   1.1    nonaka 	cmd.c_arg = arg;
    357   1.1    nonaka 	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
    358   1.1    nonaka 
    359   1.1    nonaka 	error = sdmmc_mmc_command(sc, &cmd);
    360   1.1    nonaka 	*datap = SD_R5_DATA(cmd.c_resp);
    361   1.1    nonaka 
    362   1.1    nonaka 	return error;
    363   1.1    nonaka }
    364   1.1    nonaka 
    365   1.1    nonaka /*
    366   1.1    nonaka  * Useful values of `arg' to pass in are either SD_ARG_CMD53_READ or
    367   1.1    nonaka  * SD_ARG_CMD53_WRITE.  SD_ARG_CMD53_INCREMENT may be ORed into `arg'
    368   1.1    nonaka  * to access successive register locations instead of accessing the
    369   1.1    nonaka  * same register many times.
    370   1.1    nonaka  */
    371   1.1    nonaka static int
    372   1.1    nonaka sdmmc_io_rw_extended(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    373   1.1    nonaka     int reg, u_char *datap, int datalen, int arg)
    374   1.1    nonaka {
    375   1.1    nonaka 	struct sdmmc_command cmd;
    376   1.1    nonaka 	int error;
    377   1.1    nonaka 
    378   1.1    nonaka 	/* Don't lock */
    379   1.1    nonaka 
    380   1.1    nonaka #if 0
    381   1.1    nonaka 	/* Make sure the card is selected. */
    382   1.1    nonaka 	error = sdmmc_select_card(sc, sf);
    383   1.1    nonaka 	if (error)
    384   1.1    nonaka 		return error;
    385   1.1    nonaka #endif
    386   1.1    nonaka 
    387   1.1    nonaka 	arg |= (((sf == NULL) ? 0 : sf->number) & SD_ARG_CMD53_FUNC_MASK) <<
    388   1.1    nonaka 	    SD_ARG_CMD53_FUNC_SHIFT;
    389   1.1    nonaka 	arg |= (reg & SD_ARG_CMD53_REG_MASK) <<
    390   1.1    nonaka 	    SD_ARG_CMD53_REG_SHIFT;
    391   1.1    nonaka 	arg |= (datalen & SD_ARG_CMD53_LENGTH_MASK) <<
    392   1.1    nonaka 	    SD_ARG_CMD53_LENGTH_SHIFT;
    393   1.1    nonaka 
    394   1.1    nonaka 	memset(&cmd, 0, sizeof cmd);
    395   1.1    nonaka 	cmd.c_opcode = SD_IO_RW_EXTENDED;
    396   1.1    nonaka 	cmd.c_arg = arg;
    397  1.16  jmcneill 	cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R5;
    398   1.1    nonaka 	cmd.c_data = datap;
    399   1.1    nonaka 	cmd.c_datalen = datalen;
    400  1.15   mlelstv 	cmd.c_blklen = MIN(datalen, sf->blklen);
    401  1.15   mlelstv 
    402   1.1    nonaka 	if (!ISSET(arg, SD_ARG_CMD53_WRITE))
    403   1.1    nonaka 		cmd.c_flags |= SCF_CMD_READ;
    404   1.1    nonaka 
    405   1.1    nonaka 	error = sdmmc_mmc_command(sc, &cmd);
    406   1.1    nonaka 
    407   1.1    nonaka 	return error;
    408   1.1    nonaka }
    409   1.1    nonaka 
    410   1.1    nonaka uint8_t
    411   1.1    nonaka sdmmc_io_read_1(struct sdmmc_function *sf, int reg)
    412   1.1    nonaka {
    413   1.1    nonaka 	uint8_t data = 0;
    414   1.1    nonaka 
    415   1.1    nonaka 	/* Don't lock */
    416   1.4  kiyohara 
    417   1.1    nonaka 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    418   1.1    nonaka 	    SD_ARG_CMD52_READ);
    419   1.1    nonaka 	return data;
    420   1.1    nonaka }
    421   1.1    nonaka 
    422   1.1    nonaka void
    423   1.1    nonaka sdmmc_io_write_1(struct sdmmc_function *sf, int reg, uint8_t data)
    424   1.1    nonaka {
    425   1.1    nonaka 
    426   1.1    nonaka 	/* Don't lock */
    427   1.1    nonaka 
    428   1.1    nonaka 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    429   1.1    nonaka 	    SD_ARG_CMD52_WRITE);
    430   1.1    nonaka }
    431   1.1    nonaka 
    432   1.1    nonaka uint16_t
    433   1.1    nonaka sdmmc_io_read_2(struct sdmmc_function *sf, int reg)
    434   1.1    nonaka {
    435   1.1    nonaka 	uint16_t data = 0;
    436   1.1    nonaka 
    437   1.1    nonaka 	/* Don't lock */
    438   1.1    nonaka 
    439   1.1    nonaka 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    440   1.1    nonaka 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    441   1.1    nonaka 	return data;
    442   1.1    nonaka }
    443   1.1    nonaka 
    444   1.1    nonaka void
    445   1.1    nonaka sdmmc_io_write_2(struct sdmmc_function *sf, int reg, uint16_t data)
    446   1.1    nonaka {
    447   1.1    nonaka 
    448   1.1    nonaka 	/* Don't lock */
    449   1.1    nonaka 
    450   1.1    nonaka 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    451   1.1    nonaka 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    452   1.1    nonaka }
    453   1.1    nonaka 
    454   1.1    nonaka uint32_t
    455   1.1    nonaka sdmmc_io_read_4(struct sdmmc_function *sf, int reg)
    456   1.1    nonaka {
    457   1.1    nonaka 	uint32_t data = 0;
    458   1.1    nonaka 
    459   1.1    nonaka 	/* Don't lock */
    460   1.4  kiyohara 
    461   1.1    nonaka 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    462   1.1    nonaka 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    463   1.1    nonaka 	return data;
    464   1.1    nonaka }
    465   1.1    nonaka 
    466   1.1    nonaka void
    467   1.1    nonaka sdmmc_io_write_4(struct sdmmc_function *sf, int reg, uint32_t data)
    468   1.1    nonaka {
    469   1.1    nonaka 
    470   1.1    nonaka 	/* Don't lock */
    471   1.1    nonaka 
    472   1.1    nonaka 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    473   1.1    nonaka 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    474   1.1    nonaka }
    475   1.1    nonaka 
    476   1.1    nonaka 
    477   1.1    nonaka int
    478   1.1    nonaka sdmmc_io_read_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    479   1.1    nonaka     int datalen)
    480   1.1    nonaka {
    481  1.15   mlelstv 	int blocks, bytes, error = 0;
    482   1.1    nonaka 
    483   1.1    nonaka 	/* Don't lock */
    484   1.1    nonaka 
    485  1.15   mlelstv 	while (datalen >= sf->blklen) {
    486  1.15   mlelstv 		//blocks = imin(datalen / sf->blklen,
    487  1.15   mlelstv 		//              SD_ARG_CMD53_LENGTH_MAX);
    488  1.15   mlelstv 		blocks = 1;
    489  1.15   mlelstv 		bytes = blocks * sf->blklen;
    490   1.1    nonaka 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    491  1.15   mlelstv 		    bytes, SD_ARG_CMD53_READ);
    492   1.1    nonaka 		if (error)
    493   1.1    nonaka 			goto error;
    494  1.15   mlelstv 		data += bytes;
    495  1.15   mlelstv 		datalen -= bytes;
    496   1.1    nonaka 	}
    497   1.1    nonaka 
    498  1.15   mlelstv 	if (datalen)
    499  1.15   mlelstv 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    500  1.15   mlelstv 		    SD_ARG_CMD53_READ);
    501   1.1    nonaka error:
    502   1.1    nonaka 	return error;
    503   1.1    nonaka }
    504   1.1    nonaka 
    505   1.1    nonaka int
    506   1.1    nonaka sdmmc_io_write_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    507   1.1    nonaka     int datalen)
    508   1.1    nonaka {
    509  1.15   mlelstv 	int blocks, bytes, error = 0;
    510  1.15   mlelstv 
    511  1.15   mlelstv 	/* Don't lock */
    512  1.15   mlelstv 
    513  1.15   mlelstv 	while (datalen >= sf->blklen) {
    514  1.15   mlelstv 		//blocks = imin(datalen / sf->blklen,
    515  1.15   mlelstv 		//             SD_ARG_CMD53_LENGTH_MAX);
    516  1.15   mlelstv 		blocks = 1;
    517  1.15   mlelstv 		bytes = blocks * sf->blklen;
    518  1.15   mlelstv 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    519  1.15   mlelstv 		    bytes, SD_ARG_CMD53_WRITE);
    520  1.15   mlelstv 		if (error)
    521  1.15   mlelstv 			goto error;
    522  1.15   mlelstv 		data += bytes;
    523  1.15   mlelstv 		datalen -= bytes;
    524  1.15   mlelstv 	}
    525  1.15   mlelstv 
    526  1.15   mlelstv 	if (datalen)
    527  1.15   mlelstv 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    528  1.15   mlelstv 		    SD_ARG_CMD53_WRITE);
    529  1.15   mlelstv error:
    530  1.15   mlelstv 	return error;
    531  1.15   mlelstv }
    532  1.15   mlelstv 
    533  1.15   mlelstv 
    534  1.15   mlelstv int
    535  1.15   mlelstv sdmmc_io_read_region_1(struct sdmmc_function *sf, int reg, u_char *data,
    536  1.15   mlelstv     int datalen)
    537  1.15   mlelstv {
    538  1.15   mlelstv 	int blocks, bytes, error = 0;
    539  1.15   mlelstv 
    540  1.15   mlelstv 	/* Don't lock */
    541  1.15   mlelstv 
    542  1.15   mlelstv 	while (datalen >= sf->blklen) {
    543  1.15   mlelstv 		//blocks = imin(datalen / sf->blklen,
    544  1.15   mlelstv 		//              SD_ARG_CMD53_LENGTH_MAX);
    545  1.15   mlelstv 		blocks = 1;
    546  1.15   mlelstv 		bytes = blocks * sf->blklen;
    547  1.15   mlelstv 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    548  1.15   mlelstv 		    bytes, SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    549  1.15   mlelstv 		if (error)
    550  1.15   mlelstv 			goto error;
    551  1.15   mlelstv 		reg += bytes;
    552  1.15   mlelstv 		data += bytes;
    553  1.15   mlelstv 		datalen -= bytes;
    554  1.15   mlelstv 	}
    555  1.15   mlelstv 
    556  1.15   mlelstv 	if (datalen)
    557  1.15   mlelstv 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    558  1.15   mlelstv 		    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    559  1.15   mlelstv error:
    560  1.15   mlelstv 	return error;
    561  1.15   mlelstv }
    562  1.15   mlelstv 
    563  1.15   mlelstv int
    564  1.15   mlelstv sdmmc_io_write_region_1(struct sdmmc_function *sf, int reg, u_char *data,
    565  1.15   mlelstv     int datalen)
    566  1.15   mlelstv {
    567  1.15   mlelstv 	int blocks, bytes, error = 0;
    568   1.1    nonaka 
    569   1.1    nonaka 	/* Don't lock */
    570   1.1    nonaka 
    571  1.15   mlelstv 	while (datalen >= sf->blklen) {
    572  1.15   mlelstv 		//blocks = imin(datalen / sf->blklen,
    573  1.15   mlelstv 		//              SD_ARG_CMD53_LENGTH_MAX);
    574  1.15   mlelstv 		blocks = 1;
    575  1.15   mlelstv 		bytes = blocks * sf->blklen;
    576   1.1    nonaka 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    577  1.15   mlelstv 		    bytes, SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    578   1.1    nonaka 		if (error)
    579   1.1    nonaka 			goto error;
    580  1.15   mlelstv 		reg += bytes;
    581  1.15   mlelstv 		data += bytes;
    582  1.15   mlelstv 		datalen -= bytes;
    583   1.1    nonaka 	}
    584   1.1    nonaka 
    585  1.15   mlelstv 	if (datalen)
    586  1.15   mlelstv 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    587  1.15   mlelstv 		    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    588   1.1    nonaka error:
    589   1.1    nonaka 	return error;
    590   1.1    nonaka }
    591   1.1    nonaka 
    592   1.1    nonaka #if 0
    593   1.1    nonaka static int
    594   1.1    nonaka sdmmc_io_xchg(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    595   1.1    nonaka     int reg, u_char *datap)
    596   1.1    nonaka {
    597   1.1    nonaka 
    598   1.1    nonaka 	/* Don't lock */
    599   1.1    nonaka 
    600   1.1    nonaka 	return sdmmc_io_rw_direct(sc, sf, reg, datap,
    601   1.1    nonaka 	    SD_ARG_CMD52_WRITE|SD_ARG_CMD52_EXCHANGE);
    602   1.1    nonaka }
    603   1.1    nonaka #endif
    604   1.1    nonaka 
    605   1.1    nonaka /*
    606   1.1    nonaka  * Reset the I/O functions of the card.
    607   1.1    nonaka  */
    608   1.1    nonaka static void
    609   1.1    nonaka sdmmc_io_reset(struct sdmmc_softc *sc)
    610   1.1    nonaka {
    611  1.13  jmcneill 	u_char data = CCCR_CTL_RES;
    612   1.1    nonaka 
    613  1.15   mlelstv 	if (sdmmc_io_rw_direct(sc, NULL, SD_IO_CCCR_CTL, &data,
    614  1.15   mlelstv 	    SD_ARG_CMD52_WRITE) == 0)
    615  1.13  jmcneill 		sdmmc_delay(100000);
    616   1.1    nonaka }
    617   1.1    nonaka 
    618   1.1    nonaka /*
    619   1.1    nonaka  * Get or set the card's I/O OCR value (SDIO).
    620   1.1    nonaka  */
    621   1.1    nonaka static int
    622   1.1    nonaka sdmmc_io_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, u_int32_t *ocrp)
    623   1.1    nonaka {
    624   1.1    nonaka 	struct sdmmc_command cmd;
    625   1.1    nonaka 	int error;
    626   1.1    nonaka 	int retry;
    627   1.1    nonaka 
    628   1.1    nonaka 	DPRINTF(("sdmmc_io_send_op_cond: ocr = %#x\n", ocr));
    629   1.1    nonaka 
    630   1.1    nonaka 	/* Don't lock */
    631   1.1    nonaka 
    632   1.1    nonaka 	/*
    633   1.1    nonaka 	 * If we change the OCR value, retry the command until the OCR
    634   1.1    nonaka 	 * we receive in response has the "CARD BUSY" bit set, meaning
    635   1.1    nonaka 	 * that all cards are ready for identification.
    636   1.1    nonaka 	 */
    637   1.1    nonaka 	for (retry = 0; retry < 100; retry++) {
    638   1.1    nonaka 		memset(&cmd, 0, sizeof cmd);
    639   1.1    nonaka 		cmd.c_opcode = SD_IO_SEND_OP_COND;
    640   1.1    nonaka 		cmd.c_arg = ocr;
    641  1.12   mlelstv 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R4 | SCF_TOUT_OK;
    642   1.1    nonaka 
    643   1.1    nonaka 		error = sdmmc_mmc_command(sc, &cmd);
    644   1.1    nonaka 		if (error)
    645   1.1    nonaka 			break;
    646   1.1    nonaka 		if (ISSET(MMC_R4(cmd.c_resp), SD_IO_OCR_MEM_READY) || ocr == 0)
    647   1.1    nonaka 			break;
    648   1.1    nonaka 
    649   1.1    nonaka 		error = ETIMEDOUT;
    650   1.1    nonaka 		sdmmc_delay(10000);
    651   1.1    nonaka 	}
    652   1.1    nonaka 	if (error == 0 && ocrp != NULL)
    653   1.1    nonaka 		*ocrp = MMC_R4(cmd.c_resp);
    654   1.1    nonaka 
    655   1.1    nonaka 	DPRINTF(("sdmmc_io_send_op_cond: error = %d\n", error));
    656   1.1    nonaka 
    657   1.1    nonaka 	return error;
    658   1.1    nonaka }
    659   1.1    nonaka 
    660   1.1    nonaka /*
    661   1.1    nonaka  * Card interrupt handling
    662   1.1    nonaka  */
    663   1.1    nonaka 
    664   1.1    nonaka void
    665   1.1    nonaka sdmmc_intr_enable(struct sdmmc_function *sf)
    666   1.1    nonaka {
    667   1.1    nonaka 	struct sdmmc_softc *sc = sf->sc;
    668   1.1    nonaka 	struct sdmmc_function *sf0 = sc->sc_fn0;
    669   1.1    nonaka 	uint8_t reg;
    670   1.1    nonaka 
    671   1.1    nonaka 	SDMMC_LOCK(sc);
    672   1.1    nonaka 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    673   1.1    nonaka 	reg |= 1 << sf->number;
    674   1.1    nonaka 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    675   1.1    nonaka 	SDMMC_UNLOCK(sc);
    676   1.1    nonaka }
    677   1.1    nonaka 
    678   1.1    nonaka void
    679   1.1    nonaka sdmmc_intr_disable(struct sdmmc_function *sf)
    680   1.1    nonaka {
    681   1.1    nonaka 	struct sdmmc_softc *sc = sf->sc;
    682   1.1    nonaka 	struct sdmmc_function *sf0 = sc->sc_fn0;
    683   1.1    nonaka 	uint8_t reg;
    684   1.1    nonaka 
    685   1.1    nonaka 	SDMMC_LOCK(sc);
    686   1.1    nonaka 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    687   1.1    nonaka 	reg &= ~(1 << sf->number);
    688   1.1    nonaka 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    689   1.1    nonaka 	SDMMC_UNLOCK(sc);
    690   1.1    nonaka }
    691   1.1    nonaka 
    692   1.1    nonaka /*
    693   1.1    nonaka  * Establish a handler for the SDIO card interrupt.  Because the
    694   1.1    nonaka  * interrupt may be shared with different SDIO functions, multiple
    695   1.1    nonaka  * handlers can be established.
    696   1.1    nonaka  */
    697   1.1    nonaka void *
    698   1.1    nonaka sdmmc_intr_establish(device_t dev, int (*fun)(void *), void *arg,
    699   1.1    nonaka     const char *name)
    700   1.1    nonaka {
    701   1.1    nonaka 	struct sdmmc_softc *sc = device_private(dev);
    702   1.1    nonaka 	struct sdmmc_intr_handler *ih;
    703   1.1    nonaka 
    704   1.1    nonaka 	if (sc->sc_sct->card_enable_intr == NULL)
    705   1.1    nonaka 		return NULL;
    706   1.1    nonaka 
    707  1.14  jdolecek 	ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK|M_ZERO);
    708   1.1    nonaka 	if (ih == NULL)
    709   1.1    nonaka 		return NULL;
    710   1.1    nonaka 
    711  1.14  jdolecek 	ih->ih_name = malloc(strlen(name) + 1, M_DEVBUF, M_WAITOK|M_ZERO);
    712   1.1    nonaka 	if (ih->ih_name == NULL) {
    713   1.1    nonaka 		free(ih, M_DEVBUF);
    714   1.1    nonaka 		return NULL;
    715   1.1    nonaka 	}
    716   1.1    nonaka 	strlcpy(ih->ih_name, name, strlen(name));
    717   1.1    nonaka 	ih->ih_softc = sc;
    718   1.1    nonaka 	ih->ih_fun = fun;
    719   1.1    nonaka 	ih->ih_arg = arg;
    720   1.1    nonaka 
    721   1.9   mlelstv 	mutex_enter(&sc->sc_mtx);
    722   1.1    nonaka 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    723   1.1    nonaka 		sdmmc_intr_enable(sc->sc_fn0);
    724   1.1    nonaka 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 1);
    725   1.1    nonaka 	}
    726   1.1    nonaka 	TAILQ_INSERT_TAIL(&sc->sc_intrq, ih, entry);
    727   1.9   mlelstv 	mutex_exit(&sc->sc_mtx);
    728   1.1    nonaka 
    729   1.1    nonaka 	return ih;
    730   1.1    nonaka }
    731   1.1    nonaka 
    732   1.1    nonaka /*
    733   1.1    nonaka  * Disestablish the given handler.
    734   1.1    nonaka  */
    735   1.1    nonaka void
    736   1.1    nonaka sdmmc_intr_disestablish(void *cookie)
    737   1.1    nonaka {
    738   1.1    nonaka 	struct sdmmc_intr_handler *ih = cookie;
    739   1.1    nonaka 	struct sdmmc_softc *sc = ih->ih_softc;
    740   1.1    nonaka 
    741   1.1    nonaka 	if (sc->sc_sct->card_enable_intr == NULL)
    742   1.1    nonaka 		return;
    743   1.1    nonaka 
    744   1.9   mlelstv 	mutex_enter(&sc->sc_mtx);
    745   1.1    nonaka 	TAILQ_REMOVE(&sc->sc_intrq, ih, entry);
    746   1.1    nonaka 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    747   1.1    nonaka 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 0);
    748   1.1    nonaka 		sdmmc_intr_disable(sc->sc_fn0);
    749   1.1    nonaka 	}
    750   1.9   mlelstv 	mutex_exit(&sc->sc_mtx);
    751   1.1    nonaka 
    752   1.1    nonaka 	free(ih->ih_name, M_DEVBUF);
    753   1.1    nonaka 	free(ih, M_DEVBUF);
    754   1.1    nonaka }
    755   1.1    nonaka 
    756   1.1    nonaka /*
    757   1.1    nonaka  * Call established SDIO card interrupt handlers.  The host controller
    758   1.1    nonaka  * must call this function from its own interrupt handler to handle an
    759   1.1    nonaka  * SDIO interrupt from the card.
    760   1.1    nonaka  */
    761   1.1    nonaka void
    762   1.1    nonaka sdmmc_card_intr(device_t dev)
    763   1.1    nonaka {
    764   1.1    nonaka 	struct sdmmc_softc *sc = device_private(dev);
    765   1.1    nonaka 
    766   1.8   mlelstv 	if (sc->sc_sct->card_enable_intr == NULL)
    767   1.8   mlelstv 		return;
    768   1.8   mlelstv 
    769   1.8   mlelstv 	mutex_enter(&sc->sc_intr_task_mtx);
    770   1.8   mlelstv 	if (!sdmmc_task_pending(&sc->sc_intr_task))
    771   1.8   mlelstv 		sdmmc_add_task(sc, &sc->sc_intr_task);
    772   1.8   mlelstv 	mutex_exit(&sc->sc_intr_task_mtx);
    773   1.1    nonaka }
    774   1.1    nonaka 
    775   1.1    nonaka void
    776   1.1    nonaka sdmmc_intr_task(void *arg)
    777   1.1    nonaka {
    778   1.1    nonaka 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    779   1.1    nonaka 	struct sdmmc_intr_handler *ih;
    780   1.1    nonaka 
    781   1.9   mlelstv 	mutex_enter(&sc->sc_mtx);
    782   1.1    nonaka 	TAILQ_FOREACH(ih, &sc->sc_intrq, entry) {
    783   1.1    nonaka 		/* XXX examine return value and do evcount stuff*/
    784   1.1    nonaka 		(void)(*ih->ih_fun)(ih->ih_arg);
    785   1.1    nonaka 	}
    786  1.11   mlelstv 	mutex_exit(&sc->sc_mtx);
    787  1.11   mlelstv 
    788   1.1    nonaka 	sdmmc_chip_card_intr_ack(sc->sc_sct, sc->sc_sch);
    789   1.1    nonaka }
    790  1.15   mlelstv 
    791  1.15   mlelstv int
    792  1.15   mlelstv sdmmc_io_set_blocklen(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    793  1.15   mlelstv      int blklen)
    794  1.15   mlelstv {
    795  1.15   mlelstv 	struct sdmmc_function *sf0 = sc->sc_fn0;
    796  1.15   mlelstv 	int error = EINVAL;
    797  1.15   mlelstv 
    798  1.15   mlelstv 	SDMMC_LOCK(sc);
    799  1.15   mlelstv 
    800  1.15   mlelstv 	if (blklen <= 0 ||
    801  1.15   mlelstv 	    blklen > sdmmc_chip_host_maxblklen(sc->sc_sct, sc->sc_sch))
    802  1.15   mlelstv 		goto err;
    803  1.15   mlelstv 
    804  1.15   mlelstv 	sdmmc_io_write_1(sf0, SD_IO_FBR(sf->number) +
    805  1.15   mlelstv 	    SD_IO_FBR_BLOCKLEN, blklen & 0xff);
    806  1.15   mlelstv 	sdmmc_io_write_1(sf0, SD_IO_FBR(sf->number) +
    807  1.15   mlelstv 	    SD_IO_FBR_BLOCKLEN + 1, (blklen >> 8) & 0xff);
    808  1.15   mlelstv 
    809  1.15   mlelstv 	sf->blklen = blklen;
    810  1.15   mlelstv 	error = 0;
    811  1.15   mlelstv 
    812  1.15   mlelstv err:
    813  1.15   mlelstv 	SDMMC_UNLOCK(sc);
    814  1.15   mlelstv 
    815  1.15   mlelstv 	return error;
    816  1.15   mlelstv }
    817