Home | History | Annotate | Line # | Download | only in amlogic
meson_sdio.c revision 1.3.2.1
      1  1.3.2.1   thorpej /* $NetBSD: meson_sdio.c,v 1.3.2.1 2021/03/21 21:08:54 thorpej Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2015-2019 Jared D. McNeill <jmcneill (at) invisible.ca>
      5      1.1  jmcneill  * All rights reserved.
      6      1.1  jmcneill  *
      7      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8      1.1  jmcneill  * modification, are permitted provided that the following conditions
      9      1.1  jmcneill  * are met:
     10      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15      1.1  jmcneill  *
     16      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21      1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22      1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23      1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24      1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1  jmcneill  * SUCH DAMAGE.
     27      1.1  jmcneill  */
     28      1.1  jmcneill 
     29      1.1  jmcneill #include <sys/cdefs.h>
     30  1.3.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: meson_sdio.c,v 1.3.2.1 2021/03/21 21:08:54 thorpej Exp $");
     31      1.1  jmcneill 
     32      1.1  jmcneill #include <sys/param.h>
     33      1.1  jmcneill #include <sys/bus.h>
     34      1.1  jmcneill #include <sys/device.h>
     35      1.1  jmcneill #include <sys/intr.h>
     36      1.1  jmcneill #include <sys/systm.h>
     37      1.1  jmcneill #include <sys/kernel.h>
     38      1.1  jmcneill #include <sys/gpio.h>
     39      1.1  jmcneill 
     40      1.1  jmcneill #include <dev/sdmmc/sdmmcvar.h>
     41      1.1  jmcneill #include <dev/sdmmc/sdmmcchip.h>
     42      1.1  jmcneill #include <dev/sdmmc/sdmmc_ioreg.h>
     43      1.1  jmcneill 
     44      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     45      1.1  jmcneill 
     46      1.1  jmcneill #include <arm/amlogic/meson_sdioreg.h>
     47      1.1  jmcneill 
     48      1.1  jmcneill static int	meson_sdio_match(device_t, cfdata_t, void *);
     49      1.1  jmcneill static void	meson_sdio_attach(device_t, device_t, void *);
     50      1.1  jmcneill static void	meson_sdio_attach_i(device_t);
     51      1.1  jmcneill 
     52      1.1  jmcneill static int	meson_sdio_intr(void *);
     53      1.1  jmcneill 
     54      1.1  jmcneill struct meson_sdio_softc {
     55      1.1  jmcneill 	device_t		sc_dev;
     56      1.1  jmcneill 	bus_space_tag_t		sc_bst;
     57      1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     58      1.1  jmcneill 	bus_dma_tag_t		sc_dmat;
     59      1.1  jmcneill 	void			*sc_ih;
     60      1.1  jmcneill 
     61      1.1  jmcneill 	int			sc_slot_phandle;
     62      1.1  jmcneill 
     63      1.1  jmcneill 	uint32_t		sc_bus_freq;
     64      1.1  jmcneill 	u_int			sc_cur_width;
     65      1.1  jmcneill 	int			sc_cur_port;
     66      1.1  jmcneill 
     67      1.1  jmcneill 	struct fdtbus_gpio_pin	*sc_gpio_cd;
     68      1.1  jmcneill 	int			sc_gpio_cd_inverted;
     69      1.1  jmcneill 	struct fdtbus_gpio_pin	*sc_gpio_wp;
     70      1.1  jmcneill 	int			sc_gpio_wp_inverted;
     71      1.1  jmcneill 
     72      1.1  jmcneill 	struct fdtbus_regulator	*sc_reg_vmmc;
     73      1.1  jmcneill 	struct fdtbus_regulator	*sc_reg_vqmmc;
     74      1.1  jmcneill 
     75      1.1  jmcneill 	bool			sc_non_removable;
     76      1.1  jmcneill 	bool			sc_broken_cd;
     77      1.1  jmcneill 
     78      1.1  jmcneill 	device_t		sc_sdmmc_dev;
     79      1.1  jmcneill 	kmutex_t		sc_intr_lock;
     80      1.1  jmcneill 	kcondvar_t		sc_intr_cv;
     81      1.1  jmcneill 
     82      1.1  jmcneill 	uint32_t		sc_intr_irqs;
     83      1.1  jmcneill 
     84      1.1  jmcneill 	bus_dmamap_t		sc_dmamap;
     85      1.1  jmcneill 	bus_dma_segment_t	sc_segs[1];
     86      1.1  jmcneill 	void			*sc_bbuf;
     87      1.1  jmcneill };
     88      1.1  jmcneill 
     89      1.1  jmcneill CFATTACH_DECL_NEW(meson_sdio, sizeof(struct meson_sdio_softc),
     90      1.1  jmcneill 	meson_sdio_match, meson_sdio_attach, NULL, NULL);
     91      1.1  jmcneill 
     92      1.1  jmcneill static int	meson_sdio_host_reset(sdmmc_chipset_handle_t);
     93      1.1  jmcneill static uint32_t	meson_sdio_host_ocr(sdmmc_chipset_handle_t);
     94      1.1  jmcneill static int	meson_sdio_host_maxblklen(sdmmc_chipset_handle_t);
     95      1.1  jmcneill static int	meson_sdio_card_detect(sdmmc_chipset_handle_t);
     96      1.1  jmcneill static int	meson_sdio_write_protect(sdmmc_chipset_handle_t);
     97      1.1  jmcneill static int	meson_sdio_bus_power(sdmmc_chipset_handle_t, uint32_t);
     98      1.1  jmcneill static int	meson_sdio_bus_clock(sdmmc_chipset_handle_t, int);
     99      1.1  jmcneill static int	meson_sdio_bus_width(sdmmc_chipset_handle_t, int);
    100      1.1  jmcneill static int	meson_sdio_bus_rod(sdmmc_chipset_handle_t, int);
    101      1.1  jmcneill static int	meson_sdio_signal_voltage(sdmmc_chipset_handle_t, int);
    102      1.1  jmcneill static void	meson_sdio_exec_command(sdmmc_chipset_handle_t,
    103      1.1  jmcneill 				     struct sdmmc_command *);
    104      1.1  jmcneill static void	meson_sdio_card_enable_intr(sdmmc_chipset_handle_t, int);
    105      1.1  jmcneill static void	meson_sdio_card_intr_ack(sdmmc_chipset_handle_t);
    106      1.1  jmcneill 
    107      1.1  jmcneill static int	meson_sdio_set_clock(struct meson_sdio_softc *, u_int);
    108      1.1  jmcneill static int	meson_sdio_wait_irqs(struct meson_sdio_softc *, uint32_t, int);
    109      1.1  jmcneill 
    110      1.1  jmcneill static void	meson_sdio_dmainit(struct meson_sdio_softc *);
    111      1.1  jmcneill 
    112      1.1  jmcneill static struct sdmmc_chip_functions meson_sdio_chip_functions = {
    113      1.1  jmcneill 	.host_reset = meson_sdio_host_reset,
    114      1.1  jmcneill 	.host_ocr = meson_sdio_host_ocr,
    115      1.1  jmcneill 	.host_maxblklen = meson_sdio_host_maxblklen,
    116      1.1  jmcneill 	.card_detect = meson_sdio_card_detect,
    117      1.1  jmcneill 	.write_protect = meson_sdio_write_protect,
    118      1.1  jmcneill 	.bus_power = meson_sdio_bus_power,
    119      1.1  jmcneill 	.bus_clock = meson_sdio_bus_clock,
    120      1.1  jmcneill 	.bus_width = meson_sdio_bus_width,
    121      1.1  jmcneill 	.bus_rod = meson_sdio_bus_rod,
    122      1.1  jmcneill 	.signal_voltage = meson_sdio_signal_voltage,
    123      1.1  jmcneill 	.exec_command = meson_sdio_exec_command,
    124      1.1  jmcneill 	.card_enable_intr = meson_sdio_card_enable_intr,
    125      1.1  jmcneill 	.card_intr_ack = meson_sdio_card_intr_ack,
    126      1.1  jmcneill };
    127      1.1  jmcneill 
    128      1.1  jmcneill #define SDIO_WRITE(sc, reg, val) \
    129      1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    130      1.1  jmcneill #define SDIO_READ(sc, reg) \
    131      1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    132      1.1  jmcneill 
    133      1.3   thorpej static const struct device_compatible_entry compat_data[] = {
    134      1.3   thorpej 	{ .compat = "amlogic,meson8b-sdio" },
    135      1.3   thorpej 	DEVICE_COMPAT_EOL
    136      1.1  jmcneill };
    137      1.1  jmcneill 
    138      1.3   thorpej static const struct device_compatible_entry slot_compat_data[] = {
    139      1.3   thorpej 	{ .compat = "mmc-slot" },
    140      1.3   thorpej 	DEVICE_COMPAT_EOL
    141      1.1  jmcneill };
    142      1.1  jmcneill 
    143      1.1  jmcneill static int
    144      1.1  jmcneill meson_sdio_match(device_t parent, cfdata_t cf, void *aux)
    145      1.1  jmcneill {
    146      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    147      1.1  jmcneill 
    148      1.3   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    149      1.1  jmcneill }
    150      1.1  jmcneill 
    151      1.1  jmcneill static void
    152      1.1  jmcneill meson_sdio_attach(device_t parent, device_t self, void *aux)
    153      1.1  jmcneill {
    154      1.1  jmcneill 	struct meson_sdio_softc * const sc = device_private(self);
    155      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    156      1.1  jmcneill 	const int phandle = faa->faa_phandle;
    157      1.1  jmcneill 	char intrstr[128];
    158      1.1  jmcneill 	struct clk *clk_clkin, *clk_core;
    159      1.1  jmcneill 	bus_addr_t addr, port;
    160      1.1  jmcneill 	bus_size_t size;
    161      1.1  jmcneill 	int child;
    162      1.1  jmcneill 
    163      1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    164      1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    165      1.1  jmcneill 		return;
    166      1.1  jmcneill 	}
    167      1.1  jmcneill 
    168      1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    169      1.1  jmcneill 		aprint_error(": failed to decode interrupt\n");
    170      1.1  jmcneill 		return;
    171      1.1  jmcneill 	}
    172      1.1  jmcneill 
    173      1.1  jmcneill 	clk_core = fdtbus_clock_get(phandle, "core");
    174      1.1  jmcneill 	if (clk_core == NULL || clk_enable(clk_core) != 0) {
    175      1.1  jmcneill 		aprint_error(": failed to enable core clock\n");
    176      1.1  jmcneill 		return;
    177      1.1  jmcneill 	}
    178      1.1  jmcneill 
    179      1.1  jmcneill 	clk_clkin = fdtbus_clock_get(phandle, "clkin");
    180      1.1  jmcneill 	if (clk_clkin == NULL) {
    181      1.1  jmcneill 		aprint_error(": failed to get clkin clock\n");
    182      1.1  jmcneill 		return;
    183      1.1  jmcneill 	}
    184      1.1  jmcneill 
    185      1.1  jmcneill 	sc->sc_dev = self;
    186      1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    187      1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    188      1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    189      1.1  jmcneill 		aprint_error(": failed to map registers\n");
    190      1.1  jmcneill 		return;
    191      1.1  jmcneill 	}
    192      1.1  jmcneill 
    193      1.1  jmcneill 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
    194      1.1  jmcneill 	cv_init(&sc->sc_intr_cv, "sdiointr");
    195      1.1  jmcneill 
    196      1.1  jmcneill 	sc->sc_cur_port = -1;
    197      1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child))
    198      1.3   thorpej 		if (of_compatible_match(child, slot_compat_data)) {
    199      1.1  jmcneill 			if (fdtbus_get_reg(child, 0, &port, NULL) == 0) {
    200      1.1  jmcneill 				sc->sc_slot_phandle = child;
    201      1.1  jmcneill 				sc->sc_cur_port = port;
    202      1.1  jmcneill 			}
    203      1.1  jmcneill 			break;
    204      1.1  jmcneill 		}
    205      1.1  jmcneill 	if (sc->sc_cur_port == -1) {
    206      1.1  jmcneill 		aprint_error(": couldn't get mmc slot\n");
    207      1.1  jmcneill 		return;
    208      1.1  jmcneill 	}
    209      1.1  jmcneill 
    210      1.1  jmcneill 	aprint_naive("\n");
    211      1.1  jmcneill 	aprint_normal(": SDIO controller (port %c)\n", sc->sc_cur_port + 'A');
    212      1.1  jmcneill 
    213      1.1  jmcneill 	sc->sc_reg_vmmc = fdtbus_regulator_acquire(sc->sc_slot_phandle, "vmmc-supply");
    214      1.1  jmcneill 	sc->sc_reg_vqmmc = fdtbus_regulator_acquire(sc->sc_slot_phandle, "vqmmc-supply");
    215      1.1  jmcneill 
    216      1.1  jmcneill 	sc->sc_gpio_cd = fdtbus_gpio_acquire(sc->sc_slot_phandle, "cd-gpios",
    217      1.1  jmcneill 	    GPIO_PIN_INPUT);
    218      1.1  jmcneill 	sc->sc_gpio_wp = fdtbus_gpio_acquire(sc->sc_slot_phandle, "wp-gpios",
    219      1.1  jmcneill 	    GPIO_PIN_INPUT);
    220      1.1  jmcneill 
    221      1.1  jmcneill 	sc->sc_gpio_cd_inverted = of_hasprop(sc->sc_slot_phandle, "cd-inverted");
    222      1.1  jmcneill 	sc->sc_gpio_wp_inverted = of_hasprop(sc->sc_slot_phandle, "wp-inverted");
    223      1.1  jmcneill 
    224      1.1  jmcneill 	sc->sc_non_removable = of_hasprop(sc->sc_slot_phandle, "non-removable");
    225      1.1  jmcneill 	sc->sc_broken_cd = of_hasprop(sc->sc_slot_phandle, "broken-cd");
    226      1.1  jmcneill 
    227      1.2       ryo 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_BIO, 0,
    228      1.2       ryo 	    meson_sdio_intr, sc, device_xname(self));
    229      1.1  jmcneill 	if (sc->sc_ih == NULL) {
    230      1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    231      1.1  jmcneill 		    intrstr);
    232      1.1  jmcneill 		return;
    233      1.1  jmcneill 	}
    234      1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    235      1.1  jmcneill 
    236      1.1  jmcneill 	sc->sc_bus_freq = clk_get_rate(clk_clkin);
    237      1.1  jmcneill 
    238      1.1  jmcneill 	aprint_normal_dev(self, "core %u Hz, clkin %u Hz\n", clk_get_rate(clk_core), clk_get_rate(clk_clkin));
    239      1.1  jmcneill 
    240      1.1  jmcneill 	meson_sdio_dmainit(sc);
    241      1.1  jmcneill 
    242      1.1  jmcneill 	config_interrupts(self, meson_sdio_attach_i);
    243      1.1  jmcneill }
    244      1.1  jmcneill 
    245      1.1  jmcneill static void
    246      1.1  jmcneill meson_sdio_attach_i(device_t self)
    247      1.1  jmcneill {
    248      1.1  jmcneill 	struct meson_sdio_softc *sc = device_private(self);
    249      1.1  jmcneill 	struct sdmmcbus_attach_args saa;
    250      1.1  jmcneill 
    251      1.1  jmcneill 	meson_sdio_signal_voltage(sc, SDMMC_SIGNAL_VOLTAGE_330);
    252      1.1  jmcneill 	meson_sdio_host_reset(sc);
    253      1.1  jmcneill 	meson_sdio_bus_clock(sc, 400);
    254      1.1  jmcneill 	meson_sdio_bus_width(sc, 1);
    255      1.1  jmcneill 
    256      1.1  jmcneill 	memset(&saa, 0, sizeof(saa));
    257      1.1  jmcneill 	saa.saa_busname = "sdmmc";
    258      1.1  jmcneill 	saa.saa_sct = &meson_sdio_chip_functions;
    259      1.1  jmcneill 	saa.saa_dmat = sc->sc_dmat;
    260      1.1  jmcneill 	saa.saa_sch = sc;
    261      1.1  jmcneill 	saa.saa_clkmin = 400;
    262      1.1  jmcneill 	saa.saa_clkmax = sc->sc_bus_freq;
    263      1.1  jmcneill 	/* Do not advertise DMA capabilities, we handle DMA ourselves */
    264      1.1  jmcneill 	saa.saa_caps = SMC_CAPS_4BIT_MODE|
    265      1.1  jmcneill 		       SMC_CAPS_SD_HIGHSPEED|
    266      1.1  jmcneill 		       SMC_CAPS_MMC_HIGHSPEED;
    267      1.1  jmcneill 
    268  1.3.2.1   thorpej 	sc->sc_sdmmc_dev = config_found(self, &saa, NULL, CFARG_EOL);
    269      1.1  jmcneill }
    270      1.1  jmcneill 
    271      1.1  jmcneill static int
    272      1.1  jmcneill meson_sdio_intr(void *priv)
    273      1.1  jmcneill {
    274      1.1  jmcneill 	struct meson_sdio_softc *sc = priv;
    275      1.1  jmcneill 
    276      1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    277      1.1  jmcneill 	const u_int irqs = SDIO_READ(sc, SDIO_IRQS_REG);
    278      1.1  jmcneill 	if (irqs & SDIO_IRQS_CLEAR) {
    279      1.1  jmcneill 		SDIO_WRITE(sc, SDIO_IRQS_REG, irqs);
    280      1.1  jmcneill 		sc->sc_intr_irqs |= irqs;
    281      1.1  jmcneill 		cv_broadcast(&sc->sc_intr_cv);
    282      1.1  jmcneill 	}
    283      1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
    284      1.1  jmcneill 
    285      1.1  jmcneill 	return 1;
    286      1.1  jmcneill }
    287      1.1  jmcneill 
    288      1.1  jmcneill static void
    289      1.1  jmcneill meson_sdio_dmainit(struct meson_sdio_softc *sc)
    290      1.1  jmcneill {
    291      1.1  jmcneill 	int error, rseg;
    292      1.1  jmcneill 
    293      1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, MAXPHYS, PAGE_SIZE, MAXPHYS,
    294      1.1  jmcneill 	    sc->sc_segs, 1, &rseg, BUS_DMA_WAITOK);
    295      1.1  jmcneill 	if (error) {
    296      1.1  jmcneill 		device_printf(sc->sc_dev, "bus_dmamem_alloc failed\n");
    297      1.1  jmcneill 		return;
    298      1.1  jmcneill 	}
    299      1.1  jmcneill 	KASSERT(rseg == 1);
    300      1.1  jmcneill 
    301      1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, sc->sc_segs, rseg, MAXPHYS,
    302      1.1  jmcneill 	    &sc->sc_bbuf, BUS_DMA_WAITOK);
    303      1.1  jmcneill 	if (error) {
    304      1.1  jmcneill 		device_printf(sc->sc_dev, "bus_dmamem_map failed\n");
    305      1.1  jmcneill 		return;
    306      1.1  jmcneill 	}
    307      1.1  jmcneill 
    308      1.1  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, 1, MAXPHYS, 0,
    309      1.1  jmcneill 	    BUS_DMA_WAITOK, &sc->sc_dmamap);
    310      1.1  jmcneill 	if (error) {
    311      1.1  jmcneill 		device_printf(sc->sc_dev, "bus_dmamap_create failed\n");
    312      1.1  jmcneill 		return;
    313      1.1  jmcneill 	}
    314      1.1  jmcneill }
    315      1.1  jmcneill 
    316      1.1  jmcneill static int
    317      1.1  jmcneill meson_sdio_set_clock(struct meson_sdio_softc *sc, u_int freq)
    318      1.1  jmcneill {
    319      1.1  jmcneill 	const u_int pll_freq = sc->sc_bus_freq / 2000;
    320      1.1  jmcneill 	uint32_t conf;
    321      1.1  jmcneill 	int clk_div;
    322      1.1  jmcneill 
    323      1.1  jmcneill 	if (freq == 0)
    324      1.1  jmcneill 		return 0;
    325      1.1  jmcneill 
    326      1.1  jmcneill 	clk_div = howmany(pll_freq, freq);
    327      1.1  jmcneill 
    328      1.1  jmcneill 	conf = SDIO_READ(sc, SDIO_CONF_REG);
    329      1.1  jmcneill 	conf &= ~SDIO_CONF_COMMAND_CLK_DIV;
    330      1.1  jmcneill 	conf |= __SHIFTIN(clk_div - 1, SDIO_CONF_COMMAND_CLK_DIV);
    331      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_CONF_REG, conf);
    332      1.1  jmcneill 
    333      1.1  jmcneill 	return 0;
    334      1.1  jmcneill }
    335      1.1  jmcneill 
    336      1.1  jmcneill static int
    337      1.1  jmcneill meson_sdio_wait_irqs(struct meson_sdio_softc *sc, uint32_t mask, int timeout)
    338      1.1  jmcneill {
    339      1.1  jmcneill 	int retry, error;
    340      1.1  jmcneill 
    341      1.1  jmcneill 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    342      1.1  jmcneill 
    343      1.1  jmcneill 	if (sc->sc_intr_irqs & mask)
    344      1.1  jmcneill 		return 0;
    345      1.1  jmcneill 
    346      1.1  jmcneill 	retry = timeout / hz;
    347      1.1  jmcneill 
    348      1.1  jmcneill 	while (retry > 0) {
    349      1.1  jmcneill 		error = cv_timedwait(&sc->sc_intr_cv, &sc->sc_intr_lock, hz);
    350      1.1  jmcneill 		if (error && error != EWOULDBLOCK)
    351      1.1  jmcneill 			return error;
    352      1.1  jmcneill 		if (sc->sc_intr_irqs & mask)
    353      1.1  jmcneill 			return 0;
    354      1.1  jmcneill 		--retry;
    355      1.1  jmcneill 	}
    356      1.1  jmcneill 
    357      1.1  jmcneill 	return ETIMEDOUT;
    358      1.1  jmcneill }
    359      1.1  jmcneill 
    360      1.1  jmcneill static int
    361      1.1  jmcneill meson_sdio_host_reset(sdmmc_chipset_handle_t sch)
    362      1.1  jmcneill {
    363      1.1  jmcneill 	struct meson_sdio_softc *sc = sch;
    364      1.1  jmcneill 
    365      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_IRQC_REG, SDIO_IRQC_SOFT_RESET);
    366      1.1  jmcneill 
    367      1.1  jmcneill 	delay(2);
    368      1.1  jmcneill 
    369      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_IRQS_REG, SDIO_IRQS_CLEAR);
    370      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_CONF_REG,
    371      1.1  jmcneill 	    __SHIFTIN(2, SDIO_CONF_WRITE_CRC_OK_STATUS) |
    372      1.1  jmcneill 	    __SHIFTIN(2, SDIO_CONF_WRITE_NWR) |
    373      1.1  jmcneill 	    __SHIFTIN(3, SDIO_CONF_M_ENDIAN) |
    374      1.1  jmcneill 	    __SHIFTIN(39, SDIO_CONF_COMMAND_ARG_BITS) |
    375      1.1  jmcneill 	    __SHIFTIN(0x1f4, SDIO_CONF_COMMAND_CLK_DIV));
    376      1.1  jmcneill 
    377      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_MULT_REG,
    378      1.1  jmcneill 	    __SHIFTIN(sc->sc_cur_port, SDIO_MULT_PORT_SEL));
    379      1.1  jmcneill 
    380      1.1  jmcneill 	return 0;
    381      1.1  jmcneill }
    382      1.1  jmcneill 
    383      1.1  jmcneill static uint32_t
    384      1.1  jmcneill meson_sdio_host_ocr(sdmmc_chipset_handle_t sch)
    385      1.1  jmcneill {
    386      1.1  jmcneill 	return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V;
    387      1.1  jmcneill }
    388      1.1  jmcneill 
    389      1.1  jmcneill static int
    390      1.1  jmcneill meson_sdio_host_maxblklen(sdmmc_chipset_handle_t sch)
    391      1.1  jmcneill {
    392      1.1  jmcneill 	return 512;
    393      1.1  jmcneill }
    394      1.1  jmcneill 
    395      1.1  jmcneill static int
    396      1.1  jmcneill meson_sdio_card_detect(sdmmc_chipset_handle_t sch)
    397      1.1  jmcneill {
    398      1.1  jmcneill 	struct meson_sdio_softc *sc = sch;
    399      1.1  jmcneill 	int val;
    400      1.1  jmcneill 
    401      1.1  jmcneill 	if (sc->sc_non_removable || sc->sc_broken_cd) {
    402      1.1  jmcneill 		return 1;
    403      1.1  jmcneill 	} else if (sc->sc_gpio_cd != NULL) {
    404      1.1  jmcneill 		val = fdtbus_gpio_read(sc->sc_gpio_cd);
    405      1.1  jmcneill 		if (sc->sc_gpio_cd_inverted)
    406      1.1  jmcneill 			val = !val;
    407      1.1  jmcneill 		return val;
    408      1.1  jmcneill 	} else {
    409      1.1  jmcneill 		return 1;
    410      1.1  jmcneill 	}
    411      1.1  jmcneill }
    412      1.1  jmcneill 
    413      1.1  jmcneill static int
    414      1.1  jmcneill meson_sdio_write_protect(sdmmc_chipset_handle_t sch)
    415      1.1  jmcneill {
    416      1.1  jmcneill 	struct meson_sdio_softc *sc = sch;
    417      1.1  jmcneill 	int val;
    418      1.1  jmcneill 
    419      1.1  jmcneill 	if (sc->sc_gpio_wp != NULL) {
    420      1.1  jmcneill 		val = fdtbus_gpio_read(sc->sc_gpio_wp);
    421      1.1  jmcneill 		if (sc->sc_gpio_wp_inverted)
    422      1.1  jmcneill 			val = !val;
    423      1.1  jmcneill 		return val;
    424      1.1  jmcneill 	}
    425      1.1  jmcneill 
    426      1.1  jmcneill 	return 0;
    427      1.1  jmcneill }
    428      1.1  jmcneill 
    429      1.1  jmcneill static int
    430      1.1  jmcneill meson_sdio_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    431      1.1  jmcneill {
    432      1.1  jmcneill 	return 0;
    433      1.1  jmcneill }
    434      1.1  jmcneill 
    435      1.1  jmcneill static int
    436      1.1  jmcneill meson_sdio_bus_clock(sdmmc_chipset_handle_t sch, int freq)
    437      1.1  jmcneill {
    438      1.1  jmcneill 	struct meson_sdio_softc *sc = sch;
    439      1.1  jmcneill 
    440      1.1  jmcneill 	return meson_sdio_set_clock(sc, freq);
    441      1.1  jmcneill }
    442      1.1  jmcneill 
    443      1.1  jmcneill static int
    444      1.1  jmcneill meson_sdio_bus_width(sdmmc_chipset_handle_t sch, int width)
    445      1.1  jmcneill {
    446      1.1  jmcneill 	struct meson_sdio_softc *sc = sch;
    447      1.1  jmcneill 	uint32_t conf;
    448      1.1  jmcneill 
    449      1.1  jmcneill 	conf = SDIO_READ(sc, SDIO_CONF_REG);
    450      1.1  jmcneill 	if (width == 1) {
    451      1.1  jmcneill 		conf &= ~SDIO_CONF_BUS_WIDTH;
    452      1.1  jmcneill 	} else if (width == 4) {
    453      1.1  jmcneill 		conf |= SDIO_CONF_BUS_WIDTH;
    454      1.1  jmcneill 	} else {
    455      1.1  jmcneill 		return EINVAL;
    456      1.1  jmcneill 	}
    457      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_CONF_REG, conf);
    458      1.1  jmcneill 
    459      1.1  jmcneill 	sc->sc_cur_width = width;
    460      1.1  jmcneill 
    461      1.1  jmcneill 	return 0;
    462      1.1  jmcneill }
    463      1.1  jmcneill 
    464      1.1  jmcneill static int
    465      1.1  jmcneill meson_sdio_bus_rod(sdmmc_chipset_handle_t sch, int on)
    466      1.1  jmcneill {
    467      1.1  jmcneill 	return ENOTSUP;
    468      1.1  jmcneill }
    469      1.1  jmcneill 
    470      1.1  jmcneill static int
    471      1.1  jmcneill meson_sdio_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
    472      1.1  jmcneill {
    473      1.1  jmcneill 	struct meson_sdio_softc *sc = sch;
    474      1.1  jmcneill 	u_int uvol;
    475      1.1  jmcneill 	int error;
    476      1.1  jmcneill 
    477      1.1  jmcneill 	if (sc->sc_reg_vqmmc == NULL)
    478      1.1  jmcneill 		return 0;
    479      1.1  jmcneill 
    480      1.1  jmcneill 	switch (signal_voltage) {
    481      1.1  jmcneill 	case SDMMC_SIGNAL_VOLTAGE_330:
    482      1.1  jmcneill 		uvol = 3300000;
    483      1.1  jmcneill 		break;
    484      1.1  jmcneill 	case SDMMC_SIGNAL_VOLTAGE_180:
    485      1.1  jmcneill 		uvol = 1800000;
    486      1.1  jmcneill 		break;
    487      1.1  jmcneill 	default:
    488      1.1  jmcneill 		return EINVAL;
    489      1.1  jmcneill 	}
    490      1.1  jmcneill 
    491      1.1  jmcneill 	error = fdtbus_regulator_supports_voltage(sc->sc_reg_vqmmc, uvol, uvol);
    492      1.1  jmcneill 	if (error != 0)
    493      1.1  jmcneill 		return 0;
    494      1.1  jmcneill 
    495      1.1  jmcneill 	error = fdtbus_regulator_set_voltage(sc->sc_reg_vqmmc, uvol, uvol);
    496      1.1  jmcneill 	if (error != 0)
    497      1.1  jmcneill 		return error;
    498      1.1  jmcneill 
    499      1.1  jmcneill 	return fdtbus_regulator_enable(sc->sc_reg_vqmmc);
    500      1.1  jmcneill }
    501      1.1  jmcneill 
    502      1.1  jmcneill static void
    503      1.1  jmcneill meson_sdio_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
    504      1.1  jmcneill {
    505      1.1  jmcneill 	struct meson_sdio_softc *sc = sch;
    506      1.1  jmcneill 	uint32_t send, ext, mult, addr;
    507      1.1  jmcneill 	bool use_bbuf = false;
    508      1.1  jmcneill 	int i;
    509      1.1  jmcneill 
    510      1.1  jmcneill 	KASSERT(cmd->c_blklen <= 512);
    511      1.1  jmcneill 
    512      1.1  jmcneill 	send = ext = mult = addr = 0;
    513      1.1  jmcneill 
    514      1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    515      1.1  jmcneill 
    516      1.1  jmcneill 	if (cmd->c_opcode == SD_IO_SEND_OP_COND ||
    517      1.1  jmcneill 	    cmd->c_opcode == SD_IO_RW_DIRECT ||
    518      1.1  jmcneill 	    cmd->c_opcode == SD_IO_RW_EXTENDED) {
    519      1.1  jmcneill 		cmd->c_error = EINVAL;
    520      1.1  jmcneill 		goto done;
    521      1.1  jmcneill 	}
    522      1.1  jmcneill 
    523      1.1  jmcneill 	sc->sc_intr_irqs = 0;
    524      1.1  jmcneill 
    525      1.1  jmcneill 	if (cmd->c_flags & SCF_RSP_PRESENT) {
    526      1.1  jmcneill 		if (cmd->c_flags & SCF_RSP_136) {
    527      1.1  jmcneill 			send |= __SHIFTIN(133, SDIO_SEND_RESPONSE_BITS);
    528      1.1  jmcneill 			send |= SDIO_SEND_RESPONSE_CRC7_FROM_8;
    529      1.1  jmcneill 		} else {
    530      1.1  jmcneill 			send |= __SHIFTIN(45, SDIO_SEND_RESPONSE_BITS);
    531      1.1  jmcneill 		}
    532      1.1  jmcneill 	}
    533      1.1  jmcneill 	if ((cmd->c_flags & SCF_RSP_CRC) == 0) {
    534      1.1  jmcneill 		send |= SDIO_SEND_RESPONSE_NO_CRC;
    535      1.1  jmcneill 	}
    536      1.1  jmcneill 	if (cmd->c_flags & SCF_RSP_BSY) {
    537      1.1  jmcneill 		send |= SDIO_SEND_CHECK_BUSY_DAT0;
    538      1.1  jmcneill 	}
    539      1.1  jmcneill 
    540      1.1  jmcneill 	if (cmd->c_datalen > 0) {
    541      1.1  jmcneill 		unsigned int nblks, packlen;
    542      1.1  jmcneill 
    543      1.1  jmcneill 		nblks = cmd->c_datalen / cmd->c_blklen;
    544      1.1  jmcneill 		if (nblks == 0 || (cmd->c_datalen % cmd->c_blklen) != 0)
    545      1.1  jmcneill 			++nblks;
    546      1.1  jmcneill 		packlen = (cmd->c_blklen * 8) + (0xf * sc->sc_cur_width);
    547      1.1  jmcneill 
    548      1.1  jmcneill 		send |= __SHIFTIN(nblks - 1, SDIO_SEND_REPEAT_PACKAGE);
    549      1.1  jmcneill 		ext |= __SHIFTIN(packlen, SDIO_EXT_DATA_RW_NUMBER);
    550      1.1  jmcneill 
    551      1.1  jmcneill 		if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
    552      1.1  jmcneill 			send |= SDIO_SEND_RESPONSE_DATA;
    553      1.1  jmcneill 		} else {
    554      1.1  jmcneill 			send |= SDIO_SEND_COMMAND_HAS_DATA;
    555      1.1  jmcneill 		}
    556      1.1  jmcneill 
    557      1.1  jmcneill 		cmd->c_error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
    558      1.1  jmcneill 		    sc->sc_bbuf, MAXPHYS, NULL, BUS_DMA_WAITOK);
    559      1.1  jmcneill 		if (cmd->c_error) {
    560      1.1  jmcneill 			device_printf(sc->sc_dev, "bus_dmamap_load failed\n");
    561      1.1  jmcneill 			goto done;
    562      1.1  jmcneill 		}
    563      1.1  jmcneill 		if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
    564      1.1  jmcneill 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
    565      1.1  jmcneill 			    MAXPHYS, BUS_DMASYNC_PREREAD);
    566      1.1  jmcneill 		} else {
    567      1.1  jmcneill 			memcpy(sc->sc_bbuf, cmd->c_data, cmd->c_datalen);
    568      1.1  jmcneill 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
    569      1.1  jmcneill 			    MAXPHYS, BUS_DMASYNC_PREWRITE);
    570      1.1  jmcneill 		}
    571      1.1  jmcneill 		addr = sc->sc_dmamap->dm_segs[0].ds_addr;
    572      1.1  jmcneill 		use_bbuf = true;
    573      1.1  jmcneill 	}
    574      1.1  jmcneill 	send |= __SHIFTIN(cmd->c_opcode | 0x40, SDIO_SEND_COMMAND_INDEX);
    575      1.1  jmcneill 
    576      1.1  jmcneill 	mult |= __SHIFTIN(sc->sc_cur_port, SDIO_MULT_PORT_SEL);
    577      1.1  jmcneill 
    578      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_IRQC_REG, SDIO_IRQC_SOFT_RESET);
    579      1.1  jmcneill 	delay(2);
    580      1.1  jmcneill 
    581      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_IRQC_REG, SDIO_IRQC_ARC_CMD_INTEN);
    582      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_IRQS_REG, SDIO_IRQS_CLEAR);
    583      1.1  jmcneill 
    584      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_ARGU_REG, cmd->c_arg);
    585      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_MULT_REG, mult);
    586      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_EXT_REG, ext);
    587      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_ADDR_REG, addr);
    588      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_SEND_REG, send);
    589      1.1  jmcneill 
    590      1.1  jmcneill 	cmd->c_error = meson_sdio_wait_irqs(sc, SDIO_IRQS_CMD_INT, hz * 3);
    591      1.1  jmcneill 	if (cmd->c_error) {
    592      1.1  jmcneill 		goto done;
    593      1.1  jmcneill 	}
    594      1.1  jmcneill 
    595      1.1  jmcneill 	if (SDIO_READ(sc, SDIO_IRQS_REG) & SDIO_IRQS_CMD_BUSY) {
    596      1.1  jmcneill 		int retry;
    597      1.1  jmcneill 		for (retry = 10000; retry > 0; retry--) {
    598      1.1  jmcneill 			const uint32_t irqs = SDIO_READ(sc, SDIO_IRQS_REG);
    599      1.1  jmcneill 			if ((irqs & SDIO_IRQS_CMD_BUSY) == 0)
    600      1.1  jmcneill 				break;
    601      1.1  jmcneill 			delay(100);
    602      1.1  jmcneill 		}
    603      1.1  jmcneill 		if (retry == 0) {
    604      1.1  jmcneill 			aprint_debug_dev(sc->sc_dev,
    605      1.1  jmcneill 			    "busy timeout, opcode %d flags %#x datalen %d\n",
    606      1.1  jmcneill 			    cmd->c_opcode, cmd->c_flags, cmd->c_datalen);
    607      1.1  jmcneill 			cmd->c_error = ETIMEDOUT;
    608      1.1  jmcneill 			goto done;
    609      1.1  jmcneill 		}
    610      1.1  jmcneill 	}
    611      1.1  jmcneill 
    612      1.1  jmcneill 	const uint32_t irqs = SDIO_READ(sc, SDIO_IRQS_REG);
    613      1.1  jmcneill 	if (cmd->c_flags & SCF_RSP_CRC) {
    614      1.1  jmcneill 		if ((irqs & SDIO_IRQS_RESPONSE_CRC7_OK) == 0) {
    615      1.1  jmcneill 			device_printf(sc->sc_dev, "response crc error\n");
    616      1.1  jmcneill 			cmd->c_error = EIO;
    617      1.1  jmcneill 			goto done;
    618      1.1  jmcneill 		}
    619      1.1  jmcneill 	}
    620      1.1  jmcneill 	if (cmd->c_datalen > 0) {
    621      1.1  jmcneill 		uint32_t crcmask = SDIO_IRQS_DATA_READ_CRC16_OK|
    622      1.1  jmcneill 				   SDIO_IRQS_DATA_WRITE_CRC16_OK;
    623      1.1  jmcneill 		if ((irqs & crcmask) == 0) {
    624      1.1  jmcneill 			device_printf(sc->sc_dev, "data crc error\n");
    625      1.1  jmcneill 			cmd->c_error = EIO;
    626      1.1  jmcneill 			goto done;
    627      1.1  jmcneill 		}
    628      1.1  jmcneill 	}
    629      1.1  jmcneill 
    630      1.1  jmcneill 	if (cmd->c_flags & SCF_RSP_PRESENT) {
    631      1.1  jmcneill 		mult |= SDIO_MULT_WRITE_READ_OUT_INDEX;
    632      1.1  jmcneill 		mult &= ~SDIO_MULT_RESPONSE_READ_INDEX;
    633      1.1  jmcneill 		SDIO_WRITE(sc, SDIO_MULT_REG, mult);
    634      1.1  jmcneill 
    635      1.1  jmcneill 		if (cmd->c_flags & SCF_RSP_136) {
    636      1.1  jmcneill 			for (i = 0; i < 4; i++) {
    637      1.1  jmcneill 				cmd->c_resp[i] = SDIO_READ(sc, SDIO_ARGU_REG);
    638      1.1  jmcneill 			}
    639      1.1  jmcneill 		} else {
    640      1.1  jmcneill 			cmd->c_resp[0] = SDIO_READ(sc, SDIO_ARGU_REG);
    641      1.1  jmcneill 		}
    642      1.1  jmcneill 	}
    643      1.1  jmcneill 
    644      1.1  jmcneill done:
    645      1.1  jmcneill 	if (use_bbuf) {
    646      1.1  jmcneill 		if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
    647      1.1  jmcneill 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
    648      1.1  jmcneill 			    MAXPHYS, BUS_DMASYNC_POSTREAD);
    649      1.1  jmcneill 		} else {
    650      1.1  jmcneill 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
    651      1.1  jmcneill 			    MAXPHYS, BUS_DMASYNC_POSTWRITE);
    652      1.1  jmcneill 		}
    653      1.1  jmcneill 		bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
    654      1.1  jmcneill 		if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
    655      1.1  jmcneill 			memcpy(cmd->c_data, sc->sc_bbuf, cmd->c_datalen);
    656      1.1  jmcneill 		}
    657      1.1  jmcneill 	}
    658      1.1  jmcneill 	cmd->c_flags |= SCF_ITSDONE;
    659      1.1  jmcneill 
    660      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_IRQC_REG, 0);
    661      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_IRQS_REG, SDIO_IRQS_CLEAR);
    662      1.1  jmcneill 
    663      1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
    664      1.1  jmcneill }
    665      1.1  jmcneill 
    666      1.1  jmcneill static void
    667      1.1  jmcneill meson_sdio_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
    668      1.1  jmcneill {
    669      1.1  jmcneill 	struct meson_sdio_softc *sc = sch;
    670      1.1  jmcneill 	uint32_t irqc;
    671      1.1  jmcneill 
    672      1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    673      1.1  jmcneill 	irqc = SDIO_READ(sc, SDIO_IRQC_REG);
    674      1.1  jmcneill 	if (enable) {
    675      1.1  jmcneill 		irqc |= SDIO_IRQC_ARC_IF_INTEN;
    676      1.1  jmcneill 	} else {
    677      1.1  jmcneill 		irqc &= ~SDIO_IRQC_ARC_IF_INTEN;
    678      1.1  jmcneill 	}
    679      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_IRQC_REG, irqc);
    680      1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
    681      1.1  jmcneill }
    682      1.1  jmcneill 
    683      1.1  jmcneill static void
    684      1.1  jmcneill meson_sdio_card_intr_ack(sdmmc_chipset_handle_t sch)
    685      1.1  jmcneill {
    686      1.1  jmcneill 	struct meson_sdio_softc *sc = sch;
    687      1.1  jmcneill 
    688      1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    689      1.1  jmcneill 	SDIO_WRITE(sc, SDIO_IRQS_REG, SDIO_IRQS_IF_INT);
    690      1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
    691      1.1  jmcneill }
    692