Home | History | Annotate | Line # | Download | only in sdmmc
sdmmc_io.c revision 1.2.6.2
      1  1.2.6.2  matt /*	$NetBSD: sdmmc_io.c,v 1.2.6.2 2010/04/21 00:27:52 matt Exp $	*/
      2  1.2.6.2  matt /*	$OpenBSD: sdmmc_io.c,v 1.10 2007/09/17 01:33:33 krw Exp $	*/
      3  1.2.6.2  matt 
      4  1.2.6.2  matt /*
      5  1.2.6.2  matt  * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
      6  1.2.6.2  matt  *
      7  1.2.6.2  matt  * Permission to use, copy, modify, and distribute this software for any
      8  1.2.6.2  matt  * purpose with or without fee is hereby granted, provided that the above
      9  1.2.6.2  matt  * copyright notice and this permission notice appear in all copies.
     10  1.2.6.2  matt  *
     11  1.2.6.2  matt  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  1.2.6.2  matt  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.2.6.2  matt  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  1.2.6.2  matt  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.2.6.2  matt  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.2.6.2  matt  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  1.2.6.2  matt  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.2.6.2  matt  */
     19  1.2.6.2  matt 
     20  1.2.6.2  matt /* Routines for SD I/O cards. */
     21  1.2.6.2  matt 
     22  1.2.6.2  matt #include <sys/cdefs.h>
     23  1.2.6.2  matt __KERNEL_RCSID(0, "$NetBSD: sdmmc_io.c,v 1.2.6.2 2010/04/21 00:27:52 matt Exp $");
     24  1.2.6.2  matt 
     25  1.2.6.2  matt #include <sys/param.h>
     26  1.2.6.2  matt #include <sys/kernel.h>
     27  1.2.6.2  matt #include <sys/malloc.h>
     28  1.2.6.2  matt #include <sys/proc.h>
     29  1.2.6.2  matt #include <sys/systm.h>
     30  1.2.6.2  matt 
     31  1.2.6.2  matt #include <dev/sdmmc/sdmmc_ioreg.h>
     32  1.2.6.2  matt #include <dev/sdmmc/sdmmcchip.h>
     33  1.2.6.2  matt #include <dev/sdmmc/sdmmcreg.h>
     34  1.2.6.2  matt #include <dev/sdmmc/sdmmcvar.h>
     35  1.2.6.2  matt 
     36  1.2.6.2  matt #ifdef SDMMC_DEBUG
     37  1.2.6.2  matt #define DPRINTF(s)	do { printf s; } while (0)
     38  1.2.6.2  matt #else
     39  1.2.6.2  matt #define DPRINTF(s)	do {} while (0)
     40  1.2.6.2  matt #endif
     41  1.2.6.2  matt 
     42  1.2.6.2  matt struct sdmmc_intr_handler {
     43  1.2.6.2  matt 	struct sdmmc_softc *ih_softc;
     44  1.2.6.2  matt 	char *ih_name;
     45  1.2.6.2  matt 	int (*ih_fun)(void *);
     46  1.2.6.2  matt 	void *ih_arg;
     47  1.2.6.2  matt 	TAILQ_ENTRY(sdmmc_intr_handler) entry;
     48  1.2.6.2  matt };
     49  1.2.6.2  matt 
     50  1.2.6.2  matt static int	sdmmc_io_rw_direct(struct sdmmc_softc *,
     51  1.2.6.2  matt 		    struct sdmmc_function *, int, u_char *, int);
     52  1.2.6.2  matt static int	sdmmc_io_rw_extended(struct sdmmc_softc *,
     53  1.2.6.2  matt 		    struct sdmmc_function *, int, u_char *, int, int);
     54  1.2.6.2  matt #if 0
     55  1.2.6.2  matt static int	sdmmc_io_xchg(struct sdmmc_softc *, struct sdmmc_function *,
     56  1.2.6.2  matt 		    int, u_char *);
     57  1.2.6.2  matt #endif
     58  1.2.6.2  matt static void	sdmmc_io_reset(struct sdmmc_softc *);
     59  1.2.6.2  matt static int	sdmmc_io_send_op_cond(struct sdmmc_softc *, uint32_t,
     60  1.2.6.2  matt 		    uint32_t *);
     61  1.2.6.2  matt 
     62  1.2.6.2  matt /*
     63  1.2.6.2  matt  * Initialize SD I/O card functions (before memory cards).  The host
     64  1.2.6.2  matt  * system and controller must support card interrupts in order to use
     65  1.2.6.2  matt  * I/O functions.
     66  1.2.6.2  matt  */
     67  1.2.6.2  matt int
     68  1.2.6.2  matt sdmmc_io_enable(struct sdmmc_softc *sc)
     69  1.2.6.2  matt {
     70  1.2.6.2  matt 	uint32_t host_ocr;
     71  1.2.6.2  matt 	uint32_t card_ocr;
     72  1.2.6.2  matt 	int error;
     73  1.2.6.2  matt 
     74  1.2.6.2  matt 	SDMMC_LOCK(sc);
     75  1.2.6.2  matt 
     76  1.2.6.2  matt 	/* Set host mode to SD "combo" card. */
     77  1.2.6.2  matt 	SET(sc->sc_flags, SMF_SD_MODE|SMF_IO_MODE|SMF_MEM_MODE);
     78  1.2.6.2  matt 
     79  1.2.6.2  matt 	/* Reset I/O functions. */
     80  1.2.6.2  matt 	sdmmc_io_reset(sc);
     81  1.2.6.2  matt 
     82  1.2.6.2  matt 	/*
     83  1.2.6.2  matt 	 * Read the I/O OCR value, determine the number of I/O
     84  1.2.6.2  matt 	 * functions and whether memory is also present (a "combo
     85  1.2.6.2  matt 	 * card") by issuing CMD5.  SD memory-only and MMC cards
     86  1.2.6.2  matt 	 * do not respond to CMD5.
     87  1.2.6.2  matt 	 */
     88  1.2.6.2  matt 	error = sdmmc_io_send_op_cond(sc, 0, &card_ocr);
     89  1.2.6.2  matt 	if (error) {
     90  1.2.6.2  matt 		/* No SDIO card; switch to SD memory-only mode. */
     91  1.2.6.2  matt 		CLR(sc->sc_flags, SMF_IO_MODE);
     92  1.2.6.2  matt 		error = 0;
     93  1.2.6.2  matt 		goto out;
     94  1.2.6.2  matt 	}
     95  1.2.6.2  matt 
     96  1.2.6.2  matt 	/* Parse the additional bits in the I/O OCR value. */
     97  1.2.6.2  matt 	if (!ISSET(card_ocr, SD_IO_OCR_MEM_PRESENT)) {
     98  1.2.6.2  matt 		/* SDIO card without memory (not a "combo card"). */
     99  1.2.6.2  matt 		DPRINTF(("%s: no memory present\n", SDMMCDEVNAME(sc)));
    100  1.2.6.2  matt 		CLR(sc->sc_flags, SMF_MEM_MODE);
    101  1.2.6.2  matt 	}
    102  1.2.6.2  matt 	sc->sc_function_count = SD_IO_OCR_NUM_FUNCTIONS(card_ocr);
    103  1.2.6.2  matt 	if (sc->sc_function_count == 0) {
    104  1.2.6.2  matt 		/* Useless SDIO card without any I/O functions. */
    105  1.2.6.2  matt 		DPRINTF(("%s: no I/O functions\n", SDMMCDEVNAME(sc)));
    106  1.2.6.2  matt 		CLR(sc->sc_flags, SMF_IO_MODE);
    107  1.2.6.2  matt 		error = 0;
    108  1.2.6.2  matt 		goto out;
    109  1.2.6.2  matt 	}
    110  1.2.6.2  matt 	card_ocr &= SD_IO_OCR_MASK;
    111  1.2.6.2  matt 
    112  1.2.6.2  matt 	/* Set the lowest voltage supported by the card and host. */
    113  1.2.6.2  matt 	host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
    114  1.2.6.2  matt 	error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
    115  1.2.6.2  matt 	if (error) {
    116  1.2.6.2  matt 		aprint_error_dev(sc->sc_dev,
    117  1.2.6.2  matt 		    "couldn't supply voltage requested by card\n");
    118  1.2.6.2  matt 		goto out;
    119  1.2.6.2  matt 	}
    120  1.2.6.2  matt 
    121  1.2.6.2  matt 	/* Reset I/O functions (again). */
    122  1.2.6.2  matt 	sdmmc_io_reset(sc);
    123  1.2.6.2  matt 
    124  1.2.6.2  matt 	/* Send the new OCR value until all cards are ready. */
    125  1.2.6.2  matt 	error = sdmmc_io_send_op_cond(sc, host_ocr, NULL);
    126  1.2.6.2  matt 	if (error) {
    127  1.2.6.2  matt 		aprint_error_dev(sc->sc_dev, "couldn't send I/O OCR\n");
    128  1.2.6.2  matt 		goto out;
    129  1.2.6.2  matt 	}
    130  1.2.6.2  matt 
    131  1.2.6.2  matt out:
    132  1.2.6.2  matt 	SDMMC_UNLOCK(sc);
    133  1.2.6.2  matt 
    134  1.2.6.2  matt 	return error;
    135  1.2.6.2  matt }
    136  1.2.6.2  matt 
    137  1.2.6.2  matt /*
    138  1.2.6.2  matt  * Allocate sdmmc_function structures for SD card I/O function
    139  1.2.6.2  matt  * (including function 0).
    140  1.2.6.2  matt  */
    141  1.2.6.2  matt void
    142  1.2.6.2  matt sdmmc_io_scan(struct sdmmc_softc *sc)
    143  1.2.6.2  matt {
    144  1.2.6.2  matt 	struct sdmmc_function *sf0, *sf;
    145  1.2.6.2  matt 	int error;
    146  1.2.6.2  matt 	int i;
    147  1.2.6.2  matt 
    148  1.2.6.2  matt 	SDMMC_LOCK(sc);
    149  1.2.6.2  matt 
    150  1.2.6.2  matt 	sf0 = sdmmc_function_alloc(sc);
    151  1.2.6.2  matt 	sf0->number = 0;
    152  1.2.6.2  matt 	error = sdmmc_set_relative_addr(sc, sf0);
    153  1.2.6.2  matt 	if (error) {
    154  1.2.6.2  matt 		aprint_error_dev(sc->sc_dev, "couldn't set I/O RCA\n");
    155  1.2.6.2  matt 		SET(sf0->flags, SFF_ERROR);
    156  1.2.6.2  matt 		goto out;
    157  1.2.6.2  matt 	}
    158  1.2.6.2  matt 	sc->sc_fn0 = sf0;
    159  1.2.6.2  matt 	SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf0, sf_list);
    160  1.2.6.2  matt 
    161  1.2.6.2  matt 	/* Verify that the RCA has been set by selecting the card. */
    162  1.2.6.2  matt 	error = sdmmc_select_card(sc, sf0);
    163  1.2.6.2  matt 	if (error) {
    164  1.2.6.2  matt 		aprint_error_dev(sc->sc_dev, "couldn't select I/O RCA %d\n",
    165  1.2.6.2  matt 		    sf0->rca);
    166  1.2.6.2  matt 		SET(sf0->flags, SFF_ERROR);
    167  1.2.6.2  matt 		goto out;
    168  1.2.6.2  matt 	}
    169  1.2.6.2  matt 
    170  1.2.6.2  matt 	for (i = 1; i <= sc->sc_function_count; i++) {
    171  1.2.6.2  matt 		sf = sdmmc_function_alloc(sc);
    172  1.2.6.2  matt 		sf->number = i;
    173  1.2.6.2  matt 		sf->rca = sf0->rca;
    174  1.2.6.2  matt 		SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
    175  1.2.6.2  matt 	}
    176  1.2.6.2  matt 
    177  1.2.6.2  matt out:
    178  1.2.6.2  matt 	SDMMC_UNLOCK(sc);
    179  1.2.6.2  matt }
    180  1.2.6.2  matt 
    181  1.2.6.2  matt /*
    182  1.2.6.2  matt  * Initialize SDIO card functions.
    183  1.2.6.2  matt  */
    184  1.2.6.2  matt int
    185  1.2.6.2  matt sdmmc_io_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    186  1.2.6.2  matt {
    187  1.2.6.2  matt 	int error = 0;
    188  1.2.6.2  matt 
    189  1.2.6.2  matt 	SDMMC_LOCK(sc);
    190  1.2.6.2  matt 
    191  1.2.6.2  matt 	if (sf->number == 0) {
    192  1.2.6.2  matt 		sdmmc_io_write_1(sf, SD_IO_CCCR_BUS_WIDTH, CCCR_BUS_WIDTH_1);
    193  1.2.6.2  matt 
    194  1.2.6.2  matt 		error = sdmmc_read_cis(sf, &sf->cis);
    195  1.2.6.2  matt 		if (error) {
    196  1.2.6.2  matt 			aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
    197  1.2.6.2  matt 			SET(sf->flags, SFF_ERROR);
    198  1.2.6.2  matt 			goto out;
    199  1.2.6.2  matt 		}
    200  1.2.6.2  matt 
    201  1.2.6.2  matt 		sdmmc_check_cis_quirks(sf);
    202  1.2.6.2  matt 
    203  1.2.6.2  matt #ifdef SDMMC_DEBUG
    204  1.2.6.2  matt 		if (sdmmcdebug)
    205  1.2.6.2  matt 			sdmmc_print_cis(sf);
    206  1.2.6.2  matt #endif
    207  1.2.6.2  matt 	}
    208  1.2.6.2  matt 
    209  1.2.6.2  matt out:
    210  1.2.6.2  matt 	SDMMC_UNLOCK(sc);
    211  1.2.6.2  matt 
    212  1.2.6.2  matt 	return error;
    213  1.2.6.2  matt }
    214  1.2.6.2  matt 
    215  1.2.6.2  matt /*
    216  1.2.6.2  matt  * Indicate whether the function is ready to operate.
    217  1.2.6.2  matt  */
    218  1.2.6.2  matt static int
    219  1.2.6.2  matt sdmmc_io_function_ready(struct sdmmc_function *sf)
    220  1.2.6.2  matt {
    221  1.2.6.2  matt 	struct sdmmc_softc *sc = sf->sc;
    222  1.2.6.2  matt 	struct sdmmc_function *sf0 = sc->sc_fn0;
    223  1.2.6.2  matt 	uint8_t reg;
    224  1.2.6.2  matt 
    225  1.2.6.2  matt 	if (sf->number == 0)
    226  1.2.6.2  matt 		return 1;	/* FN0 is always ready */
    227  1.2.6.2  matt 
    228  1.2.6.2  matt 	SDMMC_LOCK(sc);
    229  1.2.6.2  matt 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_IOREADY);
    230  1.2.6.2  matt 	SDMMC_UNLOCK(sc);
    231  1.2.6.2  matt 	return (reg & (1 << sf->number)) != 0;
    232  1.2.6.2  matt }
    233  1.2.6.2  matt 
    234  1.2.6.2  matt int
    235  1.2.6.2  matt sdmmc_io_function_enable(struct sdmmc_function *sf)
    236  1.2.6.2  matt {
    237  1.2.6.2  matt 	struct sdmmc_softc *sc = sf->sc;
    238  1.2.6.2  matt 	struct sdmmc_function *sf0 = sc->sc_fn0;
    239  1.2.6.2  matt 	uint8_t reg;
    240  1.2.6.2  matt 	int retry;
    241  1.2.6.2  matt 
    242  1.2.6.2  matt 	if (sf->number == 0)
    243  1.2.6.2  matt 		return 0;	/* FN0 is always enabled */
    244  1.2.6.2  matt 
    245  1.2.6.2  matt 	SDMMC_LOCK(sc);
    246  1.2.6.2  matt 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
    247  1.2.6.2  matt 	SET(reg, (1U << sf->number));
    248  1.2.6.2  matt 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
    249  1.2.6.2  matt 	SDMMC_UNLOCK(sc);
    250  1.2.6.2  matt 
    251  1.2.6.2  matt 	retry = 5;
    252  1.2.6.2  matt 	while (!sdmmc_io_function_ready(sf) && retry-- > 0)
    253  1.2.6.2  matt 		tsleep(&lbolt, PPAUSE, "pause", 0);
    254  1.2.6.2  matt 	return (retry >= 0) ? 0 : ETIMEDOUT;
    255  1.2.6.2  matt }
    256  1.2.6.2  matt 
    257  1.2.6.2  matt /*
    258  1.2.6.2  matt  * Disable the I/O function.  Return zero if the function was
    259  1.2.6.2  matt  * disabled successfully.
    260  1.2.6.2  matt  */
    261  1.2.6.2  matt void
    262  1.2.6.2  matt sdmmc_io_function_disable(struct sdmmc_function *sf)
    263  1.2.6.2  matt {
    264  1.2.6.2  matt 	struct sdmmc_softc *sc = sf->sc;
    265  1.2.6.2  matt 	struct sdmmc_function *sf0 = sc->sc_fn0;
    266  1.2.6.2  matt 	uint8_t reg;
    267  1.2.6.2  matt 
    268  1.2.6.2  matt 	if (sf->number == 0)
    269  1.2.6.2  matt 		return;		/* FN0 is always enabled */
    270  1.2.6.2  matt 
    271  1.2.6.2  matt 	SDMMC_LOCK(sc);
    272  1.2.6.2  matt 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
    273  1.2.6.2  matt 	CLR(reg, (1U << sf->number));
    274  1.2.6.2  matt 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
    275  1.2.6.2  matt 	SDMMC_UNLOCK(sc);
    276  1.2.6.2  matt }
    277  1.2.6.2  matt 
    278  1.2.6.2  matt static int
    279  1.2.6.2  matt sdmmc_io_rw_direct(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    280  1.2.6.2  matt     int reg, u_char *datap, int arg)
    281  1.2.6.2  matt {
    282  1.2.6.2  matt 	struct sdmmc_command cmd;
    283  1.2.6.2  matt 	int error;
    284  1.2.6.2  matt 
    285  1.2.6.2  matt 	/* Don't lock */
    286  1.2.6.2  matt 
    287  1.2.6.2  matt 	/* Make sure the card is selected. */
    288  1.2.6.2  matt 	error = sdmmc_select_card(sc, sf);
    289  1.2.6.2  matt 	if (error)
    290  1.2.6.2  matt 		return error;
    291  1.2.6.2  matt 
    292  1.2.6.2  matt 	arg |= ((sf == NULL ? 0 : sf->number) & SD_ARG_CMD52_FUNC_MASK) <<
    293  1.2.6.2  matt 	    SD_ARG_CMD52_FUNC_SHIFT;
    294  1.2.6.2  matt 	arg |= (reg & SD_ARG_CMD52_REG_MASK) <<
    295  1.2.6.2  matt 	    SD_ARG_CMD52_REG_SHIFT;
    296  1.2.6.2  matt 	arg |= (*datap & SD_ARG_CMD52_DATA_MASK) <<
    297  1.2.6.2  matt 	    SD_ARG_CMD52_DATA_SHIFT;
    298  1.2.6.2  matt 
    299  1.2.6.2  matt 	memset(&cmd, 0, sizeof cmd);
    300  1.2.6.2  matt 	cmd.c_opcode = SD_IO_RW_DIRECT;
    301  1.2.6.2  matt 	cmd.c_arg = arg;
    302  1.2.6.2  matt 	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
    303  1.2.6.2  matt 
    304  1.2.6.2  matt 	error = sdmmc_mmc_command(sc, &cmd);
    305  1.2.6.2  matt 	*datap = SD_R5_DATA(cmd.c_resp);
    306  1.2.6.2  matt 
    307  1.2.6.2  matt 	return error;
    308  1.2.6.2  matt }
    309  1.2.6.2  matt 
    310  1.2.6.2  matt /*
    311  1.2.6.2  matt  * Useful values of `arg' to pass in are either SD_ARG_CMD53_READ or
    312  1.2.6.2  matt  * SD_ARG_CMD53_WRITE.  SD_ARG_CMD53_INCREMENT may be ORed into `arg'
    313  1.2.6.2  matt  * to access successive register locations instead of accessing the
    314  1.2.6.2  matt  * same register many times.
    315  1.2.6.2  matt  */
    316  1.2.6.2  matt static int
    317  1.2.6.2  matt sdmmc_io_rw_extended(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    318  1.2.6.2  matt     int reg, u_char *datap, int datalen, int arg)
    319  1.2.6.2  matt {
    320  1.2.6.2  matt 	struct sdmmc_command cmd;
    321  1.2.6.2  matt 	int error;
    322  1.2.6.2  matt 
    323  1.2.6.2  matt 	/* Don't lock */
    324  1.2.6.2  matt 
    325  1.2.6.2  matt #if 0
    326  1.2.6.2  matt 	/* Make sure the card is selected. */
    327  1.2.6.2  matt 	error = sdmmc_select_card(sc, sf);
    328  1.2.6.2  matt 	if (error)
    329  1.2.6.2  matt 		return error;
    330  1.2.6.2  matt #endif
    331  1.2.6.2  matt 
    332  1.2.6.2  matt 	arg |= (((sf == NULL) ? 0 : sf->number) & SD_ARG_CMD53_FUNC_MASK) <<
    333  1.2.6.2  matt 	    SD_ARG_CMD53_FUNC_SHIFT;
    334  1.2.6.2  matt 	arg |= (reg & SD_ARG_CMD53_REG_MASK) <<
    335  1.2.6.2  matt 	    SD_ARG_CMD53_REG_SHIFT;
    336  1.2.6.2  matt 	arg |= (datalen & SD_ARG_CMD53_LENGTH_MASK) <<
    337  1.2.6.2  matt 	    SD_ARG_CMD53_LENGTH_SHIFT;
    338  1.2.6.2  matt 
    339  1.2.6.2  matt 	memset(&cmd, 0, sizeof cmd);
    340  1.2.6.2  matt 	cmd.c_opcode = SD_IO_RW_EXTENDED;
    341  1.2.6.2  matt 	cmd.c_arg = arg;
    342  1.2.6.2  matt 	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
    343  1.2.6.2  matt 	cmd.c_data = datap;
    344  1.2.6.2  matt 	cmd.c_datalen = datalen;
    345  1.2.6.2  matt 	cmd.c_blklen = MIN(datalen,
    346  1.2.6.2  matt 	    sdmmc_chip_host_maxblklen(sc->sc_sct,sc->sc_sch));
    347  1.2.6.2  matt 	if (!ISSET(arg, SD_ARG_CMD53_WRITE))
    348  1.2.6.2  matt 		cmd.c_flags |= SCF_CMD_READ;
    349  1.2.6.2  matt 
    350  1.2.6.2  matt 	error = sdmmc_mmc_command(sc, &cmd);
    351  1.2.6.2  matt 
    352  1.2.6.2  matt 	return error;
    353  1.2.6.2  matt }
    354  1.2.6.2  matt 
    355  1.2.6.2  matt uint8_t
    356  1.2.6.2  matt sdmmc_io_read_1(struct sdmmc_function *sf, int reg)
    357  1.2.6.2  matt {
    358  1.2.6.2  matt 	uint8_t data = 0;
    359  1.2.6.2  matt 
    360  1.2.6.2  matt 	/* Don't lock */
    361  1.2.6.2  matt 
    362  1.2.6.2  matt 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    363  1.2.6.2  matt 	    SD_ARG_CMD52_READ);
    364  1.2.6.2  matt 	return data;
    365  1.2.6.2  matt }
    366  1.2.6.2  matt 
    367  1.2.6.2  matt void
    368  1.2.6.2  matt sdmmc_io_write_1(struct sdmmc_function *sf, int reg, uint8_t data)
    369  1.2.6.2  matt {
    370  1.2.6.2  matt 
    371  1.2.6.2  matt 	/* Don't lock */
    372  1.2.6.2  matt 
    373  1.2.6.2  matt 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    374  1.2.6.2  matt 	    SD_ARG_CMD52_WRITE);
    375  1.2.6.2  matt }
    376  1.2.6.2  matt 
    377  1.2.6.2  matt uint16_t
    378  1.2.6.2  matt sdmmc_io_read_2(struct sdmmc_function *sf, int reg)
    379  1.2.6.2  matt {
    380  1.2.6.2  matt 	uint16_t data = 0;
    381  1.2.6.2  matt 
    382  1.2.6.2  matt 	/* Don't lock */
    383  1.2.6.2  matt 
    384  1.2.6.2  matt 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    385  1.2.6.2  matt 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    386  1.2.6.2  matt 	return data;
    387  1.2.6.2  matt }
    388  1.2.6.2  matt 
    389  1.2.6.2  matt void
    390  1.2.6.2  matt sdmmc_io_write_2(struct sdmmc_function *sf, int reg, uint16_t data)
    391  1.2.6.2  matt {
    392  1.2.6.2  matt 
    393  1.2.6.2  matt 	/* Don't lock */
    394  1.2.6.2  matt 
    395  1.2.6.2  matt 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    396  1.2.6.2  matt 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    397  1.2.6.2  matt }
    398  1.2.6.2  matt 
    399  1.2.6.2  matt uint32_t
    400  1.2.6.2  matt sdmmc_io_read_4(struct sdmmc_function *sf, int reg)
    401  1.2.6.2  matt {
    402  1.2.6.2  matt 	uint32_t data = 0;
    403  1.2.6.2  matt 
    404  1.2.6.2  matt 	/* Don't lock */
    405  1.2.6.2  matt 
    406  1.2.6.2  matt 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    407  1.2.6.2  matt 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    408  1.2.6.2  matt 	return data;
    409  1.2.6.2  matt }
    410  1.2.6.2  matt 
    411  1.2.6.2  matt void
    412  1.2.6.2  matt sdmmc_io_write_4(struct sdmmc_function *sf, int reg, uint32_t data)
    413  1.2.6.2  matt {
    414  1.2.6.2  matt 
    415  1.2.6.2  matt 	/* Don't lock */
    416  1.2.6.2  matt 
    417  1.2.6.2  matt 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    418  1.2.6.2  matt 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    419  1.2.6.2  matt }
    420  1.2.6.2  matt 
    421  1.2.6.2  matt 
    422  1.2.6.2  matt int
    423  1.2.6.2  matt sdmmc_io_read_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    424  1.2.6.2  matt     int datalen)
    425  1.2.6.2  matt {
    426  1.2.6.2  matt 	int error;
    427  1.2.6.2  matt 
    428  1.2.6.2  matt 	/* Don't lock */
    429  1.2.6.2  matt 
    430  1.2.6.2  matt 	while (datalen > SD_ARG_CMD53_LENGTH_MAX) {
    431  1.2.6.2  matt 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    432  1.2.6.2  matt 		    SD_ARG_CMD53_LENGTH_MAX, SD_ARG_CMD53_READ);
    433  1.2.6.2  matt 		if (error)
    434  1.2.6.2  matt 			goto error;
    435  1.2.6.2  matt 		data += SD_ARG_CMD53_LENGTH_MAX;
    436  1.2.6.2  matt 		datalen -= SD_ARG_CMD53_LENGTH_MAX;
    437  1.2.6.2  matt 	}
    438  1.2.6.2  matt 
    439  1.2.6.2  matt 	error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    440  1.2.6.2  matt 	    SD_ARG_CMD53_READ);
    441  1.2.6.2  matt error:
    442  1.2.6.2  matt 	return error;
    443  1.2.6.2  matt }
    444  1.2.6.2  matt 
    445  1.2.6.2  matt int
    446  1.2.6.2  matt sdmmc_io_write_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    447  1.2.6.2  matt     int datalen)
    448  1.2.6.2  matt {
    449  1.2.6.2  matt 	int error;
    450  1.2.6.2  matt 
    451  1.2.6.2  matt 	/* Don't lock */
    452  1.2.6.2  matt 
    453  1.2.6.2  matt 	while (datalen > SD_ARG_CMD53_LENGTH_MAX) {
    454  1.2.6.2  matt 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    455  1.2.6.2  matt 		    SD_ARG_CMD53_LENGTH_MAX, SD_ARG_CMD53_WRITE);
    456  1.2.6.2  matt 		if (error)
    457  1.2.6.2  matt 			goto error;
    458  1.2.6.2  matt 		data += SD_ARG_CMD53_LENGTH_MAX;
    459  1.2.6.2  matt 		datalen -= SD_ARG_CMD53_LENGTH_MAX;
    460  1.2.6.2  matt 	}
    461  1.2.6.2  matt 
    462  1.2.6.2  matt 	error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    463  1.2.6.2  matt 	    SD_ARG_CMD53_WRITE);
    464  1.2.6.2  matt error:
    465  1.2.6.2  matt 	return error;
    466  1.2.6.2  matt }
    467  1.2.6.2  matt 
    468  1.2.6.2  matt #if 0
    469  1.2.6.2  matt static int
    470  1.2.6.2  matt sdmmc_io_xchg(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    471  1.2.6.2  matt     int reg, u_char *datap)
    472  1.2.6.2  matt {
    473  1.2.6.2  matt 
    474  1.2.6.2  matt 	/* Don't lock */
    475  1.2.6.2  matt 
    476  1.2.6.2  matt 	return sdmmc_io_rw_direct(sc, sf, reg, datap,
    477  1.2.6.2  matt 	    SD_ARG_CMD52_WRITE|SD_ARG_CMD52_EXCHANGE);
    478  1.2.6.2  matt }
    479  1.2.6.2  matt #endif
    480  1.2.6.2  matt 
    481  1.2.6.2  matt /*
    482  1.2.6.2  matt  * Reset the I/O functions of the card.
    483  1.2.6.2  matt  */
    484  1.2.6.2  matt static void
    485  1.2.6.2  matt sdmmc_io_reset(struct sdmmc_softc *sc)
    486  1.2.6.2  matt {
    487  1.2.6.2  matt 
    488  1.2.6.2  matt 	/* Don't lock */
    489  1.2.6.2  matt #if 0 /* XXX command fails */
    490  1.2.6.2  matt 	(void)sdmmc_io_write(sc, NULL, SD_IO_REG_CCCR_CTL, CCCR_CTL_RES);
    491  1.2.6.2  matt 	sdmmc_delay(100000);
    492  1.2.6.2  matt #endif
    493  1.2.6.2  matt }
    494  1.2.6.2  matt 
    495  1.2.6.2  matt /*
    496  1.2.6.2  matt  * Get or set the card's I/O OCR value (SDIO).
    497  1.2.6.2  matt  */
    498  1.2.6.2  matt static int
    499  1.2.6.2  matt sdmmc_io_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, u_int32_t *ocrp)
    500  1.2.6.2  matt {
    501  1.2.6.2  matt 	struct sdmmc_command cmd;
    502  1.2.6.2  matt 	int error;
    503  1.2.6.2  matt 	int retry;
    504  1.2.6.2  matt 
    505  1.2.6.2  matt 	DPRINTF(("sdmmc_io_send_op_cond: ocr = %#x\n", ocr));
    506  1.2.6.2  matt 
    507  1.2.6.2  matt 	/* Don't lock */
    508  1.2.6.2  matt 
    509  1.2.6.2  matt 	/*
    510  1.2.6.2  matt 	 * If we change the OCR value, retry the command until the OCR
    511  1.2.6.2  matt 	 * we receive in response has the "CARD BUSY" bit set, meaning
    512  1.2.6.2  matt 	 * that all cards are ready for identification.
    513  1.2.6.2  matt 	 */
    514  1.2.6.2  matt 	for (retry = 0; retry < 100; retry++) {
    515  1.2.6.2  matt 		memset(&cmd, 0, sizeof cmd);
    516  1.2.6.2  matt 		cmd.c_opcode = SD_IO_SEND_OP_COND;
    517  1.2.6.2  matt 		cmd.c_arg = ocr;
    518  1.2.6.2  matt 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R4;
    519  1.2.6.2  matt 
    520  1.2.6.2  matt 		error = sdmmc_mmc_command(sc, &cmd);
    521  1.2.6.2  matt 		if (error)
    522  1.2.6.2  matt 			break;
    523  1.2.6.2  matt 		if (ISSET(MMC_R4(cmd.c_resp), SD_IO_OCR_MEM_READY) || ocr == 0)
    524  1.2.6.2  matt 			break;
    525  1.2.6.2  matt 
    526  1.2.6.2  matt 		error = ETIMEDOUT;
    527  1.2.6.2  matt 		sdmmc_delay(10000);
    528  1.2.6.2  matt 	}
    529  1.2.6.2  matt 	if (error == 0 && ocrp != NULL)
    530  1.2.6.2  matt 		*ocrp = MMC_R4(cmd.c_resp);
    531  1.2.6.2  matt 
    532  1.2.6.2  matt 	DPRINTF(("sdmmc_io_send_op_cond: error = %d\n", error));
    533  1.2.6.2  matt 
    534  1.2.6.2  matt 	return error;
    535  1.2.6.2  matt }
    536  1.2.6.2  matt 
    537  1.2.6.2  matt /*
    538  1.2.6.2  matt  * Card interrupt handling
    539  1.2.6.2  matt  */
    540  1.2.6.2  matt 
    541  1.2.6.2  matt void
    542  1.2.6.2  matt sdmmc_intr_enable(struct sdmmc_function *sf)
    543  1.2.6.2  matt {
    544  1.2.6.2  matt 	struct sdmmc_softc *sc = sf->sc;
    545  1.2.6.2  matt 	struct sdmmc_function *sf0 = sc->sc_fn0;
    546  1.2.6.2  matt 	uint8_t reg;
    547  1.2.6.2  matt 
    548  1.2.6.2  matt 	SDMMC_LOCK(sc);
    549  1.2.6.2  matt 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    550  1.2.6.2  matt 	reg |= 1 << sf->number;
    551  1.2.6.2  matt 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    552  1.2.6.2  matt 	SDMMC_UNLOCK(sc);
    553  1.2.6.2  matt }
    554  1.2.6.2  matt 
    555  1.2.6.2  matt void
    556  1.2.6.2  matt sdmmc_intr_disable(struct sdmmc_function *sf)
    557  1.2.6.2  matt {
    558  1.2.6.2  matt 	struct sdmmc_softc *sc = sf->sc;
    559  1.2.6.2  matt 	struct sdmmc_function *sf0 = sc->sc_fn0;
    560  1.2.6.2  matt 	uint8_t reg;
    561  1.2.6.2  matt 
    562  1.2.6.2  matt 	SDMMC_LOCK(sc);
    563  1.2.6.2  matt 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    564  1.2.6.2  matt 	reg &= ~(1 << sf->number);
    565  1.2.6.2  matt 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    566  1.2.6.2  matt 	SDMMC_UNLOCK(sc);
    567  1.2.6.2  matt }
    568  1.2.6.2  matt 
    569  1.2.6.2  matt /*
    570  1.2.6.2  matt  * Establish a handler for the SDIO card interrupt.  Because the
    571  1.2.6.2  matt  * interrupt may be shared with different SDIO functions, multiple
    572  1.2.6.2  matt  * handlers can be established.
    573  1.2.6.2  matt  */
    574  1.2.6.2  matt void *
    575  1.2.6.2  matt sdmmc_intr_establish(device_t dev, int (*fun)(void *), void *arg,
    576  1.2.6.2  matt     const char *name)
    577  1.2.6.2  matt {
    578  1.2.6.2  matt 	struct sdmmc_softc *sc = device_private(dev);
    579  1.2.6.2  matt 	struct sdmmc_intr_handler *ih;
    580  1.2.6.2  matt 	int s;
    581  1.2.6.2  matt 
    582  1.2.6.2  matt 	if (sc->sc_sct->card_enable_intr == NULL)
    583  1.2.6.2  matt 		return NULL;
    584  1.2.6.2  matt 
    585  1.2.6.2  matt 	ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK|M_CANFAIL|M_ZERO);
    586  1.2.6.2  matt 	if (ih == NULL)
    587  1.2.6.2  matt 		return NULL;
    588  1.2.6.2  matt 
    589  1.2.6.2  matt 	ih->ih_name = malloc(strlen(name) + 1, M_DEVBUF,
    590  1.2.6.2  matt 	    M_WAITOK|M_CANFAIL|M_ZERO);
    591  1.2.6.2  matt 	if (ih->ih_name == NULL) {
    592  1.2.6.2  matt 		free(ih, M_DEVBUF);
    593  1.2.6.2  matt 		return NULL;
    594  1.2.6.2  matt 	}
    595  1.2.6.2  matt 	strlcpy(ih->ih_name, name, strlen(name));
    596  1.2.6.2  matt 	ih->ih_softc = sc;
    597  1.2.6.2  matt 	ih->ih_fun = fun;
    598  1.2.6.2  matt 	ih->ih_arg = arg;
    599  1.2.6.2  matt 
    600  1.2.6.2  matt 	s = splhigh();
    601  1.2.6.2  matt 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    602  1.2.6.2  matt 		sdmmc_intr_enable(sc->sc_fn0);
    603  1.2.6.2  matt 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 1);
    604  1.2.6.2  matt 	}
    605  1.2.6.2  matt 	TAILQ_INSERT_TAIL(&sc->sc_intrq, ih, entry);
    606  1.2.6.2  matt 	splx(s);
    607  1.2.6.2  matt 
    608  1.2.6.2  matt 	return ih;
    609  1.2.6.2  matt }
    610  1.2.6.2  matt 
    611  1.2.6.2  matt /*
    612  1.2.6.2  matt  * Disestablish the given handler.
    613  1.2.6.2  matt  */
    614  1.2.6.2  matt void
    615  1.2.6.2  matt sdmmc_intr_disestablish(void *cookie)
    616  1.2.6.2  matt {
    617  1.2.6.2  matt 	struct sdmmc_intr_handler *ih = cookie;
    618  1.2.6.2  matt 	struct sdmmc_softc *sc = ih->ih_softc;
    619  1.2.6.2  matt 	int s;
    620  1.2.6.2  matt 
    621  1.2.6.2  matt 	if (sc->sc_sct->card_enable_intr == NULL)
    622  1.2.6.2  matt 		return;
    623  1.2.6.2  matt 
    624  1.2.6.2  matt 	s = splhigh();
    625  1.2.6.2  matt 	TAILQ_REMOVE(&sc->sc_intrq, ih, entry);
    626  1.2.6.2  matt 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    627  1.2.6.2  matt 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 0);
    628  1.2.6.2  matt 		sdmmc_intr_disable(sc->sc_fn0);
    629  1.2.6.2  matt 	}
    630  1.2.6.2  matt 	splx(s);
    631  1.2.6.2  matt 
    632  1.2.6.2  matt 	free(ih->ih_name, M_DEVBUF);
    633  1.2.6.2  matt 	free(ih, M_DEVBUF);
    634  1.2.6.2  matt }
    635  1.2.6.2  matt 
    636  1.2.6.2  matt /*
    637  1.2.6.2  matt  * Call established SDIO card interrupt handlers.  The host controller
    638  1.2.6.2  matt  * must call this function from its own interrupt handler to handle an
    639  1.2.6.2  matt  * SDIO interrupt from the card.
    640  1.2.6.2  matt  */
    641  1.2.6.2  matt void
    642  1.2.6.2  matt sdmmc_card_intr(device_t dev)
    643  1.2.6.2  matt {
    644  1.2.6.2  matt 	struct sdmmc_softc *sc = device_private(dev);
    645  1.2.6.2  matt 
    646  1.2.6.2  matt 	if (sc->sc_sct->card_enable_intr) {
    647  1.2.6.2  matt 		mutex_enter(&sc->sc_intr_task_mtx);
    648  1.2.6.2  matt 		if (!sdmmc_task_pending(&sc->sc_intr_task))
    649  1.2.6.2  matt 			sdmmc_add_task(sc, &sc->sc_intr_task);
    650  1.2.6.2  matt 		mutex_exit(&sc->sc_intr_task_mtx);
    651  1.2.6.2  matt 	}
    652  1.2.6.2  matt }
    653  1.2.6.2  matt 
    654  1.2.6.2  matt void
    655  1.2.6.2  matt sdmmc_intr_task(void *arg)
    656  1.2.6.2  matt {
    657  1.2.6.2  matt 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    658  1.2.6.2  matt 	struct sdmmc_intr_handler *ih;
    659  1.2.6.2  matt 	int s;
    660  1.2.6.2  matt 
    661  1.2.6.2  matt 	s = splsdmmc();
    662  1.2.6.2  matt 	TAILQ_FOREACH(ih, &sc->sc_intrq, entry) {
    663  1.2.6.2  matt 		splx(s);
    664  1.2.6.2  matt 		/* XXX examine return value and do evcount stuff*/
    665  1.2.6.2  matt 		(void)(*ih->ih_fun)(ih->ih_arg);
    666  1.2.6.2  matt 		s = splsdmmc();
    667  1.2.6.2  matt 	}
    668  1.2.6.2  matt 	sdmmc_chip_card_intr_ack(sc->sc_sct, sc->sc_sch);
    669  1.2.6.2  matt 	splx(s);
    670  1.2.6.2  matt }
    671